proportion_ci {cardx} | R Documentation |
Functions for Calculating Proportion Confidence Intervals
Description
Functions to calculate different proportion confidence intervals for use in ard_proportion()
.
Usage
proportion_ci_wald(x, conf.level = 0.95, correct = FALSE)
proportion_ci_wilson(x, conf.level = 0.95, correct = FALSE)
proportion_ci_clopper_pearson(x, conf.level = 0.95)
proportion_ci_agresti_coull(x, conf.level = 0.95)
proportion_ci_jeffreys(x, conf.level = 0.95)
proportion_ci_strat_wilson(
x,
strata,
weights = NULL,
conf.level = 0.95,
max.iterations = 10L,
correct = FALSE
)
is_binary(x)
Arguments
x |
vector of a binary values, i.e. a logical vector, or numeric with values |
conf.level |
( |
correct |
( |
strata |
( |
weights |
( |
max.iterations |
( |
Value
Confidence interval of a proportion.
Functions
-
proportion_ci_wald()
: Calculates the Wald interval by following the usual textbook definition for a single proportion confidence interval using the normal approximation.\hat{p} \pm z_{\alpha/2} \sqrt{\frac{\hat{p}(1 - \hat{p})}{n}}
-
proportion_ci_wilson()
: Calculates the Wilson interval by callingstats::prop.test()
. Also referred to as Wilson score interval.\frac{\hat{p} + \frac{z^2_{\alpha/2}}{2n} \pm z_{\alpha/2} \sqrt{\frac{\hat{p}(1 - \hat{p})}{n} + \frac{z^2_{\alpha/2}}{4n^2}}}{1 + \frac{z^2_{\alpha/2}}{n}}
-
proportion_ci_clopper_pearson()
: Calculates the Clopper-Pearson interval by callingstats::binom.test()
. Also referred to as theexact
method.\left( \frac{k}{n} \pm z_{\alpha/2} \sqrt{\frac{\frac{k}{n}(1-\frac{k}{n})}{n} + \frac{z^2_{\alpha/2}}{4n^2}} \right) / \left( 1 + \frac{z^2_{\alpha/2}}{n} \right)
-
proportion_ci_agresti_coull()
: Calculates theAgresti-Coull
interval (created byAlan Agresti
andBrent Coull
) by (for 95% CI) adding two successes and two failures to the data and then using the Wald formula to construct a CI.\left( \frac{\tilde{p} + z^2_{\alpha/2}/2}{n + z^2_{\alpha/2}} \pm z_{\alpha/2} \sqrt{\frac{\tilde{p}(1 - \tilde{p})}{n} + \frac{z^2_{\alpha/2}}{4n^2}} \right)
-
proportion_ci_jeffreys()
: Calculates the Jeffreys interval, an equal-tailed interval based on the non-informative Jeffreys prior for a binomial proportion.\left( \text{Beta}\left(\frac{k}{2} + \frac{1}{2}, \frac{n - k}{2} + \frac{1}{2}\right)_\alpha, \text{Beta}\left(\frac{k}{2} + \frac{1}{2}, \frac{n - k}{2} + \frac{1}{2}\right)_{1-\alpha} \right)
-
proportion_ci_strat_wilson()
: Calculates the stratified Wilson confidence interval for unequal proportions as described in Xin YA, Su XG. Stratified Wilson and Newcombe confidence intervals for multiple binomial proportions. Statistics in Biopharmaceutical Research. 2010;2(3).\frac{\hat{p}_j + \frac{z^2_{\alpha/2}}{2n_j} \pm z_{\alpha/2} \sqrt{\frac{\hat{p}_j(1 - \hat{p}_j)}{n_j} + \frac{z^2_{\alpha/2}}{4n_j^2}}}{1 + \frac{z^2_{\alpha/2}}{n_j}}
-
is_binary()
: Helper to determine if vector is binary (logical or 0/1)
Examples
x <- c(
TRUE, TRUE, TRUE, TRUE, TRUE,
FALSE, FALSE, FALSE, FALSE, FALSE
)
proportion_ci_wald(x, conf.level = 0.9)
proportion_ci_wilson(x, correct = TRUE)
proportion_ci_clopper_pearson(x)
proportion_ci_agresti_coull(x)
proportion_ci_jeffreys(x)
# 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
proportion_ci_strat_wilson(
x = rsp, strata = strata,
conf.level = 0.90
)
# Not automatic setting of weights
proportion_ci_strat_wilson(
x = rsp, strata = strata,
weights = rep(1 / n_strata, n_strata),
conf.level = 0.90
)