Build a gtsummary table of proportions (with confidence intervals) for a binary or multinomial variable, split by a grouping variable. The estimate and its confidence interval are rendered on two rows, matching the layout of binary-outcome / response summary tables (e.g. RSPT01).

The table is a plain gtsummary object, so the usual modify_*() verbs apply. Between-group comparisons are added with add_proportion_difference() (difference in rates, confidence interval, p-value) and add_proportion_odds_ratio() (odds ratio).

All statistical choices, the confidence-interval method, the conf.level, and (for comparisons) the test, live on these functions rather than in the calling template. Cosmetic choices, factor labels, category wording, indentation, live in the template.

tbl_proportion(
  data,
  variable,
  by = NULL,
  value = NULL,
  method = c("waldcc", "wald", "clopper-pearson", "wilson", "wilsoncc", "agresti-coull",
    "jeffreys", "strat_wilson", "strat_wilsoncc"),
  conf.level = 0.95,
  statistic = c("{n} ({p}%)", "({conf.low}, {conf.high})"),
  label = "Responders",
  ci_label = NULL,
  estimate_fun = label_roche_number(digits = 1, scale = 100)
)

Arguments

data

(data.frame)
analysis data.

variable

(string)
name of the variable to summarize. May be a logical/binary flag (use value = TRUE) or a factor/character with several levels (leave value NULL to report every level).

by

(string)
name of the grouping (arm) variable. When NULL, a single-column summary is produced and comparison verbs are not applicable.

value

(scalar)
for a binary summary, the level counted as a "success" (e.g. TRUE). When NULL (default) every level of variable is reported, each on its own estimate/CI row pair.

method

(string)
confidence-interval method passed to cardx::ard_categorical_ci(). One of "waldcc" (Wald with continuity correction, the default), "wald", "clopper-pearson", "wilson", "wilsoncc", "agresti-coull", "jeffreys", "strat_wilson", "strat_wilsoncc".

conf.level

(scalar)
confidence level for the interval. Default is 0.95.

statistic

(character)
two-element character vector of glue templates for the estimate row and the CI row. Default c("{n} ({p}%)", "({conf.low}, {conf.high})").

label

(string or named list)
for a binary summary, the label of the estimate row (default "Responders"). For a multinomial summary, an optional named list mapping level values to labels; unmapped levels use the level value itself.

ci_label

(string)
label of the confidence-interval row. Default is built from conf.level and method, e.g. "95% CI (Wald, with continuity correction)".

estimate_fun

(function)
formatter applied to the CI bounds. Default label_roche_number(digits = 1, scale = 100).

Value

a tbl_proportion/gtsummary table.

Examples

set.seed(1)
df <- data.frame(
  arm = factor(sample(c("A", "B"), 100, TRUE), levels = c("A", "B")),
  rsp = sample(c(TRUE, FALSE), 100, TRUE)
)

# binary responder summary
tbl_proportion(df, variable = "rsp", by = "arm", value = TRUE)
Characteristic A B
Responders 25 (51.0%) 28 (54.9%)
    95% CI (Wald, with continuity correction) (36.0, 66.0) (40.3, 69.5)
# multinomial breakdown df$cat <- factor(sample(c("CR", "PR", "SD"), 100, TRUE)) tbl_proportion(df, variable = "cat", by = "arm")
Characteristic A B
CR 18 (36.7%) 22 (43.1%)
    95% CI (Wald, with continuity correction) (22.2, 51.3) (28.6, 57.7)
PR 13 (26.5%) 15 (29.4%)
    95% CI (Wald, with continuity correction) (13.1, 39.9) (15.9, 42.9)
SD 18 (36.7%) 14 (27.5%)
    95% CI (Wald, with continuity correction) (22.2, 51.3) (14.2, 40.7)