gpt4 book ai didi

r - 绘制用于 Cox 回归的 Kaplan-Meier

转载 作者:行者123 更新时间:2023-12-04 09:40:49 25 4
gpt4 key购买 nike

我在 R 中使用以下代码建立了一个 Cox 比例风险模型,该模型可以预测死亡率。添加协变量 A、B 和 C 只是为了避免混淆(即年龄、性别、种族),但我们真正对预测变量 X 感兴趣。X 是一个连续变量。

cox.model <- coxph(Surv(time, dead) ~ A + B + C + X, data = df)

现在,我在为此绘制 Kaplan-Meier 曲线时遇到了麻烦。我一直在寻找如何创建这个数字,但我没有太多运气。我不确定是否可以为 Cox 模型绘制 Kaplan-Meier? Kaplan-Meier 是针对我的协变量进行调整还是不需要它们?

我所做的尝试如下,但有人告诉我这是不对的。
plot(survfit(cox.model), xlab = 'Time (years)', ylab = 'Survival Probabilities')

我还试图绘制一个图表,显示死亡率的累积风险。我不知道我是否做得对,因为我尝试了几种不同的方法并得到了不同的结果。理想情况下,我想绘制两条线,一条显示 X 的第 75 个百分点的死亡风险,一条显示 X 的第 25 个百分点。我该怎么做?

我可以列出我尝试过的所有其他内容,但我不想混淆任何人!

非常感谢。

最佳答案

这是取自 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

请注意,该模型使用 fin, age, race, wexp, mar, paro, prio预测 arrest .如 this document 中所述 survfit()函数使用 Kaplan-Meier 估计的存活率。
plot(survfit(mod.allison), ylim=c(0.7, 1), xlab="Weeks",
ylab="Proportion Not Rearrested")

survival estimate plot

我们得到了一个生存率图(具有 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 # same as -log(sfit$surv)

然后只是绘制这些
plot(cumhaz, xlab="weeks ahead", ylab="cumulative hazard",
ylim=c(min(cumhaz.lower), max(cumhaz.upper)))
lines(cumhaz.lower)
lines(cumhaz.upper)

cumhaz

你会想要使用 survfit(..., conf.int=0.50)获得 75% 和 25% 而不是 97.5% 和 2.5% 的波段。

关于r - 绘制用于 Cox 回归的 Kaplan-Meier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32076310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com