为了通过样本中的生存几率来可视化连续预测变量的分布,我希望按照标题中所述转换 K-M 图。
这样可以很容易地看到连续值的全部范围。我牺牲了生存总体时间分布的可视化,但使用颜色并在 y 轴上保持 P(生存),我可以轻松绘制 1 年、5 年生存率等。
但是,我不确定如何在 R 中执行此操作。我已在生存包中使用了
survfit()
和 ggsurvplot()
中的 survminer
,但不清楚是否支持这样的转换。
您可以使用模型上的
predict
函数来执行此操作,提供连续变量的值以及您希望测量生存概率的时间。
让我们使用生存包中的
lung
示例,其中 age
作为感兴趣的连续变量:
library(survival)
model <- coxph(Surv(time, status) ~ age, data = lung)
现在我们创建一个包含 30 - 80 岁所有年龄段的数据框,随访时间为 6 个月、1 年和 5 年:
newdata <- expand.grid(age = 30:80, time = c(182, 365, 5*365), status = 1)
我们可以将其输入
predict
并获得生存概率,置信区间为 95%:
preds <- predict(model, newdata = newdata, type = 'expected', se.fit = TRUE)
newdata$pred <- exp(-preds$fit)
newdata$upper <- exp(-(preds$fit + 1.96 * preds$se.fit))
newdata$lower <- exp(-(preds$fit - 1.96 * preds$se.fit))
现在我们可以使用 vanilla ggplot 进行绘图:
library(ggplot2)
ggplot(newdata, aes(age, pred, color = factor(time))) +
geom_ribbon(aes(ymax = upper, ymin = lower, fill = factor(time)),
alpha = 0.2, color = NA) +
geom_line() +
scale_fill_discrete('Time', labels = c('6 months', '1 year', '5 years')) +
scale_color_discrete('Time', labels = c('6 months', '1 year', '5 years')) +
scale_y_continuous('Survival probability', labels = scales::percent) +
theme_minimal() +
ggtitle(paste('Survival Probability according to age',
'at 6 months, 1 year, 5 years'))