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,
  df = NULL,
  ...
)

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

Arguments

x

A tbl_summary or a tbl_svysummary object

...

Not used

method

Confidence interval method. Default is list(all_categorical() ~ "wilson", all_continuous() ~ "t.test") for tbl_summary objects and list(all_categorical() ~ "svyprop", all_continuous() ~ "svymean") for tbl_svysummary objects. 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.

df

For tbl_svysummary(), the number of degrees of freedom used to estimate confidence intervals. By default, will use survey::degf().

Value

gtsummary table

method argument

for tbl_summary tables

Must be one of c("wilson", "wilson.no.correct", "exact", "asymptotic") for categorical variables, and c("t.test", "wilcox.test") for continuous variables.

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.

for tbl_svysummary tables

Must be one of c("svyprop", "svyprop.logit", "svyprop.likelihood", "svyprop.asin", "svyprop.beta", "svyprop.mean", "svyprop.xlogit") for categorical variables, and c("svymean", "svymedian", "svymedian.mean", "svymedian.beta", "svymedian.xlogit", "svymedian.asin", "svymedian.score") for continuous variables.

Confidence intervals for proportions are computed with survey::svyciprop(). See the help file of this function for details on the different methods available to compute CIs. The default method "svyprop" is equivalent to "svyprop.logit", corresponding to a call to survey::svyciprop() with method = "logit".

Confidence intervals for means (method "svymean") are computed using confint(svymean()).

Confidence intervals for medians are computed with survey::svyquantile(). See the help file of this function for details on the different methods available to compute CIs. The default method "svymedian" is equivalent to "svymedian.mean", corresponding to a call to surevy::svyquantile() with method = "mean".

Example Output

Example 1

image of rendered example table

Example 2

image of rendered example table

Example 3

image of rendered example table

See also

Review list, formula, and selector syntax used throughout gtsummary

Examples

# \donttest{
# 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)

# Example 3 ----------------------------------
data(api, package = "survey")
add_ci_ex3 <-
  survey::svydesign(id = ~dnum, weights = ~pw, data = apiclus1, fpc = ~fpc) %>%
  tbl_svysummary(
    include = c(api00, hsg, stype),
    statistic = hsg ~ "{mean} ({sd})"
  ) %>%
  add_ci(
    method = api00 ~ "svymedian"
  )
# }