R 中的生存分析 - 将 Kaplan Meier 图转换为 X = 连续变量,Y = P(生存),彩色线 = 选择的时间点

问题描述 投票:0回答:1

为了通过样本中的生存几率来可视化连续预测变量的分布,我希望按照标题中所述转换 K-M 图。

这样可以很容易地看到连续值的全部范围。我牺牲了生存总体时间分布的可视化,但使用颜色并在 y 轴上保持 P(生存),我可以轻松绘制 1 年、5 年生存率等。

但是,我不确定如何在 R 中执行此操作。我已在生存包中使用了

survfit()
ggsurvplot()
中的
survminer
,但不清楚是否支持这样的转换。

r ggplot2 survival-analysis survival survminer
1个回答
0
投票

您可以使用模型上的

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'))

© www.soinside.com 2019 - 2024. All rights reserved.