Reputation: 31
I am using the tbl_summary() function from the gtsummary package to summarize several continuous variables in my data set. I would like to print a 95% confidence interval for the mean and am using the add_ci() function to accomplish this. The problem is that I cannot get the column to print the appropriate number of significant digits for the 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)
This produces a mean value with two digits after the decimal place, but only rounded whole numbers for the CI.
I have tried:
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))
But get this error:
Error in eval_tidy(pair$lhs, env = default_env) :
argument "x" is missing, with no default
Any help is greatly appreciated.
Edit, sample of data:
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")
Upvotes: 3
Views: 1366
Reputation: 1150
Thanks for the sample data. You can use purrr::partial
for this task. Rather than use style_sigfig
, it's also more appropriate to use style_number
. With style_sigfig
, values up to the thousandth will be displayed for the lower bound of dcs.post.total
in your sample data as 4 digits will always be displayed with digits = 4
. It may be that style_sigfig
is fine for your actual data, but style_number
is generally more applicable.
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))
Upvotes: 3