You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R中绘制多模型生存分析决策曲线(Cox比例风险回归)

生存数据DCA对比单变量与多变量Cox模型

要在同一张决策曲线分析图中对比单变量(仅marker)和多变量(famhistory + marker)Cox模型的结果,核心是先为两个模型分别生成对应时间点的失败概率预测值,将它们合并到同一数据集后,一次性传入dca()函数即可实现同图对比。

完整实现代码

library(dcurves)
library(dplyr)
library(broom)
library(survival)
library(ggplot2)

# 1. 训练两个Cox模型
# 单变量模型(仅marker)
cox_univar <- coxph(Surv(ttcancer, cancer) ~ marker, data = df_surv)
# 多变量模型(famhistory + marker)
cox_multivar <- coxph(Surv(ttcancer, cancer) ~ famhistory + marker, data = df_surv)

# 2. 生成两个模型的1.5年失败概率预测值,并合并到数据集
df_surv_combined <- df_surv %>%
  # 统一预测时间点为1.5年
  mutate(ttcancer = 1.5) %>%
  # 单变量模型预测
  broom::augment(cox_univar, newdata = ., type.predict = "expected") %>%
  mutate(pr_failure18_univar = 1 - exp(-.fitted)) %>%
  select(-.fitted, -.se.fit) %>%
  # 多变量模型预测
  broom::augment(cox_multivar, newdata = ., type.predict = "expected") %>%
  mutate(pr_failure18_multivar = 1 - exp(-.fitted)) %>%
  select(-.fitted, -.se.fit)

# 3. 运行决策曲线分析,同时纳入两个预测变量
dca_result <- dca(Surv(ttcancer, cancer) ~ pr_failure18_univar + pr_failure18_multivar,
                  data = df_surv_combined,
                  time = 1.5)

# 4. 绘制对比图
plot(dca_result) +
  labs(title = "1.5年癌症发生风险的DCA对比",
       subtitle = "单变量Cox模型 vs 多变量Cox模型",
       x = "阈值概率",
       y = "净获益") +
  theme_minimal()

关键说明

  • 预测变量命名:给两个模型的预测值设置不同的变量名(pr_failure18_univar和pr_failure18_multivar),避免混淆。
  • 统一时间点:在生成预测时,将ttcancer统一设为1.5年,确保两个模型的预测都是针对同一时间窗口的失败概率。
  • 多变量DCA输入:dca()函数支持在公式中传入多个预测变量,运行后会自动在同一张图中绘制所有模型的曲线,同时包含"全不干预"和"全干预"的参考线。
  • 自定义绘图:可以通过ggplot2的语法修改图的标题、主题等,让结果更清晰。

内容的提问来源于stack exchange,提问作者mepstein1218

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 00:25:53