This function calculates CIs for a population proportion. By default,
"Clopper-Pearson" CIs are calculated (via stats::binom.test()
).
Further possibilities are "Wilson" (without continuity correction),
"Agresti-Coull" (using normal quantile instead of +2 correction),
and "bootstrap" (by default "bca").
A numeric vector with one value (0/1) per observation, or the number of successes.
The sample size. Only needed if x
is a vector of length 1.
Lower and upper probabilities, by default c(0.025, 0.975)
.
Type of CI. One of "Clopper-Pearson" (the default), "Agresti–Coull", "Wilson", "bootstrap".
Type of bootstrap CI. Only used for type = "bootstrap"
.
The number of bootstrap resamples. Only used for type = "bootstrap"
.
An integer random seed. Only used for type = "bootstrap"
.
Further arguments passed to boot::boot()
.
An object of class "cint", see ci_mean()
for details.
Note that we use the formulas for the Wilson and Agresti-Coull intervals in
https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval.
They agree with binom::binom.confint(x, n, method = "ac"/"wilson")
.
Clopper, C. and Pearson, E. S. (1934). The use of confidence or fiducial limits illustrated in the case of the binomial. Biometrika. 26 (4).
Wilson, E. B. (1927). Probable inference, the law of succession, and statistical inference. Journal of the American Statistical Association, 22 (158).
Agresti, A. and Coull, B. A. (1998). Approximate is better than 'exact' for interval estimation of binomial proportions. The American Statistician, 52 (2).
x <- rep(0:1, times = c(50, 100))
ci_proportion(x)
#>
#> Two-sided 95% Clopper-Pearson confidence interval for the true
#> proportion
#>
#> Sample estimate: 0.6666667
#> Confidence interval:
#> 2.5% 97.5%
#> 0.5851570 0.7414436
#>
ci_proportion(x, type = "Wilson")
#>
#> Two-sided 95% Wilson confidence interval for the true proportion
#>
#> Sample estimate: 0.6666667
#> Confidence interval:
#> 2.5% 97.5%
#> 0.5878976 0.7371124
#>
ci_proportion(x, type = "Agresti-Coull")
#>
#> Two-sided 95% Agresti-Coull confidence interval for the true proportion
#>
#> Sample estimate: 0.6666667
#> Confidence interval:
#> 2.5% 97.5%
#> 0.5877845 0.7372254
#>