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

如何用gt_summary的tbl_strata计算患病率及Wilson 95%置信区间?

解决方法:用add_ci()添加Wilson置信区间

你的报错原因很明确:tbl_summary()本身不接受conf.level和conf.method这两个参数,这俩是专门用来添加置信区间的add_ci()函数的参数。要在分层表格里计算患病率和Wilson 95%置信区间,得调整代码结构,把置信区间的计算放到tbl_summary()之后。

修正后的代码如下:

tbl1 <- tbl_strata(
  data = interestbd_df,
  strata = still_births,
  .tbl_fun = ~tbl_summary(
    .,
    type = all_categorical() ~ "categorical",
    statistic = all_categorical() ~ "{n}/{N} ({p}%)",  # 先显示例数、总数和百分比
    percent = "cell",
    missing = "no",
    by = NULL
  ) %>%
    add_ci(  # 这里添加置信区间
      variables = all_categorical(),
      conf.level = 0.95,
      conf.method = "wilson",
      statistic = "{conf.low}% to {conf.high}%"  # 指定置信区间的显示格式
    ),
  .combine_with = "tbl_stack"
)

关键修正点:

  • 删掉tbl_summary()里的conf.level和conf.method参数,改用add_ci()函数来处理置信区间计算
  • add_ci()里通过variables指定要计算置信区间的变量(这里选所有分类变量),并明确设置conf.method = "wilson"来用Wilson方法计算
  • 调整了statistic的显示格式,让表格先呈现患病率的基础统计量,再追加置信区间列

这样运行后,每个分层组里的分类变量都会显示对应的患病率(n/N和百分比),以及Wilson 95%置信区间。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 15:42:05