使用gtsummary与survey包时遇问题:找不到Variable列
解决gtsummary处理survey模型时"找不到Variable列"的报错
问题背景
使用survey::svyglm构建准二项逻辑回归模型,模型本身运行正常,但调用gtsummary::tbl_regression生成表格并添加显著性星号时,报错提示"找不到'Variable列'",报错溯源到add_significance_stars函数。
模型构建代码
PSYDISTMODEL2015to2019 <- survey::svyglm( spdmon ~ EcstasyVariable + PSILCY2+ lsdflag + PEYOTE2 +MESC2 + MarriageVariable+ EducationVariable+ CATAG6+irsex+ income+ NEWRACE2+ sexident +KetamineVar + DMTVariable+PoppersVariable+ RiskyVariable+ cocflag+ herflag+ pcpflag+ InhalantVariable + PainRelieverVariable+ TranquilizerVariable + StimulantVariable+ SedativeVariable+ mrjflag+year, data=NSDUH2015to2019Data, family=quasibinomial(), design=totalDesign ) summary(PSYDISTMODEL2015to2019) DistressModelOdds2015to2019 <- exp(cbind(OR = coef(PSYDISTMODEL2015to2019), confint(PSYDISTMODEL2015to2019))) DistressModelOdds2015to2019 <- round(DistressModelOdds2015to2019, digits =2) DistressModelOdds2015to2019[c(2,3),] # 模型相关输出均正常
原表格生成代码(报错版本)
#Past Month Psy Distress PSYDIST2015to2019OverallTable <- tbl_regression(PSYDISTMODEL2015to2019,exponentiate = TRUE, include = c( "EcstasyVariable", "PSILCY2", "lsdflag", "PEYOTE2", "MESC2", "cocflag", "herflag", "pcpflag", "InhalantVariable", "PainRelieverVariable", "TranquilizerVariable", "StimulantVariable", "SedativeVariable", "mrjflag", "PoppersVariable", "DMTVariable", "KetamineVar") , show_single_row = c( "EcstasyVariable", "PSILCY2", "lsdflag", "PEYOTE2", "MESC2", "cocflag", "herflag", "pcpflag", "InhalantVariable", "PainRelieverVariable", "TranquilizerVariable", "StimulantVariable", "SedativeVariable", "mrjflag", "PoppersVariable", "DMTVariable", "KetamineVar" )) %>% add_significance_stars(pattern = "{estimate}{stars} ({conf.low}, {conf.high})", hide_ci = TRUE, hide_se = TRUE) %>% modify_table_styling( columns = estimate, rows = p.value < 0.05 & estimate < 1, text_format = "bold") %>% modify_header(estimate ~ "**aOR (95% CI)**") %>% modify_header(label ~ "**Lifetime Use**") %>% modify_footnote(estimate ~ "aOR = adjusted odds ratio; CI = confidence interval", abbreviation = TRUE)
报错信息
Error in `scope_tidy()`: ! The `.$x` data frame does not have the required "variable" column. Backtrace: 1. ... %>% ... 6. gtsummary::add_significance_stars(...) 7. gtsummary::modify_footnote(x, any_of(pattern_cols[1]) ~ p_footnote) 8. gtsummary:::.combine_update_and_dots(...) 9. broom.helpers::.formula_list_to_named_list(...) 10. broom.helpers:::.single_formula_to_list(...) 11. broom.helpers::.select_to_varnames(...) 12. broom.helpers::scope_tidy(var_info, data)
解决方案
1. 升级依赖包
首先确保gtsummary和broom.helpers是最新版本,旧版本对survey模型的兼容性存在bug:
install.packages(c("gtsummary", "broom.helpers"))
2. 调整add_significance_stars参数
报错核心原因是设置hide_ci=TRUE后,conf.low和conf.high列被移除,但pattern里还在调用这些列,导致后续函数找不到对应字段。修改方式二选一:
方式一:保留CI列,不设置hide_ci=TRUE
PSYDIST2015to2019OverallTable <- tbl_regression( PSYDISTMODEL2015to2019, exponentiate = TRUE, include = c( "EcstasyVariable", "PSILCY2", "lsdflag", "PEYOTE2", "MESC2", "cocflag", "herflag", "pcpflag", "InhalantVariable", "PainRelieverVariable", "TranquilizerVariable", "StimulantVariable", "SedativeVariable", "mrjflag", "PoppersVariable", "DMTVariable", "KetamineVar" ), show_single_row = c( "EcstasyVariable", "PSILCY2", "lsdflag", "PEYOTE2", "MESC2", "cocflag", "herflag", "pcpflag", "InhalantVariable", "PainRelieverVariable", "TranquilizerVariable", "StimulantVariable", "SedativeVariable", "mrjflag", "PoppersVariable", "DMTVariable", "KetamineVar" ) ) %>% add_significance_stars( pattern = "{estimate}{stars} ({conf.low}, {conf.high})", hide_se = TRUE ) %>% modify_table_styling( columns = estimate, rows = p.value < 0.05 & estimate < 1, text_format = "bold" ) %>% modify_header( estimate ~ "**aOR (95% CI)**", label ~ "**Lifetime Use**" ) %>% modify_footnote( estimate ~ "aOR = adjusted odds ratio; CI = confidence interval", abbreviation = TRUE )
方式二:不在pattern中包含CI,通过表头说明
PSYDIST2015to2019OverallTable <- tbl_regression( PSYDISTMODEL2015to2019, exponentiate = TRUE, include = c( "EcstasyVariable", "PSILCY2", "lsdflag", "PEYOTE2", "MESC2", "cocflag", "herflag", "pcpflag", "InhalantVariable", "PainRelieverVariable", "TranquilizerVariable", "StimulantVariable", "SedativeVariable", "mrjflag", "PoppersVariable", "DMTVariable", "KetamineVar" ), show_single_row = c( "EcstasyVariable", "PSILCY2", "lsdflag", "PEYOTE2", "MESC2", "cocflag", "herflag", "pcpflag", "InhalantVariable", "PainRelieverVariable", "TranquilizerVariable", "StimulantVariable", "SedativeVariable", "mrjflag", "PoppersVariable", "DMTVariable", "KetamineVar" ) ) %>% add_significance_stars( pattern = "{estimate}{stars}", hide_se = TRUE, hide_ci = TRUE ) %>% modify_table_styling( columns = estimate, rows = p.value < 0.05 & estimate < 1, text_format = "bold" ) %>% modify_header( estimate ~ "**aOR (95% CI)**", label ~ "**Lifetime Use**" ) %>% modify_footnote( estimate ~ "aOR = adjusted odds ratio; CI = confidence interval", abbreviation = TRUE )
3. 分步调试定位问题
如果上述方法无效,分步执行代码,先生成基础表格确认正常,再逐步添加修饰:
# 生成基础表格 base_table <- tbl_regression( PSYDISTMODEL2015to2019, exponentiate = TRUE, include = c( "EcstasyVariable", "PSILCY2", "lsdflag", "PEYOTE2", "MESC2", "cocflag", "herflag", "pcpflag", "InhalantVariable", "PainRelieverVariable", "TranquilizerVariable", "StimulantVariable", "SedativeVariable", "mrjflag", "PoppersVariable", "DMTVariable", "KetamineVar" ), show_single_row = c( "EcstasyVariable", "PSILCY2", "lsdflag", "PEYOTE2", "MESC2", "cocflag", "herflag", "pcpflag", "InhalantVariable", "PainRelieverVariable", "TranquilizerVariable", "StimulantVariable", "SedativeVariable", "mrjflag", "PoppersVariable", "DMTVariable", "KetamineVar" ) ) # 检查基础表格是否正常 print(base_table) # 逐步添加修饰 final_table <- base_table %>% add_significance_stars(hide_se = TRUE) %>% modify_table_styling(columns = estimate, rows = p.value < 0.05 & estimate < 1, text_format = "bold") %>% modify_header(estimate ~ "**aOR (95% CI)**", label ~ "**Lifetime Use**") %>% modify_footnote(estimate ~ "aOR = adjusted odds ratio; CI = confidence interval", abbreviation = TRUE)
内容的提问来源于stack exchange,提问作者user29407182
相关产品推荐
相关产品推荐

