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

如何使用gtsummary为数据集各列添加含置信区间的行

问题

希望在表格末尾添加一行,展示数据集(W、S、F、LF)中每一列的置信区间,请问是否可以使用gtsummary包实现该需求?

附数据集及示例代码:

library(gtsummary)

a <- structure(list(SampleDate = structure(c(15710, 15713, 15713, 
15710, 15710, 15713, 15713, 15710, 15708, 15713, 15712, 15708, 
15708, 15713, 15712, 15708), class = "Date"), year = c("2012", 
"2013", "2013", "2012", "2013", "2013", "2013", "2013", "2013", 
"2012", "2013", "2013", "2013", "2013", "2013", "2013"), F = c(0, 
1, 0, 0, 0, 1, 0, 0, 0, 22, 0, 0, 0, 65, 0, 0), W = c(0, 0, 1, 
0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0), S = c(0, 0, 0, 0, 1, 
0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0), LF = c(1, 0, 0, 1, 0, 0, 0, 
1, 0, 0, 0, 0, 1, 0, 0, 1)), class = "data.frame", row.names = c(NA, 
-16L))

# 数据预览
#  SampleDate year  F W S LF
#1  2013-01-05 2012  0 0 0  1
#2  2013-01-08 2013  1 0 0  0
#3  2013-01-08 2013  0 1 0  0
#4  2013-01-05 2012  0 0 0  1
#5  2013-01-05 2013  0 0 1  0
#6  2013-01-08 2013  1 0 0  0
#7  2013-01-08 2013  0 1 0  0
#8  2013-01-05 2013  0 0 0  1
#9  2013-01-03 2013  0 0 1  0
#10 2013-01-08 2012 22 0 0  0
#11 2013-01-07 2013  0 1 0  0
#12 2013-01-03 2013  0 0 1  0
#13 2013-01-03 2013  0 0 0  1
#14 2013-01-08 2013 65 0 0  0
#15 2013-01-07 2013  0 1 0  0
#16 2013-01-03 2013  0 0 0  1

# 原示例代码(处理SampleDate)
a %>%
  select(SampleDate) %>%
  tbl_summary(
    missing =  "no",
    statistic = all_continuous() ~ "{mean} ({sd})") %>% add_ci()
回答

可以用gtsummary包实现这个需求。通过tbl_summary()生成基础统计表格,add_ci()计算置信区间,再结合modify_table_body()自定义添加汇总行,就能在表格末尾展示各列的置信区间。

完整实现代码如下:

library(gtsummary)
library(tibble)

# 你的数据集
a <- structure(list(SampleDate = structure(c(15710, 15713, 15713, 
15710, 15710, 15713, 15713, 15710, 15708, 15713, 15712, 15708, 
15708, 15713, 15712, 15708), class = "Date"), year = c("2012", 
"2013", "2013", "2012", "2013", "2013", "2013", "2013", "2013", 
"2012", "2013", "2013", "2013", "2013", "2013", "2013"), F = c(0, 
1, 0, 0, 0, 1, 0, 0, 0, 22, 0, 0, 0, 65, 0, 0), W = c(0, 0, 1, 
0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0), S = c(0, 0, 0, 0, 1, 
0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0), LF = c(1, 0, 0, 1, 0, 0, 0, 
1, 0, 0, 0, 0, 1, 0, 0, 1)), class = "data.frame", row.names = c(NA, 
-16L))

# 生成带置信区间汇总行的表格
a %>%
  select(F, W, S, LF) %>%  # 筛选目标列
  tbl_summary(
    missing = "no",
    statistic = all_continuous() ~ "{mean} ({sd})",  # 设置基础统计量格式
    label = list(F = "F", W = "W", S = "S", LF = "LF")  # 定义列显示标签
  ) %>%
  add_ci(conf.level = 0.95) %>%  # 添加95%置信区间
  modify_table_body(
    ~ .x %>%
      # 计算各列的置信区间上下限并格式化为[下限, 上限]
      bind_rows(
        tibble(
          variable = "置信区间汇总",
          row_type = "label",
          label = "各列95%置信区间",
          F = paste0("[", round(t.test(a$F)$conf.int[1], 2), ", ", round(t.test(a$F)$conf.int[2], 2), "]"),
          W = paste0("[", round(t.test(a$W)$conf.int[1], 2), ", ", round(t.test(a$W)$conf.int[2], 2), "]"),
          S = paste0("[", round(t.test(a$S)$conf.int[1], 2), ", ", round(t.test(a$S)$conf.int[2], 2), "]"),
          LF = paste0("[", round(t.test(a$LF)$conf.int[1], 2), ", ", round(t.test(a$LF)$conf.int[2], 2), "]")
        )
      )
  )

代码说明:

  • select(F, W, S, LF):只保留需要分析的目标列
  • tbl_summary():生成包含均值和标准差的基础统计表格
  • add_ci():为每个变量计算并添加95%置信区间(可通过conf.level参数调整置信水平)
  • modify_table_body():自定义添加汇总行,通过t.test()直接计算各列的置信区间上下限,格式化为统一的区间样式后插入表格末尾

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 03:54:57