gpt4 book ai didi

r - 修改 gsummary 置信区间中的有效数字时出现问题

转载 作者:行者123 更新时间:2023-12-05 03:28:52 26 4
gpt4 key购买 nike

我正在使用 gtsummary 包中的 tbl_summary() 函数来汇总我的数据集中的几个连续变量。我想打印均值的 95% 置信区间,并使用 add_ci() 函数来完成此操作。问题是我无法让该列为 CI 打印适当数量的有效数字。

df %>% select("dcs.pre.total", "dcs.post.total") %>% 
tbl_summary(percent = "row",
type = everything() ~ "continuous",
statistic = list(all_continuous () ~ "{mean}"),
digits = all_continuous () ~ 2) %>%
add_ci(pattern = "{stat} ({ci})",
style_fun = everything() ~ style_sigfig)

这会生成小数点后两位数字的平均值,但仅为 CI 的四舍五入整数。

我试过:

df %>% select("dcs.pre.total", "dcs.post.total") %>% 
tbl_summary(percent = "row",
type = everything() ~ "continuous",
statistic = list(all_continuous () ~ "{mean}"),
digits = all_continuous () ~ 2) %>%
add_ci(pattern = "{stat} ({ci})",
style_fun = everything() ~ style_sigfig(digits = 2))

但是得到这个错误:

Error in eval_tidy(pair$lhs, env = default_env) : 
argument "x" is missing, with no default

非常感谢任何帮助。

编辑,数据样本:

dput(head(df[,c(83, 84)]))
structure(list(dcs.pre.total = c(43.75, 70.8333333333333, 68.75,
75, 64.5833333333333, 56.25), dcs.post.total = c(25, 4.16666666666667,
16.6666666666667, 16.6666666666667, 20.8333333333333, 10.4166666666667
)), row.names = c(2L, 3L, 4L, 5L, 6L, 9L), class = "data.frame")

最佳答案

感谢您提供样本数据。您可以使用 purrr::partial 来完成这项任务。与其使用style_sigfig,还不如使用style_number。使用 style_sigfig,示例数据中 dcs.post.total 的下限将显示高达千分之一的值,因为 始终显示 4 位数字数字 = 4style_sigfig 可能适合您的实际数据,但 style_number 通常更适用。

library(gtsummary)
df <-
structure(list(dcs.pre.total = c(43.75, 70.8333333333333, 68.75,
75, 64.5833333333333, 56.25), dcs.post.total = c(25, 4.16666666666667,
16.6666666666667, 16.6666666666667, 20.8333333333333, 10.4166666666667
)), row.names = c(2L, 3L, 4L, 5L, 6L, 9L), class = "data.frame")


df %>% select("dcs.pre.total", "dcs.post.total") %>%
tbl_summary(percent = "row",
type = everything() ~ "continuous",
statistic = list(all_continuous () ~ "{mean}"),
digits = all_continuous () ~ 2) %>%
add_ci(pattern = "{stat} ({ci})",
style_fun = everything() ~ purrr::partial(style_number, digits = 2))

关于r - 修改 gsummary 置信区间中的有效数字时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71166601/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com