proportion_summary: Summarize a proportion

Description Usage Arguments Details Example Output Author(s) See Also Examples

View source: R/utils-tbl_custom_summary.R

Description

\lifecycle

experimental This helper, to be used with tbl_custom_summary(), creates a function computing a proportion and its confidence interval.

Usage

1
2
3
4
5
6
7
8
proportion_summary(
  variable,
  value,
  weights = NULL,
  na.rm = TRUE,
  conf.level = 0.95,
  method = c("wilson", "wilson.no.correct", "exact", "asymptotic")
)

Arguments

variable

String indicating the name of the variable from which the proportion will be computed.

value

Value (or list of values) of variable to be taken into account in the numerator.

weights

Optional string indicating the name of a weighting variable. If NULL, all observations will be assumed to have a weight equal to 1.

na.rm

Should missing values be removed before computing the proportion? (default is TRUE)

conf.level

Confidence level for the returned confidence interval. Must be strictly greater than 0 and less than 1. Default to 0.95, which corresponds to a 95 percent confidence interval.

method

Confidence interval method. Must be one of c("wilson", "wilson.no.correct", "exact", "asymptotic"). See details below.

Details

Computed statistics:

Methods c("wilson", "wilson.no.correct") are calculated with stats::prop.test() (with correct = c(TRUE, FALSE)). The default method, "wilson", includes the Yates continuity correction. Methods c("exact", "asymptotic") are calculated with Hmisc::binconf() and the corresponding method.

Example Output

Example 1

Author(s)

Joseph Larmarange

See Also

Other tbl_custom_summary tools: add_overall(), continuous_summary(), ratio_summary(), tbl_custom_summary()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# Example 1 ----------------------------------
proportion_summary_ex1 <-
  Titanic %>%
  as.data.frame() %>%
  tbl_custom_summary(
    include = c("Age", "Class"),
    by = "Sex",
    stat_fns = ~ proportion_summary("Survived", "Yes", weights = "Freq"),
    statistic = ~ "{prop}% ({n}/{N}) [{conf.low}-{conf.high}]",
    digits = ~ list(
      function(x) {style_percent(x, digits = 1)},
      0, 0, style_percent, style_percent
    ),
    overall_row = TRUE,
    overall_row_last = TRUE
  ) %>%
  bold_labels() %>%
  modify_footnote(
    update = all_stat_cols() ~ "Proportion (%) of survivors (n/N) [95% CI]"
  )

gtsummary documentation built on Jan. 21, 2022, 1:09 a.m.