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

如何在gtsummary的add_difference()中输出二元变量实际均值差而非标准化均值差?

解决方案

当然可以实现,通过调整add_difference()的参数,就能让二元变量输出实际比例差值而非标准化均值差,同时保留95%置信区间。具体代码调整如下:

library(gtsummary)

trial %>% 
  select(-c("stage")) %>%
  tbl_summary(by = trt,
              statistic = list(
                all_continuous() ~ "{mean} ({sd})",
                all_categorical() ~ "{n} / {N} ({p}%)"
              )) %>%
  add_difference(
    # 指定二元变量计算原始比例差
    estimate = list(all_binary() ~ "difference"),
    # 用比例检验计算95%置信区间
    test = list(all_binary() ~ "prop.test"),
    # 自定义差值和CI的显示格式(百分比形式)
    statistic = list(all_binary() ~ "{estimate}% ({conf.low}% to {conf.high}%)")
  )

参数说明:

  • estimate = list(all_binary() ~ "difference"):强制所有二元变量计算组间原始比例差值,替代默认的标准化均值差。
  • test = list(all_binary() ~ "prop.test"):调用比例检验方法,自动生成差值对应的95%置信区间。
  • statistic参数:自定义输出格式,让差值和置信区间都以百分比形式展示,和你示例里的需求完全匹配。

如果只需要针对特定二元变量调整,把all_binary()替换成具体变量名即可,比如vars(response, death)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 13:07:16