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

如何绘制case-cohort分析的cch对象?解决survminer兼容问题

解决case-cohort研究中cch对象的HR绘图问题

问题背景

我开展了一项case-cohort研究,通过survival包的cch()函数做Cox回归,估算包括分子生物标志物在内的多个协变量的HR值,以此评估预后效能。但survminer包的绘图函数仅支持survfit类对象,无法直接兼容cch对象,导致无法快速生成HR森林图等可视化结果。

研究数据参数:总队列规模5404例,子队列包含189例对照和55例病例,cch()的调用代码如下:

# case-cohort: 使用survival包的cch函数
library(survival)

result <- cch(Surv(timetoevent, event) ~ age+ sex+ smoke+ box + weight + cholesterol+ cholesterol_HDL +  molecular_marker, 
              data = dat, subcoh =  ~ casecohort2, id = ~parti, cohort.size = 5404, 
              method = "LinYing", robust = TRUE)

解决方案

方法1:手动提取数据,用ggplot2绘制森林图

直接从cch对象中提取HR、置信区间等核心数据,用ggplot2自定义绘图,灵活性更高:

library(ggplot2)

# 从cch结果中提取关键统计量
cch_coef <- coef(result)
cch_se <- sqrt(diag(result$var))
hr_values <- exp(cch_coef)
ci_lower <- exp(cch_coef - 1.96 * cch_se)
ci_upper <- exp(cch_coef + 1.96 * cch_se)

# 整理成绘图用的数据框
plot_data <- data.frame(
  covariate = names(cch_coef),
  hr = hr_values,
  ci_low = ci_lower,
  ci_high = ci_upper
)

# 绘制森林图
ggplot(plot_data, aes(x = hr, y = reorder(covariate, hr))) +
  geom_vline(xintercept = 1, linetype = "dashed", color = "gray50") +  # HR=1参考线
  geom_point(size = 3, color = "#2c3e50") +
  geom_errorbarh(aes(xmin = ci_low, xmax = ci_high), height = 0.2, color = "#2c3e50") +
  labs(x = "Hazard Ratio (95% Confidence Interval)", y = "Covariates") +
  theme_bw() +
  theme(axis.text.y = element_text(size = 10),
        plot.title = element_text(hjust = 0.5, size = 12, face = "bold"))

方法2:模拟coxph对象,适配survminer函数

survminer的ggforest()等函数依赖coxph对象的结构,我们可以手动构造一个结构相似的对象来调用这些函数:

library(survminer)

# 构造模拟的coxph对象,保留survminer需要的核心字段
fake_coxph_obj <- list(
  coefficients = coef(result),
  var = result$var,
  formula = result$formula,
  n = result$n,
  nevent = result$nevent,
  call = result$call
)
# 给对象添加coxph类属性
class(fake_coxph_obj) <- "coxph"

# 使用ggforest绘制森林图
ggforest(fake_coxph_obj, data = dat)

注意:这种模拟方法仅适配基础绘图功能,部分高级统计输出可能无法正常显示,需根据需求调整。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 22:36:37