我可以使用 tbl_summary 生成汇总表,包括平均值 (sd)、中位数 (IQR) 和范围,但是我想在平均值行下添加一行以包含平均值的置信区间。但使用下面的代码,我只能将 CI 作为附加列。我希望有人能帮助我。
data |>
tbl_summary(by=Cohort,
type = all_continuous() ~ 'continuous2',
include = c(`Total value 1`),
statistic = all_continuous() ~ c("{mean} ({sd})", "{median} ({p25}, {p75})", "{min},{max}"),
) |>
add_ci() |>
add_overall(last=TRUE) |>
bold_labels() |>
add_stat_label(label = all_continuous() ~ c("Mean (SD)", "Median (IQR)", "Min, Max")) |>
as_gt()
请参阅下面的两种方法来获取表中平均值的 95% CI。祝编程愉快!
library(gtsummary)
# Method 1: Define fns for LB and UB ------------
conf.low <- function(x) t.test(x)$conf.int[1]
conf.high <- function(x) t.test(x)$conf.int[2]
trial |>
tbl_summary(
by = trt,
include = age,
missing = "no",
type = all_continuous() ~ "continuous2",
statistic = all_continuous() ~ c("{mean} ({sd})", "{conf.low}, {conf.high}", "{median} ({p25}, {p75})", "{min}, {max}")
) |>
add_stat_label(label = all_continuous() ~ c("Mean (SD)", "95% CI", "Median (IQR)", "Min, Max")) |>
bold_labels() |>
as_kable()
特点 | 药物AN = 98 | 药物BN = 102 |
---|---|---|
年龄 | ||
平均值(SD) | 47 (15) | 47 (14) |
95% CI | 44、50 | 45、50 |
中位数 (IQR) | 46 (37, 60) | 48 (39, 56) |
最小、最大 | 6, 78 | 9, 83 |
# Method 2: Use ARD data structure --------------
library(cards)
trial |>
# first create ARD data frame of results
ard_stack(
.by = trt,
ard_continuous(
variables = age,
statistic = ~ continuous_summary_fns(other_stats = list(ttest = \(x) t.test(x) |> broom::tidy()))
),
.attributes = TRUE,
.missing = TRUE
) |>
# build table with ARD
tbl_ard_summary(
by = trt,
type = all_continuous() ~ "continuous2",
statistic = all_continuous() ~ c("{mean} ({sd})", "{conf.low}, {conf.high}", "{median} ({p25}, {p75})", "{min}, {max}")
) |>
add_stat_label(label = all_continuous() ~ c("Mean (SD)", "95% CI", "Median (IQR)", "Min, Max")) |>
bold_labels() |>
as_kable()
特点 | 药物A | 药物B |
---|---|---|
年龄 | ||
平均值(SD) | 47.0 (14.7) | 47.4 (14.0) |
95% CI | 43.9, 50.1 | 44.6, 50.3 |
中位数 (IQR) | 46.0(37.0,60.0) | 48.0(39.0、56.0) |
最小、最大 | 6.0、78.0 | 9.0, 83.0 |
创建于 2024 年 10 月 23 日,使用 reprex v2.1.1