Functions for Calculating Proportion Confidence Intervals
Source:R/proportion_ci.R
proportion_ci.Rd
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
)
Arguments
- x
vector of a binary values, i.e. a logical vector, or numeric with values
c(0, 1)
- conf.level
(
numeric
)
a scalar in(0, 1)
indicating the confidence level. Default is0.95
- correct
(
flag
)
include the continuity correction. For further information, see for examplestats::prop.test()
.- strata
(
factor
)
variable with one level per stratum and same length asx
.- weights
(
numeric
orNULL
)
weights for each level of the strata. IfNULL
, they are estimated using the iterative algorithm 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.
Functions
proportion_ci_wald()
: Calculates the Wald interval by following the usual textbook definition for a single proportion confidence interval using the normal approximation.proportion_ci_wilson()
: Calculates the Wilson interval by callingstats::prop.test()
. Also referred to as Wilson score interval.proportion_ci_clopper_pearson()
: Calculates the Clopper-Pearson interval by callingstats::binom.test()
. Also referred to as theexact
method.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.proportion_ci_jeffreys()
: Calculates the Jeffreys interval, an equal-tailed interval based on the non-informative Jeffreys prior for a binomial proportion.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).
Examples
x <- c(
TRUE, TRUE, TRUE, TRUE, TRUE,
FALSE, FALSE, FALSE, FALSE, FALSE
)
proportion_ci_wald(x, conf.level = 0.9)
#> $N
#> [1] 10
#>
#> $estimate
#> [1] 0.5
#>
#> $conf.low
#> [1] 0.2399258
#>
#> $conf.high
#> [1] 0.7600742
#>
#> $conf.level
#> [1] 0.9
#>
#> $method
#> Wald Confidence Interval without continuity correction
#>
proportion_ci_wilson(x, correct = TRUE)
#> $N
#> [1] 10
#>
#> $conf.level
#> [1] 0.95
#>
#> $estimate
#> p
#> 0.5
#>
#> $statistic
#> X-squared
#> 0
#>
#> $p.value
#> [1] 1
#>
#> $parameter
#> df
#> 1
#>
#> $conf.low
#> [1] 0.2365931
#>
#> $conf.high
#> [1] 0.7634069
#>
#> $method
#> Wilson Confidence Interval with continuity correction
#>
#> $alternative
#> [1] "two.sided"
#>
proportion_ci_clopper_pearson(x)
#> $N
#> [1] 10
#>
#> $conf.level
#> [1] 0.95
#>
#> $estimate
#> probability of success
#> 0.5
#>
#> $statistic
#> number of successes
#> 5
#>
#> $p.value
#> [1] 1
#>
#> $parameter
#> number of trials
#> 10
#>
#> $conf.low
#> [1] 0.187086
#>
#> $conf.high
#> [1] 0.812914
#>
#> $method
#> [1] "Clopper-Pearson Confidence Interval"
#>
#> $alternative
#> [1] "two.sided"
#>
proportion_ci_agresti_coull(x)
#> $N
#> [1] 10
#>
#> $estimate
#> [1] 0.5
#>
#> $conf.low
#> [1] 0.2365931
#>
#> $conf.high
#> [1] 0.7634069
#>
#> $conf.level
#> [1] 0.95
#>
#> $method
#> [1] "Agresti-Coull Confidence Interval"
#>
proportion_ci_jeffreys(x)
#> $N
#> [1] 10
#>
#> $estimate
#> [1] 0.5
#>
#> $conf.low
#> [1] 0.2235287
#>
#> $conf.high
#> [1] 0.7764713
#>
#> $conf.level
#> [1] 0.95
#>
#> $method
#> Jeffreys Interval
#>
# 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
)
#> $N
#> [1] 100
#>
#> $estimate
#> [1] 0.49
#>
#> $conf.low
#> [1] 0.4072891
#>
#> $conf.high
#> [1] 0.5647887
#>
#> $conf.level
#> [1] 0.9
#>
#> $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
#>
#> $method
#> Stratified Wilson Confidence Interval without continuity correction
#>
# Not automatic setting of weights
proportion_ci_strat_wilson(
x = rsp, strata = strata,
weights = rep(1 / n_strata, n_strata),
conf.level = 0.90
)
#> $N
#> [1] 100
#>
#> $estimate
#> [1] 0.49
#>
#> $conf.low
#> [1] 0.4190436
#>
#> $conf.high
#> [1] 0.5789733
#>
#> $conf.level
#> [1] 0.9
#>
#> $method
#> Stratified Wilson Confidence Interval without continuity correction
#>