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").

ci_proportion(
  x,
  n = NULL,
  probs = c(0.025, 0.975),
  type = c("Clopper-Pearson", "Agresti-Coull", "Wilson", "bootstrap"),
  boot_type = c("bca", "perc", "stud", "norm", "basic"),
  R = 9999L,
  seed = NULL,
  ...
)

Arguments

x

A numeric vector with one value (0/1) per observation, or the number of successes.

n

The sample size. Only needed if x is a vector of length 1.

probs

Lower and upper probabilities, by default c(0.025, 0.975).

type

Type of CI. One of "Clopper-Pearson" (the default), "Agresti–Coull", "Wilson", "bootstrap".

boot_type

Type of bootstrap CI. Only used for type = "bootstrap".

R

The number of bootstrap resamples. Only used for type = "bootstrap".

seed

An integer random seed. Only used for type = "bootstrap".

...

Further arguments passed to boot::boot().

Value

An object of class "cint", see ci_mean() for details.

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").

References

  1. Clopper, C. and Pearson, E. S. (1934). The use of confidence or fiducial limits illustrated in the case of the binomial. Biometrika. 26 (4).

  2. Wilson, E. B. (1927). Probable inference, the law of succession, and statistical inference. Journal of the American Statistical Association, 22 (158).

  3. Agresti, A. and Coull, B. A. (1998). Approximate is better than 'exact' for interval estimation of binomial proportions. The American Statistician, 52 (2).

Examples

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 
#>