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)
)(data.frame)
analysis data.
(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).
(string)
name of the grouping (arm) variable. When NULL, a single-column summary
is produced and comparison verbs are not applicable.
(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.
(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".
(scalar)
confidence level for the interval. Default is 0.95.
(character)
two-element character vector of glue templates for the estimate row and the
CI row. Default c("{n} ({p}%)", "({conf.low}, {conf.high})").
(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.
(string)
label of the confidence-interval row. Default is built from conf.level
and method, e.g. "95% CI (Wald, with continuity correction)".
(function)
formatter applied to the CI bounds. Default
label_roche_number(digits = 1, scale = 100).
a tbl_proportion/gtsummary table.
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
# multinomial breakdown
df$cat <- factor(sample(c("CR", "PR", "SD"), 100, TRUE))
tbl_proportion(df, variable = "cat", by = "arm")
Characteristic
A
B