修复gtsummary单变量Cox回归表报错:聚类变量被误纳入建模
单变量Cox回归表报错修复方案
问题背景
原本可正常运行的单变量Cox回归表出现报错:程序误将聚类变量identifier纳入建模(该变量仅应作为聚类参数),同时伴随指数溢出错误。
错误信息
Adding missing grouping variables:
identifier
x There was an error constructing modelcoxph(formula = Surv(tstart, tstop, status) ~ identifier + strata(Site), data = ., cluster = identifier)See error below.
Error inmutate():
i In argument:model = map(...).
Caused by error inmap():
i In index: 1.
Caused by error invalue[[3L]]():
! Error in agreg.fit(X, Y, istrat, offset, init, control, weights = weights, : exp overflow due to covariates
错误根源
- 数据集
tbl1未包含identifier变量,但method.args中指定了cluster = identifier,程序自动将该变量加入模型公式,违反聚类参数的使用逻辑。 - 测试数据中所有
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
相关产品推荐
相关产品推荐

