Skip to contents

Adds a column with the confidence interval of the proportion or mean shown in each cell of a table created with tbl_summary(), one column next to each statistic column (and to the overall column of add_overall() when present).

Usage

add_ci(
  x,
  method = list(all_continuous() ~ "t.test", all_categorical() ~ "wilson"),
  include = everything(),
  statistic = list(all_continuous() ~ "{conf.low}, {conf.high}", all_categorical() ~
    "{conf.low}%, {conf.high}%"),
  conf.level = 0.95,
  style_fun = list(all_continuous() ~ label_style_sigfig(), all_categorical() ~
    label_style_sigfig(scale = 100)),
  pattern = NULL,
  ...
)

Arguments

x

(tbl_summary)
A table created with tbl_summary().

method

(formula-list)
The method for each variable. Default is list(all_continuous() ~ "t.test", all_categorical() ~ "wilson"). See the section below.

include

(selector)
Variables to compute an interval for. Default is everything().

statistic

(formula-list)
How the interval is shown, with {conf.low} and {conf.high} in curly braces. Default is list(all_continuous() ~ "{conf.low}, {conf.high}", all_categorical() ~ "{conf.low}%, {conf.high}%").

conf.level

(scalar numeric)
Confidence level. Default is 0.95.

style_fun

(formula-list)
Functions that format the limits. Default is list(all_continuous() ~ label_style_sigfig(), all_categorical() ~ label_style_sigfig(scale = 100)).

pattern

(string)
A pattern merging the interval into the statistic column, with {stat} standing for the statistic and {ci} for the interval, e.g. pattern = "{stat} ({ci})". Default is NULL, which keeps the intervals in their own columns.

...

Not used.

Value

A table of class c("tbl_summary", "ltsummary") with a ci_stat_k column after each stat_k column, headed by the confidence level, or with the intervals merged into the statistic columns when pattern is given.

method argument

For categorical and dichotomous variables, an interval for the proportion of each level (or of the value level), with the denominator of tbl_summary(percent =):

  • "wilson", "wilson.no.correct": prop.test(correct = TRUE) and prop.test(correct = FALSE);

  • "exact": the Clopper-Pearson interval of binom.test();

  • "wald", "wald.no.correct": the Wald interval with and without the continuity correction 1 / (2n), truncated to [0, 1];

  • "agresti.coull": the Wald interval after adding z^2 / 2 successes and failures;

  • "jeffreys": the Beta(n + 1/2, N - n + 1/2) quantiles.

For continuous variables, an interval for the mean or the pseudo-median:

  • "t.test": t.test(x);

  • "wilcox.test": wilcox.test(x, conf.int = TRUE).

A message is given when "t.test" is requested for a variable whose statistic does not show the mean.

See also

add_difference() for the interval of the difference between two groups.

Other add statistics: add_difference(), add_difference_row(), add_n(), add_overall(), add_p(), add_p_continuous, add_q(), add_stat(), add_stat_label(), tests

Examples

# Example 1 ----------------------------------
trial |>
  tbl_summary(
    missing = "no",
    statistic = all_continuous() ~ "{mean} ({sd})",
    include = c(marker, response, trt)
  ) |>
  add_ci()
# Example 2 ---------------------------------- # intervals merged into the cells trial |> tbl_summary( statistic = all_categorical() ~ "{p}%", missing = "no", include = c(response, grade) ) |> add_ci(pattern = "{stat} ({ci})") |> remove_footnote_header(everything())
# Example 3 ---------------------------------- # by group with an overall column, exact intervals, a different level trial |> tbl_summary( by = trt, include = c(age, response), statistic = age ~ "{mean} ({sd})", missing = "no" ) |> add_overall() |> add_ci( method = response ~ "exact", conf.level = 0.9, statistic = age ~ "{conf.low} to {conf.high}" )