我正在尝试使用以下数据集报告 t 检验的 p 值
structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25), strata = c(10,
20, 30, 10, 20, 20, 10, 20, 30, 30, 10, 30, 30, 20, 10, 20, 20,
20, 10, 20, 20, 30, 30, 20, 30), weight = c(10, 8, 17, 15, 9,
10, 25, 8, 8, 13, 17, 24, 12, 15, 3, 12, 16, 17, 24, 12, 3, 2,
8, 14, 4), popgroup = c("A", "B", "A", "A", "A", "A", "B", "B",
"B", "A", "A", "B", "A", "B", "A", "A", "B", "A", "A", "B", "A",
"B", "B", "B", "B"), inc_01 = c(1500, 1200, 130, 500, 750, 2000,
10000, 1500, 1050, 400, 360, 490, 250, 400, 2500, 1300, 800,
540, 690, 520, 600, 700, 700, 600, 400), inc_02 = c(360, 450,
120, 300, 900, 560, 450, 280, 720, 360, 1000, 900, 530, 820,
640, 520, 130, 140, 150, 650, 240, 130, 200, 300, 500)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -25L))
和语法
library(gtsummary)
table_01 <- example %>%
as_survey_design(strata = strata, weights = weight) %>%
select(popgroup, inc_01, inc_02) %>%
filter( !is.na(popgroup)) %>%
gtsummary::tbl_svysummary(
by = popgroup,
type = list(inc_01 ~ "continuous",
inc_02 ~ "continuous"),
statistic = list(c(inc_01, inc_02) ~ "{mean} ({mean.std.error})"),
missing = "no",
digits = list(c(inc_01, inc_02) ~ c(4, 4)),
) %>%
add_p(
test = list(
all_continuous() ~ "svy.t.test",
pvalue_fun = function(x) style_pvalue(x, digits = 4)
)
) %>%
modify_fmt_fun(update = statistic ~ function(x) style_number(x, digits = 6))
table_01
但小数位数仍为 1。 任何帮助将不胜感激。预先感谢!
你们非常亲密;问题就在这里——你应该 (1) 不要将
test
包装在列表中,并且 digits
的 gtsummary::style_pvalue
参数也不能超过 3。
...
gtsummary::add_p(
test = list(
all_continuous() ~ "svy.t.test",
pvalue_fun = function(x) gtsummary::style_pvalue(x, digits = 4)
)
)
...
以下是正确的语法:
table_01 <- example |>
srvyr::as_survey_design(
strata = strata,
weights = weight
) |>
dplyr::select(popgroup, inc_01, inc_02) |>
dplyr::filter(!is.na(popgroup)) |>
gtsummary::tbl_svysummary(
by = popgroup,
type = list(
inc_01 ~ "continuous",
inc_02 ~ "continuous"
),
statistic = list(c(inc_01, inc_02) ~ "{mean} ({mean.std.error})"),
missing = "no",
digits = list(c(inc_01, inc_02) ~ c(4, 4)),
) |>
# You shouldn't be wrapping these arguments in a list
gtsummary::add_p(
test = all_continuous() ~ "svy.t.test",
pvalue_fun = function(x) gtsummary::style_pvalue(x, digits = 3)
) |>
gtsummary::modify_fmt_fun(
update = statistic ~ function(x) gtsummary::style_number(x, digits = 6)
)
table_01