Add a new column with the confidence intervals for proportions, means, etc.

add_ci(x, ...)

# S3 method for tbl_summary
add_ci(
  x,
  method = NULL,
  include = everything(),
  statistic = NULL,
  conf.level = 0.95,
  style_fun = NULL,
  pattern = NULL,
  ...
)

Arguments

x

A tbl_summary object

...

Not used

method

Confidence interval method. Default is list(all_categorical() ~ "wilson", all_continuous() ~ "t.test"). Must be one of c("wilson", "wilson.no.correct", "exact", "asymptotic") for categorical variables, and c("t.test", "wilcox.test") for continuous variables. See details below.

include

variables to include in the summary table. Default is everything()

statistic

Formula indicating how the confidence interval will be displayed. Default is list(all_categorical() ~ "{conf.low}%, {conf.high}%", all_continuous() ~ "{conf.low}, {conf.high}")

conf.level

Confidence level. Default is 0.95

style_fun

Function to style upper and lower bound of confidence interval. Default is list(all_categorical() ~ purrr::partial(style_sigfig, scale = 100), all_continuous() ~ style_sigfig).

pattern

string indicating the pattern to use to merge the CI with the statistics cell. The default is NULL, where no columns are merged. The two columns that will be merged are the statistics column, represented by "{stat}" and the CI column represented by "{ci}", e.g. pattern = "{stat} ({ci})" will merge the two columns with the CI in parentheses.

Value

gtsummary table

method argument

Methods c("wilson", "wilson.no.correct") are calculated with prop.test(correct = c(TRUE, FALSE)). The default method, "wilson", includes the Yates continuity correction. Methods c("exact", "asymptotic") are calculated with Hmisc::binconf(method=). Confidence intervals for means are calculated using t.test() and wilcox.test() for pseudo-medians.

Example Output

Example 1

Example 2

Examples

# Example 1 ----------------------------------
add_ci_ex1 <-
  trial %>%
  select(marker, response, trt) %>%
  tbl_summary(missing = "no",
              statistic = all_continuous() ~ "{mean} ({sd})") %>%
  add_ci()

# Example 2 ----------------------------------
add_ci_ex2 <-
  trial %>%
    select(response, grade) %>%
    tbl_summary(statistic = all_categorical() ~ "{p}%",
                missing = "no") %>%
    add_ci(pattern = "{stat} ({ci})") %>%
    modify_footnote(everything() ~ NA)