Tests for a Single Proportion
A test for one proportion asks whether a population proportion, \(p\), differs from some hypothesized value, \(p_0\).
\[H_0: p = p_0 \qquad \text{vs.} \qquad H_A: p \ne p_0 \ \ (\text{or} \ <, \ >)\]
Conditions for the z-test: \(n p_0 \ge 5\) and \(n(1-p_0) \ge 5\). When this condition fails (usually because \(p_0\) is very small or very close to 1, or \(n\) is small), we use an exact test instead.
Motivating example: A clinic wants to check how well their flu vaccine is performing this season. Historically, around 8% of unvaccinated adults get the flu each season. Out of 150 vaccinated patients tracked this season, 9 developed the flu. Does the vaccinated group’s flu rate differ from the 8% historical (unvaccinated) benchmark?
Checking Conditions
Fill in the code below to check whether the conditions for a z-test are satisfied, using \(n = 150\) and \(p_0 = 0.08\).
# checking conditions for the z-test
150 * 0.08
* (1 - )
# checking conditions for the z-test
150 * 0.08
150 * (1 - 0.08)[1] 12 [1] 138
Both values are well above 5, so we can proceed with the z-test.
Z-test Using prop.test()
Fill in the code below to run a two-sided z-test for one proportion, testing whether the true infection rate differs from 0.08. We’ll use correct = F to turn off the continuity correction, so our results line up more closely with the by-hand calculations below.
# z-test for one proportion
prop.test(x = , n = , p = , correct = F)
# z-test for one proportion
prop.test(x = 9, n = 150, p = 0.08, correct = F)[1] p-value = 0.3666
With a p-value of about 0.37, we’d fail to reject \(H_0\)—we don’t have strong evidence that the vaccinated group’s flu rate differs from 8%. (Not every example needs to come out “significant”—this is a perfectly reasonable, informative result!)
By Hand
Fill in the code below to calculate the observed proportion, the z test statistic, and the two-sided p-value by hand.
# null value and sample size
p0 = 0.08
n =
# observed proportion
phat = / 150
# z test statistic
z_obs = (phat - ) / sqrt(p0 * (1 - p0) / )
z_obs
# two-sided p-value
2 * pnorm(abs(z_obs), lower.tail = F)
# null value and sample size
p0 = 0.08
n = 150
# observed proportion
phat = 9 / 150
# z test statistic
z_obs = (phat - p0) / sqrt(p0 * (1 - p0) / n)
z_obs[1] -0.9028939
# two-sided p-value
2 * pnorm(abs(z_obs), lower.tail = F)[1] 0.3665822
This matches the output from prop.test() above.
Confidence Interval (Wald)
Fill in the code below to calculate a 95% Wald confidence interval for the true proportion.
# 95% Wald confidence interval
phat + c(-1, 1) * qnorm() * sqrt(phat * (1 - phat) / n)
# 95% Wald confidence interval
phat + c(-1, 1) * qnorm(0.975) * sqrt(phat * (1 - phat) / n)[1] 0.02199489 0.09800511
This interval (about 0.022 to 0.098) contains 0.08, consistent with our earlier conclusion that we don’t have strong evidence the true rate differs from 8%.
Now calculate the same type of interval using the plus-4 method instead, which tends to perform better (especially with small samples or proportions near 0 or 1). This method simply adds 2 “successes” and 2 “failures” (4 observations total) before calculating a standard Wald interval.
# plus-4 method
ptilde = ( + ) / ( + )
ptilde + c(-1, 1) * qnorm(0.975) * sqrt(ptilde * (1 - ptilde) / (150 + 4))
# plus-4 method
ptilde = (9 + 2) / (150 + 4)
ptilde + c(-1, 1) * qnorm(0.975) * sqrt(ptilde * (1 - ptilde) / (150 + 4))[1] 0.03075321 0.11210393
The plus-4 interval shifts slightly toward 0.5 compared to the Wald interval, and tends to have better coverage properties in practice (especially for smaller samples or extreme proportions).
Why might adding 2 “successes” and 2 “failures” before calculating the interval help when the true proportion is close to 0 or 1, or the sample size is small?
When Conditions Fail: The Exact Binomial Test
Suppose instead we’re investigating a rare adverse reaction to the vaccine. The historical rate of this reaction is only 1%. Out of the same 150 patients, 2 experienced this reaction.
Fill in the code below to check the z-test conditions for this scenario (\(n = 150\), \(p_0 = 0.01\)).
# checking conditions
150 * 0.01
* (1 - )
# checking conditions
150 * 0.01
150 * (1 - 0.01)[1] 1.5 [1] 148.5
\(n p_0 = 1.5\) is well below 5—the z-test conditions are violated! We need an exact test instead.
Fill in the code below to run the exact binomial test, testing whether the true adverse reaction rate is greater than 0.01.
# exact binomial test
binom.test(x = , n = , p = , alternative = )
# exact binomial test
binom.test(x = 2, n = 150, p = 0.01, alternative = "greater")[1] p-value = 0.443
With a p-value of about 0.44, we have no evidence that the true adverse reaction rate exceeds the historical 1% baseline.
In your own words, why can’t we just use the z-test here even though we have a reasonably large sample size (n = 150)? What is it about this specific null proportion that causes the problem?