Confidence interval for a single binomial proportion
proportion_ci.RdVectorized computation of a two-sided confidence interval for a single
proportion x / n, using one of five standard methods. All methods are
computed in closed form or via qbeta (no per-row
binom.test loop), so the function scales to a full count table's worth
of cells at once.
Usage
proportion_ci(
x,
n,
method = c("clopper_pearson", "wilson", "wald", "agresti_coull", "jeffreys"),
level = 0.95
)Value
A data.table with two columns, lower and upper,
giving the interval bounds as proportions in [0, 1] (multiply by 100
for the percentage scale).
Details
The methods and their references:
clopper_pearsonExact interval based on the beta distribution. Matches
stats::binom.test()and SASPROC FREQ ... EXACT(the clinical convention). This is the default.wilsonWilson score interval without continuity correction. Matches
stats::prop.test(correct = FALSE).waldNormal-approximation ("simple asymptotic") interval, clamped to
[0, 1].agresti_coullAdds
z^2/2pseudo-successes and pseudo-failures, then applies the Wald interval to the adjusted counts.jeffreysBayesian interval using the Jeffreys
Beta(0.5, 0.5)prior, with the standard boundary adjustments atx == 0andx == n.
Edge cases: when n == 0 (or either input is NA) both bounds are
NA; when x == 0 the lower bound is exactly 0; when
x == n the upper bound is exactly 1.