问题描述
我有一个Cox比例风险模型,该模型使用R中的以下代码来预测死亡率。添加协变量A,B和C只是为了避免混淆(即年龄,性别,种族),但我们对预测变量X确实很感兴趣。X是连续变量。
cox.model<-coxph(生存(时间,死亡)〜A + B + C + X,数据= df)
现在,我很难为此绘制Kaplan-Meier曲线。我一直在寻找如何创建这个数字的方法,但是运气不高。我不确定是否可以为Cox模型绘制Kaplan-Meier? Kaplan-Meier是否针对我的协变量进行调整还是不需要它们?
我在下面尝试过,但被告知这是不对的。
图(survfit(cox.model),xlab =时间(年),ylab =生存概率)$我还试图绘制一个数字,显示死亡的累积危害。我不知道自己是否做对了,因为我尝试了几种不同的方法并获得了不同的结果。理想情况下,我想绘制两条线,一条线显示X的第75个百分位数的死亡风险,另一条线显示X的第25个百分位数。我该怎么做?
我可以列出我尝试过的所有其他内容,但我不想让任何人感到困惑!
非常感谢。
解决方案以下是从
我们得到了一个图(置信区间为95%)存活率。对于累积危害率,您可以执行以下操作
#plot(survfit(mod.allison)$ cumhaz)
但这不会给出置信区间。但是,不用担心!我们知道H(t)= -ln(S(t)),我们有S(t)的置信区间。我们所需要做的就是
sfit<-survfit(mod.allison)
cumhaz.upper<- -log(sfit $ upper)
cumhaz.lower<---log(sfit $ lower)
cumhaz<-sfit $ cumhaz#与-log(sfit $ surv)
然后只需绘制这些
图(cumhaz,xlab =未来几周,ylab =累积危害,
ylim = c(min(cumhaz.lower),max(cumhaz.upper)))
线(cumhaz .lower)
行(cumhaz.upper)
您将要使用 survfit(...,conf.int = 0.50)
获取乐队75%和25%,而不是97.5%和2.5%。
I have a Cox proportional hazards model set up using the following code in R that predicts mortality. Covariates A, B and C are added simply to avoid confounding (i.e. age, sex, race) but we are really interested in the predictor X. X is a continuous variable.
cox.model <- coxph(Surv(time, dead) ~ A + B + C + X, data = df)
Now, I'm having troubles plotting a Kaplan-Meier curve for this. I've been searching on how to create this figure but I haven't had much luck. I'm not sure if plotting a Kaplan-Meier for a Cox model is possible? Does the Kaplan-Meier adjust for my covariates or does it not need them?
What I did try is below, but I've been told this isn't right.
plot(survfit(cox.model), xlab = 'Time (years)', ylab = 'Survival Probabilities')
I also tried to plot a figure that shows cumulative hazard of mortality. I don't know if I'm doing it right since I've tried it a few different ways and get different results. Ideally, I would like to plot two lines, one that shows the risk of mortality for the 75th percentile of X and one that shows the 25th percentile of X. How can I do this?
I could list everything else I've tried, but I don't want to confuse anyone!
Many thanks.
解决方案 Here is an example taken from this paper.
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
Note that the model uses fin, age, race, wexp, mar, paro, prio
to predict arrest
. As mentioned in this document the survfit()
function uses the Kaplan-Meier estimate for the survival rate.
plot(survfit(mod.allison), ylim=c(0.7, 1), xlab="Weeks",
ylab="Proportion Not Rearrested")
We get a plot (with a 95% confidence interval) for the survival rate. For the cumulative hazard rate you can do
# plot(survfit(mod.allison)$cumhaz)
but this doesn't give confidence intervals. However, no worries! We know that H(t) = -ln(S(t)) and we have confidence intervals for S(t). All we need to do is
sfit <- survfit(mod.allison)
cumhaz.upper <- -log(sfit$upper)
cumhaz.lower <- -log(sfit$lower)
cumhaz <- sfit$cumhaz # same as -log(sfit$surv)
Then just plot these
plot(cumhaz, xlab="weeks ahead", ylab="cumulative hazard",
ylim=c(min(cumhaz.lower), max(cumhaz.upper)))
lines(cumhaz.lower)
lines(cumhaz.upper)
You'll want to use survfit(..., conf.int=0.50)
to get bands for 75% and 25% instead of 97.5% and 2.5%.
这篇关于绘制Kaplan-Meier进行Cox回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!