Compute the required sample size for estimating a population proportion with a specified margin of error or coefficient of variation.
Usage
n_prop(p, ...)
# Default S3 method
n_prop(
p,
...,
moe = NULL,
cv = NULL,
rmoe = NULL,
alpha = 0.05,
N = Inf,
deff = 1,
resp_rate = 1,
method = c("wald", "wilson", "logodds", "beta"),
df = NULL,
min_cases = NULL,
plan = NULL
)
# S3 method for class 'svyplan_prec'
n_prop(p, ..., moe = NULL, cv = NULL, rmoe = NULL)Arguments
- p
For the default method: expected proportion, in (0, 1). For
svyplan_precobjects: a precision result fromprec_prop().- ...
Additional arguments passed to methods. Unused arguments are rejected.
- moe
Desired margin of error, the half-width of the confidence interval on the proportion scale. For example,
moe = 0.05means the 95 percent CI should be no wider than +/- 5 percentage points. Specify exactly one ofmoe,cv, orrmoe.- cv
Target coefficient of variation (relative standard error). For example,
cv = 0.10means the standard error should be at most 10 percent of the estimate. Usecvwhen you want precision to scale with the estimate (common in economic surveys). Usemoewhen you want a fixed absolute precision (common in health/DHS surveys). Specify exactly one ofmoe,cv, orrmoe.- rmoe
Target margin of error relative to
p, sormoe = 0.12asks for a 95 percent interval whose half-width is 12 percent of the proportion. This is how MICS and DHS state a precision requirement. It ismoe / p, and therefore fixes the same intervalmoedoes while scaling with the estimate the waycvdoes; see the precision quantities section ofprec_prop(). Specify exactly one ofmoe,cv, orrmoe.- alpha
Significance level, default 0.05.
- N
Population size.
Inf(default) means no finite population correction.- deff
Design effect multiplier (> 0). Accounts for the loss of precision from a complex design (clustering, unequal weights) compared to simple random sampling. A DEFF of 1.5 means 50 percent more interviews are needed for the same precision. Estimate from a previous survey, use
design_effect()to compute it, or apply a rule of thumb (1.5–2.0 for typical cluster designs). Values < 1 are valid for efficient designs (e.g., stratified sampling with Neyman allocation).- resp_rate
Expected response rate, in (0, 1]. Default 1 (no adjustment). The required sample size is inflated by
1 / resp_rate. Estimate from response rates observed in similar surveys in the same population.- method
One of
"wald"(default),"wilson","logodds", or"beta".- df
Degrees of freedom of the variance estimator the planned design will have, typically sampled PSUs minus strata, and available from
design_df()where the plan is in hand. It widens the interval for a variance estimated from few clusters.NULL(default) applies no adjustment, treating the variance as known. See Details, including for what an unsetdfmeans undermethod = "beta", where the reference is the value a simple random sample would have rather than infinity.- min_cases
Minimum expected number of positive cases the sample must yield, an alternative constraint to precision for a rare outcome. The returned
nsatisfies both.NULL(default) sizes on precision alone. See Details.- plan
Optional
svyplan()object providing design defaults.
Value
A svyplan_n object with type = "proportion":
nRequired sample size, continuous and gross. It already carries
deffand the1 / resp_rateinflation, so it counts the units to release, not the completed interviews.$nandas.double()keep the unrounded value, which is what makes the round trip throughprec_prop()exact;print()andas.integer()round it up to the whole units you would field. Take the field figure fromas.integer()rather than from$n.se,moe,cv,rmoePrecision the design achieves at that
n, the same valuesprec_prop()reports for the same inputs.seis the sampling standard error andcvisse / p, so both are the same under all four methods.moeis half the length of the interval the chosenmethodbuilds, which equalsqnorm(1 - alpha / 2) * seunder"wald"alone; for the three asymmetric methods it is not an offset from either limit, so read the limits withconfint().rmoeismoe / p, so a target stated as a relative margin of error reads back in the units it was stated in.methodThe interval method used.
expected_casesPositive cases the design expects to yield,
n * resp_rate * p.bindingWhich constraint set the size,
"precision"or"min_cases".NULLwhen nomin_caseswas given, there being nothing for precision to bind against.paramsThe validated inputs (
p,alpha,N,deff,resp_rate,df,min_caseswhen given, and whichever ofmoe,cv, orrmoewas the target).predict(),confint()and theprec_prop()round trip read the design back from here.
Details
Four confidence interval methods are available:
Wald (
"wald"): Standard normal approximation (Cochran, 1977, Ch. 3). Supportsmoe,rmoe, andcvtargets, with optional finite population correction.Wilson (
"wilson"): Wilson (1927) score interval.moeandrmoetargets only, with optional finite population correction.Log-odds (
"logodds"): Log-odds (logit) transform interval.moeandrmoetargets only, with optional finite population correction.Beta (
"beta"): Korn-Graubard (1998) interval, the Clopper-Pearson limits evaluated at the effective sample size, optionally widened for the degrees of freedom of the variance estimator viadf.moeandrmoetargets only, with optional finite population correction. This is the methodsurvey::svyciprop(method = "beta")reports, and the two agree exactly on a design where they see the same effective size.
All four read one variance, \(\mathrm{deff}\,\frac{N}{N-1}\,p(1-p)(1/n-1/N)\). They differ only in the interval built around it, so they agree closely whenever the margin of error is small and diverge only where the normal approximation itself is doubtful. In practice the choice matters for a rare or near-universal outcome and is immaterial otherwise; see the section below.
The design effect and the finite population correction enter every method
through the effective sample size
\(n_\mathrm{eff}=n_\mathrm{net}/(\mathrm{deff}\cdot\mathrm{fpc})\), the
size at which an infinite-population simple random sample would carry the
same variance. For Wald this reproduces the usual closed form exactly; for
the other three it is the approximation that keeps deff and N acting
on the interval as they act on the variance. A census therefore yields a
zero margin of error under all four.
The Wilson, log-odds, and beta intervals are not symmetric about p, so
the reported moe is half the interval's length rather than an offset
either limit sits at. For the limits themselves, use confint(), which
returns the interval the chosen method actually produces. The gap matters
most for the beta method at a rare outcome, where the upper arm can be
several times the lower one.
Choosing a method
Reach for Wald unless you have a reason not to. It is the only method
that also solves a cv target, and it is what every survey text and
every sample size table reports. moe and rmoe targets are available
under all four.
How much the choice costs depends on the sample size, and the four
methods converge on each other slowly. Over p from 0.1 to 0.9, the
widest and narrowest margins of error differ by 8.2 percent at
n = 100, 3.8 percent at 500, 2.7 percent at 1000, and 1.2 percent at
5000. Below a few thousand the choice is worth a moment; at survey scale
it usually is not.
The case that decides it is a rare or near-universal outcome. As p
approaches 0 or 1 the Wald interval loses coverage and can extend past 0
or 1. confint() truncates it at the boundary when it does, and moe
is then no longer half the reported interval's length. Wilson and
log-odds lie strictly inside \((0, 1)\) and beta inside \([0, 1]\),
all three by construction, so none of them needs that truncation.
Planning a survey for a 2 percent prevalence is the case that justifies
method = "wilson": the score interval keeps its nominal coverage far
better there.
Log-odds is the narrower case again: it respects the parameter space like Wilson but keeps the estimate at the center of the interval on the logit scale, which matters when the plan will be reported as an odds ratio or fed into a logistic model. If you are not doing either, prefer Wilson.
Reach for "beta" when the expected number of positive cases is small in
absolute terms, not merely when p is small: a few dozen cases or fewer,
which is the regime Korn and Graubard wrote for and the one where the
normality of the estimated proportion breaks down however large the
sample is. Rare-outcome domain estimates in a clustered survey are the
standard case. Beta is the widest of the four over most of the range,
and so the most demanding to plan for, but not everywhere: for a very
rare outcome at a small sample, log-odds is wider still. Nor is it the
only method that answers to a variance estimated from few clusters. All
four read df through the same t quantile and widen by it, so df
alone is not a reason to choose beta.
A note on where the methods bind. For sizing, the choice is usually
secondary: over the range measured above the four sizes differ by a few
percent, less than the uncertainty in the assumed p, deff, and
response rate. For assessing an achieved design the choice can dominate,
because that is where small realized samples of rare outcomes appear. If
you are unsure, plan with Wald and report with "beta".
A minimum expected number of cases
For a rare outcome in a small domain the constraint that actually binds
is often not a margin of error but a count: an indicator nobody will
publish on fewer than 30 observed cases, a subgroup analysis that needs
enough events to fit anything to. min_cases states that requirement
directly. The size it demands is
$$n_\mathrm{cases} = \mathrm{min\_cases} / (p \cdot
\mathrm{resp\_rate}),$$
and the result is the larger of that and the size precision asks for, so
both constraints hold. binding says which one decided, and
expected_cases reports the count either way.
deff does not enter this size, and that is deliberate rather than an
omission. A design effect describes how precisely the proportion is
estimated; the number of positive cases that turn up in a sample of
\(n\) is a property of the sample size and the prevalence alone. The
response rate does enter, because the cases are counted among
respondents and the returned n is gross, on the same footing as every
other size the package reports.
expected_cases is reported on every proportion result, with or without
min_cases, since it is the number the method guidance above turns on:
"beta" earns its place when the expected count of positive cases is
small in absolute terms, and that count was previously left for the
reader to work out.
Finite population correction
Setting N to a finite value reduces the required sample size when
the sampling fraction (n/N) is non-negligible. As a rule of thumb,
FPC has little effect when n/N < 5 percent. The Wald FPC uses the
Cochran (1977, Ch. 3) form with an N/(N-1) factor to account for
the Bernoulli finite-population variance. This differs from
n_mean(), where no N/(N-1) adjustment is needed because the
variance is already defined on N-1 degrees of freedom.
Degrees of freedom
The interval quantile is the normal one by default, which treats the
variance as known. That is standard for survey sampling where the sample
is large enough for the central limit theorem to apply, and it is what a
df of NULL selects.
Supplying df says the variance will be estimated from a design with
that many degrees of freedom, and switches the quantile to
\(t_{1-\alpha/2}(\mathrm{df})\). It is opt-in rather than derived: in
n_cluster() the df depends on the PSU count being solved for, so an
automatic version would need a fixed point. Read it off a plan you
already have with design_df(), or pass a count directly.
The three interval methods and the Korn-Graubard one reach the same
widening by different routes. "wald", "wilson" and "logodds"
substitute the quantile in the half-width. "beta" instead scales the
effective sample size by the squared ratio of the two t quantiles
(Korn and Graubard, 1998, eq. 2.2), which is the same widening expressed
on the sample size rather than on the interval. Both hold
\(\mathrm{moe} = q \cdot \mathrm{se}\) with the one quantile, so a
margin of error and the standard error reported beside it always agree.
The two routes differ in what an unset df means, because they differ
in what they measure it against. For the three substituting methods
NULL and Inf are the same statement, the normal quantile. For
"beta" the reference is the value a simple random sample of the same
size would have, so NULL is equivalent to df = n - 1, and df = Inf
claims more degrees of freedom than an SRS has and narrows the interval
accordingly.
The coefficient of variation is not uniformly invariant to df. Only
"wald" and the mean engine build se without a quantile in it, so
only their cv is unchanged; "wilson", "logodds" and "beta" build
the half-width first and read se back out of it, so their cv moves
with the quantile.
df is deliberately absent from the power functions. There the
quantile is a normal deviate for an alternative rather than an interval
half-width, and a t-based power calculation is a different procedure,
not a substituted quantile.
When called on a svyplan_prec object, parameters are extracted from the
stored result. Any argument of the default method (e.g. method, deff,
N) can be overridden through .... Unknown argument names are an
error. Passing a different method evaluates the stored precision
target under that formula. The round-trip will not be exact because the
precision was computed under the original method.
References
Cochran, W. G. (1977). Sampling Techniques (3rd ed.). Wiley.
Wilson, E. B. (1927). Probable inference, the law of succession, and statistical inference. Journal of the American Statistical Association, 22(158), 209–212.
Brown, L. D., Cai, T. T. and DasGupta, A. (2001). Interval estimation for a binomial proportion. Statistical Science, 16(2), 101–133. The coverage comparison that motivates preferring the score interval for a rare or near-universal outcome.
Korn, E. L. and Graubard, B. I. (1998). Confidence intervals for proportions with small expected number of positive counts estimated from survey data. Survey Methodology, 24(2), 193–201.
Clopper, C. J. and Pearson, E. S. (1934). The use of confidence or fiducial limits illustrated in the case of the binomial. Biometrika, 26(4), 404–413.
See also
n_mean() for continuous variables, n_cluster() for
multistage designs, n_multi() for multiple indicators,
prec_prop() for the inverse.
Other sample size functions:
n_alloc(),
n_change(),
n_cluster(),
n_mean(),
n_multi(),
n_multi_cluster(),
n_panel(),
n_pooled(),
n_twophase()
Examples
# Wald, absolute margin of error
n_prop(p = 0.3, moe = 0.05)
#> Sample size for proportion (wald)
#> n = 323 (p = 0.30, moe = 0.050, deff = 1)
#> expected cases = 96.8
# Wald, target CV with finite population
n_prop(p = 0.5, cv = 0.10, N = 10000)
#> Sample size for proportion (wald)
#> n = 100 (p = 0.50, cv = 0.100, deff = 1)
#> expected cases = 49.5
# Wilson score interval
n_prop(p = 0.1, moe = 0.03, method = "wilson")
#> Sample size for proportion (wilson)
#> n = 388 (p = 0.10, moe = 0.030, deff = 1)
#> expected cases = 38.7
# Korn-Graubard interval for a rare outcome, and the asymmetric limits
# it implies
rare <- n_prop(p = 0.02, moe = 0.01, method = "beta")
confint(rare)
#> 2.5 % 97.5 %
#> 0.01173814 0.03173814
# The same, allowing for a variance estimated from 30 PSUs in 5 strata
n_prop(p = 0.02, moe = 0.01, method = "beta", df = 25)
#> Sample size for proportion (beta)
#> n = 949 (p = 0.02, moe = 0.010, deff = 1)
#> expected cases = 19.0
# With design effect and response rate
n_prop(p = 0.3, moe = 0.05, deff = 1.5, resp_rate = 0.8)
#> Sample size for proportion (wald)
#> n = 606 gross (net: 485) (p = 0.30, moe = 0.050, deff = 1.50, resp_rate = 0.80)
#> expected cases = 145.2
# At least 30 expected cases of a rare outcome, whatever precision asks
rare <- n_prop(p = 0.02, moe = 0.02, min_cases = 30)
rare$binding
#> [1] "min_cases"
rare$expected_cases
#> [1] 30
# MICS/DHS-style relative margin of error: 12 percent of the proportion
n_prop(p = 0.2, rmoe = 0.12, deff = 1.5, resp_rate = 0.9)
#> Sample size for proportion (wald)
#> n = 1779 gross (net: 1601) (p = 0.20, rmoe = 0.120, deff = 1.50, resp_rate = 0.90)
#> expected cases = 320.1