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

使用tbl_regression结合加权Cox模型时add_n/add_nevent显示异常

解决方案

针对加权Cox模型(包括多重插补的mira对象与普通coxph对象)使用tbl_regression()搭配add_n()/add_nevent()时数值不符的问题,核心原因是gtsummary默认未正确识别加权模型内部存储的样本量和事件数,需手动传入模型内的正确数值:

1. 处理多重插补的mira对象(如你的fit_dichot_mice_adjusted)

先从模型对象中提取正确的N和事件数,再传入add_n()和add_nevent():

# 提取每个插补模型的N(所有插补数据集N一致,取第一个即可)
correct_n <- fit_dichot_mice_adjusted$analyses[[1]]$n
# 提取所有插补模型的事件数,取均值后取整(或根据需求直接取单个模型的数值)
correct_nevent <- sapply(fit_dichot_mice_adjusted$analyses, function(x) x$nevent) %>% 
  mean() %>% round()

# 生成表格时手动传入正确数值
tbl_regression(fit_dichot_mice_adjusted, 
      exponentiate = T,
      pvalue_fun = label_style_pvalue(digits = 2),
      show_single_row = "intervention_status",
      label = list(intervention_status = "5 < treatments")) %>% 
      add_n(n = correct_n) %>% 
      add_nevent(nevent = correct_nevent)

2. 处理普通加权coxph对象

直接从模型中提取内置的N和事件数传入函数:

# 假设加权模型名为fit_weighted
correct_n <- fit_weighted$n
correct_nevent <- fit_weighted$nevent

tbl_regression(fit_weighted, 
      exponentiate = T,
      pvalue_fun = label_style_pvalue(digits = 2),
      show_single_row = "intervention_status",
      label = list(intervention_status = "5 < treatments")) %>% 
      add_n(n = correct_n) %>% 
      add_nevent(nevent = correct_nevent)

原因说明

add_n()默认会提取模型拟合时的原始数据集行数,而非加权模型内部记录的有效样本量;add_nevent()同理,未自动读取coxph模型内置的nevent值。手动传入模型中存储的正确数值即可修正显示偏差。

内容的提问来源于stack exchange,提问作者Y.Silverman

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 15:55:12