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

修复gtsummary单变量Cox回归表报错:聚类变量被误纳入建模

单变量Cox回归表报错修复方案

问题背景

原本可正常运行的单变量Cox回归表出现报错:程序误将聚类变量identifier纳入建模(该变量仅应作为聚类参数),同时伴随指数溢出错误。

错误信息

Adding missing grouping variables: identifier
x There was an error constructing model coxph(formula = Surv(tstart, tstop, status) ~ identifier + strata(Site), data = ., cluster = identifier) See error below.
Error in mutate():
i In argument: model = map(...).
Caused by error in map():
i In index: 1.
Caused by error in value[[3L]]():
! Error in agreg.fit(X, Y, istrat, offset, init, control, weights = weights, : exp overflow due to covariates

错误根源

  1. 数据集tbl1未包含identifier变量,但method.args中指定了cluster = identifier,程序自动将该变量加入模型公式,违反聚类参数的使用逻辑。
  2. 测试数据中所有status值均为0(无事件发生),导致Cox模型拟合时出现指数溢出。

修改方案

  • 保留数据集中的identifier变量,确保聚类参数能正确引用
  • 使用quote()包裹聚类变量,避免被误解析为模型协变量
  • 确保实际数据中存在事件(status至少有部分为1)

修复后的代码

# 筛选数据时保留聚类变量identifier
tbl1 <- cox.model.data %>% 
  select(
    identifier,
    tstart,
    tstop,
    status,
    Site,
    job_cat
  )

# 构建单变量Cox回归表
uvlm_table <- tbl_uvregression(
  data = tbl1,
  method = coxph,
  y = Surv(tstart, tstop, status),
  # 用quote()避免cluster变量被误纳入模型公式
  method.args = list(cluster = quote(identifier)),
  formula = "{y} ~ {x} + strata(Site)",
  exponentiate = TRUE,
  pvalue_fun = ~style_pvalue(.x, digits=3)
)

关键说明

  • 必须在数据中保留identifier,否则coxph无法找到聚类变量
  • quote(identifier)让程序将其视为聚类参数,而非模型的自变量
  • 若数据中无事件(全0的status),需补充事件数据或调整分析思路,否则模型无法正常拟合

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 14:40:25