Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display confidence intervals coorrectly with multiple stats in tbl_summary

Tags:

r

gt

gtsummary

I have the follow code to display multiple statistics. I need to display the confidence interval too, but the code display the CI in the wrong line, I would like to display in same line that mean(sd) are displayed.

library(gtsummary)
iris %>% 
  select(Sepal.Length, Sepal.Width,Species) %>% 
  tbl_summary(by = Species,
              type = all_continuous() ~ "continuous2",
              statistic = all_continuous() ~ c(
                "{mean} ({sd})",
                "{median} ({p25}, {p75})",
                "{min}, {max}"
              ),
              missing = "no", digits = everything() ~ 1) %>% 
  add_ci(include = all_continuous(),
         statistic = all_continuous()~ "{conf.low}, {conf.high}")

Result table:

enter image description here

Table I want:

enter image description here

like image 643
Plinio Leal Avatar asked Oct 21 '25 11:10

Plinio Leal


1 Answers

I am not sure if there is an argument within add_ci() to get what you need; but we can modify the output and shift the CI columns down.

library(gtsummary)

iris %>% 
  select(Sepal.Length, Sepal.Width,Species) %>% 
  tbl_summary(by = Species,
              type = all_continuous() ~ "continuous2",
              statistic = all_continuous() ~ c(
                "{mean} ({sd})",
                "{median} ({p25}, {p75})",
                "{min}, {max}"
              ),
              missing = "no", digits = everything() ~ 1) %>% 
  add_ci(include = all_continuous(),
         statistic = all_continuous()~ "{conf.low}, {conf.high}") -> summary_table

for (x in paste0("ci_stat_", 1:3)) {
    summary_table$table_body[[x]] <- dplyr::lag(summary_table$table_body[[x]])
 }

summary_table

Created on 2024-03-08 with reprex v2.0.2

like image 129
M-- Avatar answered Oct 23 '25 02:10

M--