Him*_*ims 8 plot r cox-regression survival-analysis
我使用R中的以下代码建立了Cox比例风险模型,用于预测死亡率.协变量A,B和C的添加只是为了避免混淆(即年龄,性别,种族),但我们真的对预测X感兴趣.X是一个连续变量.
cox.model <- coxph(Surv(time, dead) ~ A + B + C + X, data = df)
Run Code Online (Sandbox Code Playgroud)
现在,我正在为此绘制Kaplan-Meier曲线.我一直在寻找如何创造这个数字,但我没有太多运气.我不确定是否可以为Cox模型绘制Kaplan-Meier?Kaplan-Meier是否适合我的协变量或不需要它们?
我尝试过的是下面,但我被告知这是不对的.
plot(survfit(cox.model), xlab = 'Time (years)', ylab = 'Survival Probabilities')
Run Code Online (Sandbox Code Playgroud)
我还试图绘制一个显示死亡累积危险的数字.我不知道我是否正确行事,因为我尝试了几种不同的方式并获得了不同的结果.理想情况下,我想绘制两条线,一条显示X的第75百分位的死亡风险,另一条显示X的第25百分位.我怎么能这样做?
我可以列出我尝试过的所有其他内容,但我不想混淆任何人!
非常感谢.
下面是取自例如本文.
url <- "http://socserv.mcmaster.ca/jfox/Books/Companion/data/Rossi.txt"
Rossi <- read.table(url, header=TRUE)
Rossi[1:5, 1:10]
# week arrest fin age race wexp mar paro prio educ
# 1 20 1 no 27 black no not married yes 3 3
# 2 17 1 no 18 black no not married yes 8 4
# 3 25 1 no 19 other yes not married yes 13 3
# 4 52 0 yes 23 black yes married yes 1 5
# 5 52 0 no 19 other yes not married yes 3 3
mod.allison <- coxph(Surv(week, arrest) ~
fin + age + race + wexp + mar + paro + prio,
data=Rossi)
mod.allison
# Call:
# coxph(formula = Surv(week, arrest) ~ fin + age + race + wexp +
# mar + paro + prio, data = Rossi)
#
#
# coef exp(coef) se(coef) z p
# finyes -0.3794 0.684 0.1914 -1.983 0.0470
# age -0.0574 0.944 0.0220 -2.611 0.0090
# raceother -0.3139 0.731 0.3080 -1.019 0.3100
# wexpyes -0.1498 0.861 0.2122 -0.706 0.4800
# marnot married 0.4337 1.543 0.3819 1.136 0.2600
# paroyes -0.0849 0.919 0.1958 -0.434 0.6600
# prio 0.0915 1.096 0.0286 3.194 0.0014
#
# Likelihood ratio test=33.3 on 7 df, p=2.36e-05 n= 432, number of events= 114
Run Code Online (Sandbox Code Playgroud)
请注意,该模型用于fin, age, race, wexp, mar, paro, prio预测arrest.如本文件中所述,该survfit()函数使用Kaplan-Meier估计存活率.
plot(survfit(mod.allison), ylim=c(0.7, 1), xlab="Weeks",
ylab="Proportion Not Rearrested")
Run Code Online (Sandbox Code Playgroud)
我们得到了一个生存率的图(置信区间为95%).对于您可以做的累积危险率
# plot(survfit(mod.allison)$cumhaz)
Run Code Online (Sandbox Code Playgroud)
但这并没有给出置信区间.但是,不用担心!我们知道H(t)= -ln(S(t))并且我们对S(t)有置信区间.我们所需要做的就是
sfit <- survfit(mod.allison)
cumhaz.upper <- -log(sfit$upper)
cumhaz.lower <- -log(sfit$lower)
cumhaz <- sfit$cumhaz # same as -log(sfit$surv)
Run Code Online (Sandbox Code Playgroud)
然后只是绘制这些
plot(cumhaz, xlab="weeks ahead", ylab="cumulative hazard",
ylim=c(min(cumhaz.lower), max(cumhaz.upper)))
lines(cumhaz.lower)
lines(cumhaz.upper)
Run Code Online (Sandbox Code Playgroud)
您需要使用survfit(..., conf.int=0.50)75%和25%而不是97.5%和2.5%的乐队.