Skip to contents

Builds a stat_fns function for tbl_custom_summary() that reports the proportion of observations equal to value, with a confidence interval. The returned statistics are {n}, {N}, {prop}, {conf.low} and {conf.high}.

Usage

proportion_summary(
  variable,
  value,
  weights = NULL,
  na.rm = TRUE,
  conf.level = 0.95,
  method = c("wilson", "wilson.no.correct", "wald", "wald.no.correct", "exact",
    "agresti.coull", "jeffreys")
)

Arguments

variable

(string)
Column name of the summarized variable.

value

The values of variable counted as events; several values may be given.

weights

(string)
Column name of optional frequency weights; each row is repeated that many times. Default is NULL.

na.rm

(scalar logical)
Whether missing values of variable are removed. Default is TRUE.

conf.level

(scalar numeric)
Confidence level. Default 0.95.

method

(string)
The interval: "wilson" (the default), "wilson.no.correct", "wald", "wald.no.correct", "exact", "agresti.coull" or "jeffreys", computed in base R as in add_ci(). "asymptotic" is accepted as an alias of "wald.no.correct".

Value

A function for the stat_fns argument of tbl_custom_summary().

See also

Other custom summaries: ratio_summary()

Examples

# Example 1 ----------------------------------
# response rate by grade and treatment
trial |>
  tbl_custom_summary(
    by = trt,
    include = grade,
    stat_fns = ~proportion_summary("response", value = 1),
    statistic = ~"{n}/{N} = {prop}% ({conf.low}; {conf.high})",
    digits = ~list(prop = label_style_percent(digits = 1))
  )