[英]Adjusted Survival Curves in R From Cox Model at Specific Covariate Values
I'd like to plot adjusted survival curves from a Cox model at specific covariate values.我想 plot 在特定的协变量值下调整 Cox model 的生存曲线。 The survfit
function in the survival
package and ggsurvplot
in survminer
allow one to easily plot adjusted survival curves from a model, but seem to only do so at mean values of covariates. survfit
package 中的survival
package 和ggsurvplot
中的survminer
允许人们轻松地从 model 调整生存曲线 plot,但似乎仅在协变量的平均值处这样做。 I would like to plot curves at values that I specify, but can't find a way to easily do this in R
.我想 plot 曲线在我指定的值,但找不到在R
中轻松执行此操作的方法。 SAS
can do this easily by using the BASELINE
command in PROC PHREG
and I'm looking to be able to do something like this in R
. SAS
可以通过在PROC PHREG
中使用BASELINE
命令轻松完成此操作,我希望能够在R
中执行类似的操作。
You can do this "by hand".您可以“手动”执行此操作。 Here is one possibility using the example data from coxph()
.这是使用来自coxph()
的示例数据的一种可能性。 Essentially, you need to make a dataset that holds constant all model variables at values you want to use and then vary time from the minimum to maximum in the data (here 0:4).本质上,您需要制作一个数据集,该数据集将所有 model 个变量保持为您要使用的值,然后在数据中从最小值到最大值(此处为 0:4)改变时间。 Then, you can predict the survival probabilities for these data and plot.然后,您可以预测这些数据和 plot 的生存概率。
library(ggplot2)
library(survival)
test1 <- list(time=c(4,3,1,1,2,2,3),
status=c(1,1,1,0,1,1,0),
x=c(0,2,1,1,1,0,0),
sex=c(0,0,0,0,1,1,1))
# Fit a stratified model
m1 <- coxph(Surv(time, status) ~ x + strata(sex), test1)
tmp1 <- data.frame(
time = 0:4,
status=0,
x = 1,
sex = 0)
tmp1$fit <- predict(m1, newdata=tmp1, type="survival")
ggplot(tmp1, aes(x=time, y=fit)) +
geom_line() +
geom_point() +
theme_classic()
Below, we pick a different value of x
, generate predictions and put the data together with the prediction data above.下面,我们选择不同的x
值,生成预测并将数据与上面的预测数据放在一起。 This could be done in a single step if you wanted.如果您愿意,这可以一步完成。
tmp2 <- data.frame(
time = 0:4,
status=0,
x = 2,
sex = 0)
tmp2$fit <- predict(m1, newdata=tmp2, type="survival")
tmp <- rbind(tmp1, tmp2)
Now, we could plot both sets of predictions on the same plot.现在,我们可以对同一个 plot 进行两组预测 plot。
ggplot(tmp, aes(x=time, y=fit, colour=as.factor(x))) +
geom_line() +
geom_point() +
theme_classic() +
labs(colour="X")
Created on 2022-04-29 by the reprex package (v2.0.1)由reprex package (v2.0.1) 创建于 2022-04-29
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.