如何在tbl_uvregression中使用car::Anova分析geeglm模型?
解决方案
问题原因
gtsummary默认的add_global_p方法对geeglm模型适配不足,导致直接调用car::Anova失败,但手动处理单个模型可行,因此需要自定义适配函数衔接两者。
步骤1:自定义全局检验函数
编写函数接收geeglm模型,调用car::Anova提取卡方值和P值,返回gtsummary兼容的格式:
# 适配geeglm的全局检验函数 custom_anova_geeglm <- function(x, ...) { anova_out <- car::Anova(x, type = 3, test.statistic = "Wald") tibble::tibble( term = "global", statistic = anova_out$`Chisq`, p.value = anova_out$`Pr(>Chisq)`, df = anova_out$Df ) }
步骤2:整合到单变量分析流程
修改原代码,用自定义函数计算全局P值,同时添加卡方列并优化显示:
library(gtsummary) library(geepack) library(dplyr) example <- trial %>% na.omit() %>% tbl_uvregression( method = geepack::geeglm, y = death, method.args = list( family = binomial, id = grade, corstr = "independence" ), exponentiate = TRUE, add_estimate_to_reference_rows = TRUE, conf.level = 0.95 ) %>% # 用自定义函数计算全局P值 add_global_p(fn = custom_anova_geeglm) %>% # 新增卡方值列 modify_table_body( ~ .x %>% mutate(Chisq = case_when(term == "global" ~ round(statistic, 2), TRUE ~ NA_real_)) ) %>% # 调整列顺序,让卡方与P值相邻 modify_column_order(c(estimate, conf.low, conf.high, Chisq, p.value)) %>% # 优化表头显示 modify_header( Chisq = "**Chi-square**", p.value = "**p-value**" ) # 查看结果 example
说明
- 自定义函数专门处理
geeglm模型,确保car::Anova的结果能被gtsummary识别 - 通过
modify_table_body从统计量字段提取卡方值,生成独立列 - 列顺序调整和表头重命名是为了让表格逻辑更清晰,符合常规报告格式
内容的提问来源于stack exchange,提问作者Uknowepi
相关产品推荐
相关产品推荐

