Skip to contents

[Stable]

Estimate the proportion of responders within a studied population.

Usage

prop_wilson(rsp, conf_level, correct = FALSE)

prop_strat_wilson(
  rsp,
  strata,
  weights = NULL,
  conf_level = 0.95,
  max_iterations = NULL,
  correct = FALSE
)

prop_clopper_pearson(rsp, conf_level)

prop_wald(rsp, conf_level, correct = FALSE)

prop_agresti_coull(rsp, conf_level)

prop_jeffreys(rsp, conf_level)

s_proportion(
  df,
  .var,
  conf_level = 0.95,
  method = c("waldcc", "wald", "clopper-pearson", "wilson", "wilsonc", "strat_wilson",
    "strat_wilsonc", "agresti-coull", "jeffreys"),
  weights = NULL,
  max_iterations = 50,
  variables = list(strata = NULL),
  long = FALSE
)

a_proportion(
  df,
  .var,
  conf_level = 0.95,
  method = c("waldcc", "wald", "clopper-pearson", "wilson", "wilsonc", "strat_wilson",
    "strat_wilsonc", "agresti-coull", "jeffreys"),
  weights = NULL,
  max_iterations = 50,
  variables = list(strata = NULL),
  long = FALSE
)

estimate_proportion(
  lyt,
  vars,
  ...,
  show_labels = "hidden",
  table_names = vars,
  .stats = NULL,
  .formats = NULL,
  .labels = NULL,
  .indent_mods = NULL
)

Arguments

rsp

(logical)
whether each subject is a responder or not.

conf_level

(proportion)
confidence level of the interval.

correct

(flag)
apply continuity correction.

strata

(factor)
with one level per stratum and same length as rsp.

weights

(numeric or NULL)
weights for each level of the strata. If NULL, they are estimated using the iterative algorithm proposed in Yan and Su (2010) that minimizes the weighted squared length of the confidence interval.

max_iterations

(count)
maximum number of iterations for the iterative procedure used to find estimates of optimal weights.

df

(logical or data.frame)
if only a logical vector is used, it indicates whether each subject is a responder or not. TRUE represents a successful outcome. If a data.frame is provided, also the strata variable names must be provided in variables as a list element with the strata strings. In the case of data.frame, the logical vector of responses must be indicated as a variable name in .var.

.var

(string)
single variable name that is passed by rtables when requested by a statistics function.

method

(string)
the method used to construct the confidence interval for proportion of successful outcomes; one of waldcc, wald, clopper-pearson, wilson, wilsonc, strat_wilson, strat_wilsonc, agresti-coull or jeffreys.

variables

(named list of string)
list of additional analysis variables.

long

(flag)
a long description is required.

lyt

(layout)
input layout where analyses will be added to.

vars

(character)
variable names for the primary analysis variable to be iterated over.

...

other arguments are ultimately conveyed to s_proportion().

show_labels

label visibility: one of "default", "visible" and "hidden".

table_names

(character)
this can be customized in case that the same vars are analyzed multiple times, to avoid warnings from rtables.

.stats

(character)
statistics to select for the table.

.formats

(named character or list)
formats for the statistics.

.labels

(named character)
labels for the statistics (without indent).

.indent_mods

(named integer)
indent modifiers for the labels.

Functions

  • prop_wilson(): the Wilson interval calls stats::prop.test(). Also referred to as Wilson score interval.

  • prop_strat_wilson(): Calculates the stratified Wilson confidence interval for unequal proportions as described in Yan and Su (2010)

  • prop_clopper_pearson(): the Clopper-Pearson interval calls stats::binom.test(). Also referred to as the exact method.

  • prop_wald(): the Wald interval follows the usual textbook definition for a single proportion confidence interval using the normal approximation.

  • prop_agresti_coull(): the Agresti-Coull interval was created by Alan Agresti and Brent Coull and can be understood (for 95% CI) as adding two successes and two failures to the data, and then using the Wald formula to construct a CI.

  • prop_jeffreys(): the Jeffreys interval is an equal-tailed interval based on the non-informative Jeffreys prior for a binomial proportion.

  • s_proportion(): statistics function estimating a proportion along with its confidence interval.

  • a_proportion(): Formatted Analysis function which can be further customized by calling rtables::make_afun() on it. It is used as afun in rtables::analyze().

  • estimate_proportion(): used in a rtables pipeline.

References

  • Yan X, Su XG (2010). “Stratified Wilson and Newcombe Confidence Intervals for Multiple Binomial Proportions.” Stat. Biopharm. Res., 2(3), 329--335.

Examples

rsp <- c(
  TRUE, TRUE, TRUE, TRUE, TRUE,
  FALSE, FALSE, FALSE, FALSE, FALSE
)
prop_wilson(rsp, conf_level = 0.9)
#> [1] 0.2692718 0.7307282

# Stratified Wilson confidence interval with unequal probabilities

set.seed(1)
rsp <- sample(c(TRUE, FALSE), 100, TRUE)
strata_data <- data.frame(
  "f1" = sample(c("a", "b"), 100, TRUE),
  "f2" = sample(c("x", "y", "z"), 100, TRUE),
  stringsAsFactors = TRUE
)
strata <- interaction(strata_data)
n_strata <- ncol(table(rsp, strata)) # Number of strata

prop_strat_wilson(
  rsp = rsp, strata = strata,
  conf_level = 0.90
)
#> $conf_int
#>     lower     upper 
#> 0.4072891 0.5647887 
#> 
#> $weights
#>       a.x       b.x       a.y       b.y       a.z       b.z 
#> 0.2074199 0.1776464 0.1915610 0.1604678 0.1351096 0.1277952 
#> 

# Not automatic setting of weights
prop_strat_wilson(
  rsp = rsp, strata = strata,
  weights = rep(1 / n_strata, n_strata),
  conf_level = 0.90
)
#> $conf_int
#>     lower     upper 
#> 0.4190436 0.5789733 
#> 
prop_clopper_pearson(rsp, conf_level = .95)
#> [1] 0.3886442 0.5919637

prop_wald(rsp, conf_level = 0.95)
#> [1] 0.3920214 0.5879786
prop_wald(rsp, conf_level = 0.95, correct = TRUE)
#> [1] 0.3870214 0.5929786

prop_agresti_coull(rsp, conf_level = 0.95)
#> [1] 0.3942193 0.5865206

prop_jeffreys(rsp, conf_level = 0.95)
#> [1] 0.3934779 0.5870917


# Case with only logical vector.
rsp_v <- c(1, 0, 1, 0, 1, 1, 0, 0)
s_proportion(rsp_v)
#> $n_prop
#> [1] 4.0 0.5
#> attr(,"label")
#> [1] "Responders"
#> 
#> $prop_ci
#> [1]  9.102404 90.897596
#> attr(,"label")
#> [1] "95% CI (Wald, with correction)"
#> 

# Example for Stratified Wilson CI
nex <- 100 # Number of example rows
dta <- data.frame(
  "rsp" = sample(c(TRUE, FALSE), nex, TRUE),
  "grp" = sample(c("A", "B"), nex, TRUE),
  "f1" = sample(c("a1", "a2"), nex, TRUE),
  "f2" = sample(c("x", "y", "z"), nex, TRUE),
  stringsAsFactors = TRUE
)

s_proportion(
  df = dta,
  .var = "rsp",
  variables = list(strata = c("f1", "f2")),
  conf_level = 0.90,
  method = "strat_wilson"
)
#> $n_prop
#> [1] 56.00  0.56
#> attr(,"label")
#> [1] "Responders"
#> 
#> $prop_ci
#>    lower    upper 
#> 49.71483 65.08445 
#> attr(,"label")
#> [1] "90% CI (Stratified Wilson, without correction)"
#> 

dta_test <- data.frame(
  USUBJID = paste0("S", 1:12),
  ARM     = rep(LETTERS[1:3], each = 4),
  AVAL    = c(A = c(1, 1, 1, 1), B = c(0, 0, 1, 1), C = c(0, 0, 0, 0))
)

basic_table() %>%
  split_cols_by("ARM") %>%
  estimate_proportion(vars = "AVAL") %>%
  build_table(df = dta_test)
#>                                        A              B              C     
#> ———————————————————————————————————————————————————————————————————————————
#> Responders                        4 (100.0%)      2 (50.0%)      0 (0.0%)  
#> 95% CI (Wald, with correction)   (87.5, 100.0)   (0.0, 100.0)   (0.0, 12.5)