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

如何使用tbl_summary展示ANOVA的检验统计量F值

问题原因

Error: Error in update= argument input. Select from ‘variable’, ‘test_name’, ‘var_type’, ‘var_label’, ‘row_type’, ‘label’, ‘stat_1’, ‘stat_2’, ‘stat_3’, ‘stat_4’, ‘test_result’, ‘p.value’

该报错的核心原因是:默认情况下add_p()仅会在汇总表中输出p值列,不会自动包含ANOVA的检验统计量列,你尝试修改不存在的statistic列表头,才会触发可选列范围的报错。

你之前将statistic替换为test_result后输出异常,是因为test_result是gtsummary默认生成的、拼接了检验名称+统计量+p值的汇总列,并非单独的检验统计量列,不符合你的输出需求。

解决方案

你需要先在add_p()中通过include参数主动把检验统计量加入输出表,再修改对应列表头即可,修改后的完整代码如下:

hiv %>% 
  select(education, sexfirsttime) %>% 
  mutate(education=
  factor(education, levels= c("no education", "primary","secondary","college"))) %>% 
  tbl_summary(missing="no", 
              by=education,
    statistic = all_continuous() ~"{mean} ({sd})", 
    label = sexfirsttime ~ "Age of first time sex") %>% 
  # 增加include参数,同时输出p值和检验统计量
  add_p(test= all_continuous() ~ "aov", include = c(p.value, statistic)) %>% 
  modify_header(statistic ~ "**Test Statistic**",
                p.value ~ "**p值**") # 可按需调整p值列的表头

补充说明

  • 如果需要调整检验统计量的显示格式,可以结合modify_fmt_fun()函数自定义统计量的小数位数等样式。
  • 该方案适配gtsummary v1.6.0及以上版本,如果你使用的版本过低,可先升级包再运行代码。

内容的提问来源于stack exchange,提问作者Bidhya Sharma

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 17:36:07