For independent samples from two populations, the pooled statistic $z = (\hat{p}_1 - \hat{p}_2)/\sqrt{\hat{p}(1-\hat{p})(1/n_1 + 1/n_2)}$, with $\hat{p}$ the pooled proportion, tests $H_0: p_1 = p_2$ in large samples, while the interval for $p_1 - p_2$ uses the unpooled standard error. The chi-square statistic of the $2 \times 2$ table equals $z^2$ for every table, so the two tests are one. Fisher's exact test takes over when expected counts are small. For 80 young men and 80 young women from NHANES, 26 against 9 with hypertension, $z = 3.25$, $p = 0.00115$, and the 95% interval for the difference is 0.089 to 0.336. Detecting the population difference (0.318 against 0.146) with 80% power needs about 95 per group.
In the National Health and Nutrition Examination Survey (NHANES) of 2017 to 2018, 31.8% of the 822 men aged 18 to 39 had hypertension, against 14.6% of the 874 women of the same ages. A study samples 80 young men and 80 young women. It finds 26 hypertensive men and 9 hypertensive women: 32.5% against 11.3%. This article shows that the difference in the samples is strong evidence of a difference between the populations, and how large the population difference could be; the numbers are in the paragraph above.
Comparing two proportions is the most common comparison in medical research. It is the design of nearly every trial with a yes-or-no outcome, such as recovered or not, or readmitted or not, in a treatment group and a control group. The single-proportion methods it builds on are in Confidence Intervals for a Proportion and Tests and Sample Size for a Proportion. The outcome, hypertension by the 2017 ACC/AHA definition (Ostchega et al., 2020), is described in The Normal Approximation to the Binomial.
First we derive the two-proportion $z$ test of the course text and the interval for the difference. Then we show that the chi-square test of a $2 \times 2$ table is the same test in a different form. Next come the exact alternative for small samples and three ways to measure the size of a difference. Finally we derive the sample size each group needs, solve three problems in full, and look back over the course.
The setting
The notation follows the course text. From population 1, with proportion $p_1$ having the outcome, a random sample of $n_1$ contains $x_1$ with the outcome. From population 2, independently, a sample of $n_2$ contains $x_2$. The sample proportions are
$$\hat{p}_1 = \frac{x_1}{n_1}, \qquad \hat{p}_2 = \frac{x_2}{n_2}.$$
The hypotheses are $H_0: p_1 = p_2$ (equivalently $p_1 - p_2 = 0$) and, for a two-sided test, $H_A: p_1 \ne p_2$. A Bayesian comparison of the same two hypotheses, with a prior for the proportions under each, reports the odds between them instead of a p-value; Bayes Factors and Posterior Model Probabilities computes it for the NHANES adults by age group.
The natural estimate of $p_1 - p_2$ is $\hat{p}_1 - \hat{p}_2$. Its mean is $p_1 - p_2$. Since the samples are independent, its variance is the sum of the two variances from the normal approximation article:
$$\operatorname{Var}(\hat{p}_1 - \hat{p}_2) = \frac{p_1(1-p_1)}{n_1} + \frac{p_2(1-p_2)}{n_2}.$$
For large samples each $\hat{p}_i$ is approximately normal, and so is their difference. The test and the interval both standardize the difference by an estimate of this standard deviation. They differ in which estimate they use, for the same reason the one-sample test and interval differ.
The pooled z test
Under $H_0$, the two proportions are equal to a common value $p$, and the variance becomes
$$p(1 - p)\left(\frac{1}{n_1} + \frac{1}{n_2}\right).$$
The null hypothesis does not say what $p$ is, so it must be estimated, and under $H_0$ both samples estimate it. The best estimate combines them. It is the pooled proportion,
$$\hat{p} = \frac{x_1 + x_2}{n_1 + n_2} = \frac{n_1\hat{p}_1 + n_2\hat{p}_2}{n_1 + n_2},$$
the overall proportion with the outcome when the two samples are put together. (The course text writes it $\hat{p}$ without a subscript; the subscripted $\hat{p}_1$ and $\hat{p}_2$ are the separate groups.) The test statistic is
$$z = \frac{\hat{p}_1 - \hat{p}_2}{\sqrt{\hat{p}(1-\hat{p})\left(\dfrac{1}{n_1} + \dfrac{1}{n_2}\right)}},$$
referred to the standard normal distribution, with two-sided $p$-value $2P(Z \ge |z|)$. The NIST/SEMATECH e-Handbook of Statistical Methods (§7.3.3) gives the same statistic for comparing the defect rates of two processes.
Pooling is the counterpart of the pooled variance in the two-sample $t$-test, with one difference. There, pooling is an assumption that may fail. Here, equal variances follow from the null hypothesis itself, since equal proportions imply equal Bernoulli variances.
The worked test
Back to the 80 men and 80 women:
$$\hat{p}_1 = \frac{26}{80} = 0.3250, \qquad \hat{p}_2 = \frac{9}{80} = 0.1125, \qquad \hat{p} = \frac{26 + 9}{160} = 0.2188,$$
$$\operatorname{SE}_0 = \sqrt{0.2188 \times 0.7813 \times \left(\frac{1}{80} + \frac{1}{80}\right)} = 0.06536, \qquad z = \frac{0.3250 - 0.1125}{0.06536} = 3.251,$$
and the two-sided $p$-value is 0.00115. The samples give strong evidence that hypertension is more common among young men than young women. The usual check for the normal approximation is that each group has at least 5 expected outcomes and 5 expected non-outcomes under $H_0$, that is, $n_i\hat{p}$ and $n_i(1 - \hat{p})$. Here they are 17.5 and 62.5 in each group, so the check is comfortably met. The course text states the check with each group's own proportion instead, requiring $n_1\hat{p}_1$, $n_1(1-\hat{p}_1)$, $n_2\hat{p}_2$ and $n_2(1-\hat{p}_2)$ all to exceed 5. Here those counts are 26, 54, 9 and 71.
The test says the difference is real. The interval of the next section says how large it is.
The interval for the difference
Example (the interval does not pool). The test pooled the two samples to estimate one common proportion, but an interval for $p_1 - p_2$ does not pool them. It cannot assume the proportions are equal, since its purpose is to say how far apart they are. It uses the unpooled standard error, each group's variance estimated from its own proportion:
$$\operatorname{SE} = \sqrt{\frac{\hat{p}_1(1-\hat{p}_1)}{n_1} + \frac{\hat{p}_2(1-\hat{p}_2)}{n_2}}, \qquad (\hat{p}_1 - \hat{p}_2) \pm z_{1-\alpha/2}\operatorname{SE}.$$
The two standard errors have separate jobs: pooled for the test, unpooled for the interval. It is the two-sample form of the one-sample rule, $p_0$ for a test and $\hat{p}$ for an interval. (Likelihood Ratio, Wald and Score Tests names the two: the square of the pooled statistic is the score statistic and the square of the unpooled one the Wald statistic. Its third worked problem computes both on this sample, with the likelihood ratio statistic.) For the worked samples,
$$\operatorname{SE} = \sqrt{\frac{0.3250 \times 0.6750}{80} + \frac{0.1125 \times 0.8875}{80}} = 0.06317,$$
and the 95% interval is $0.2125 \pm 1.96 \times 0.06317 = (0.089,\ 0.336)$. The population difference, 0.1711, lies inside it. The sample happened to exaggerate the difference, and the interval is wide enough to say so. With 80 per group, the data are consistent with a difference anywhere from 9 to 34 percentage points.
This is a Wald interval. It shares the weakness of the one-sample Wald interval, though in a milder form, because the errors of two estimates partly offset. The script computed its exact coverage at the true proportions, 0.3175 and 0.1465, by summing the probabilities of all pairs of counts whose interval contains 0.1711. The coverage is 0.9366 with 20 per group, 0.9448 with 40 and 0.9455 with 80. For small samples, or proportions near 0 or 1, an interval built on Wilson's method for each group is preferred, along the lines of the one-sample case.
Newcombe's interval. The standard construction of that kind is due to Newcombe (1998), in a comparison of eleven methods for this interval. Compute the Wilson interval for each group separately, $(l_1, u_1)$ for $p_1$ and $(l_2, u_2)$ for $p_2$. The interval for $p_1 - p_2$ is then
$$\left(\hat{p}_1 - \hat{p}_2 - \sqrt{(\hat{p}_1 - l_1)^2 + (u_2 - \hat{p}_2)^2},\quad \hat{p}_1 - \hat{p}_2 + \sqrt{(u_1 - \hat{p}_1)^2 + (\hat{p}_2 - l_2)^2}\right).$$
Here is the idea. The Wald interval's margin is $z\sqrt{\operatorname{SE}_1^2 + \operatorname{SE}_2^2}$, the square root of the sum of the squares of the two one-sample margins. Newcombe's interval keeps that form and replaces each one-sample Wald margin by the distance from the estimate to the relevant Wilson limit. The difference is smallest when $p_1$ is at its lower limit and $p_2$ at its upper limit, so the lower end uses $\hat{p}_1 - l_1$ and $u_2 - \hat{p}_2$, and the upper end uses the other two distances. The interval is not symmetric about $\hat{p}_1 - \hat{p}_2$, because Wilson's intervals are not symmetric about their estimates. The formula is Newcombe's proposal, cited here, and its coverage is measured below.
For the worked samples the Wilson intervals are $(0.2324,\ 0.4336)$ for the men and $(0.0603,\ 0.2002)$ for the women. The four distances are $0.3250 - 0.2324 = 0.0926$, $0.2002 - 0.1125 = 0.0877$, $0.4336 - 0.3250 = 0.1086$ and $0.1125 - 0.0603 = 0.0522$, so the interval is
$$\left(0.2125 - \sqrt{0.0926^2 + 0.0877^2},\quad 0.2125 + \sqrt{0.1086^2 + 0.0522^2}\right) = (0.085,\ 0.333).$$
It sits a little lower than the Wald interval $(0.089,\ 0.336)$. Its exact coverage at the true proportions, computed in the same way, is 0.9539 with 20 per group, 0.9495 with 40 and 0.9492 with 80, against the Wald interval's 0.9366, 0.9448 and 0.9455. The gain is largest in the smallest samples. These coverages are for this one pair of proportions, not a general comparison; Newcombe's (1998) paper is the place for that.
Unlike the one-sample case, the pooled test and the unpooled interval do not correspond exactly. Near the boundary, the test can reject while the interval just includes 0, or the reverse. For example, with 20 per group, 2 and 7 events give $z = -1.89$, so the test does not reject, while the interval, $(-0.497,\ -0.003)$, excludes 0. With 1 event in 20 and 0 in 80, $z = 2.01$ rejects, while the interval, $(-0.046,\ 0.146)$, contains 0. With equal group sizes only the first kind of disagreement can occur. The pooled variance, $2\hat{p}(1-\hat{p})/n$ with $\hat{p} = (\hat{p}_1 + \hat{p}_2)/2$, exceeds the unpooled one, $[\hat{p}_1(1-\hat{p}_1) + \hat{p}_2(1-\hat{p}_2)]/n$, by $(\hat{p}_1 - \hat{p}_2)^2/(2n)$, which is never negative. So whenever the pooled test rejects, the difference is more than 1.96 unpooled standard errors from 0 and the interval excludes 0. They agree in the worked example, where $z = 3.25$ and the interval is far from 0.
Why not test with the unpooled standard error, so that test and interval match? That test gives $z = 3.36$ here, but its actual Type I error rate runs above $\alpha$ in small samples. Computed exactly at $p_1 = p_2 = 0.2294$, it is 0.0675 with 20 per group, against 0.0507 for the pooled test. At 40 and 80 per group the two are 0.0525 against 0.0505 and 0.0517 against 0.0513. The pooled statistic keeps the stated level more closely in these cases, which is why it is the standard test.
The same test as a 2 x 2 table
Many reports present the same comparison as a table and a chi-square test. It is the same test, as this section proves. The data can be written as a contingency table with one row per group and one column per outcome:
| Hypertension | No hypertension | Total | |
|---|---|---|---|
| Men | $a = 26$ | $b = 54$ | $n_1 = 80$ |
| Women | $c = 9$ | $d = 71$ | $n_2 = 80$ |
| Total | $m_1 = 35$ | $m_2 = 125$ | $N = 160$ |
The chi-square test, which goes back to Karl Pearson, asks whether the rows and columns are independent, that is, whether the chance of hypertension is the same in both rows. If it is, the best estimate of that chance is the overall share $m_1/N$. The expected count in each cell is then its row total times its column share. For men with hypertension it is $E_{11} = n_1 m_1/N = 80 \times 35/160 = 17.5$. For men without it is $n_1 m_2/N = 80 \times 125/160 = 62.5$, and since the two groups are of equal size the women's expected counts are the same, 17.5 and 62.5. The statistic adds up the squared differences between observed and expected counts, each scaled by the expected count:
$$X^2 = \sum_{\text{cells}} \frac{(O - E)^2}{E} = \frac{(26 - 17.5)^2}{17.5} + \frac{(54 - 62.5)^2}{62.5} + \frac{(9 - 17.5)^2}{17.5} + \frac{(71 - 62.5)^2}{62.5} = 10.569.$$
It is referred to the chi-squared distribution with one degree of freedom, giving $p = 0.00115$.
Example (the chi-square statistic is the square of z). That $p$-value is exactly the $z$ test's, and $X^2 = 10.569$ is the square of the $z$ statistic: $3.2510^2 = 10.569$. The agreement is not a coincidence, and the proof shows why.
Proof that X² = z²
Here is the plan of the proof. Every cell of a $2 \times 2$ table turns out to deviate from its expected count by the same amount. That reduces $X^2$ to a short formula, and the square of $z$ reduces to the same formula.
The formal version. For the first cell,
$$a - \frac{n_1 m_1}{N} = \frac{a(a + b + c + d) - (a + b)(a + c)}{N} = \frac{ad - bc}{N}.$$
The observed and expected counts have the same row and column totals, so the deviations in a row or a column must sum to zero. The other cells therefore deviate by $-(ad - bc)/N$, $-(ad - bc)/N$ and $+(ad - bc)/N$. Hence
$$X^2 = \frac{(ad - bc)^2}{N^2}\sum_{\text{cells}}\frac{1}{E}.$$
The sum of reciprocals of the expected counts simplifies by grouping cells in the same row:
$$\sum \frac{1}{E} = \frac{N}{n_1}\left(\frac{1}{m_1} + \frac{1}{m_2}\right) + \frac{N}{n_2}\left(\frac{1}{m_1} + \frac{1}{m_2}\right) = \frac{N}{m_1 m_2}\cdot N\left(\frac{1}{n_1} + \frac{1}{n_2}\right) = \frac{N^3}{n_1 n_2 m_1 m_2},$$
using $m_1 + m_2 = N$ and $n_1 + n_2 = N$. So
$$X^2 = \frac{N(ad - bc)^2}{n_1 n_2 m_1 m_2},$$
the shortcut formula for a $2 \times 2$ table. Now the $z$ statistic. Its numerator is
$$\hat{p}_1 - \hat{p}_2 = \frac{a}{n_1} - \frac{c}{n_2} = \frac{a(c + d) - c(a + b)}{n_1 n_2} = \frac{ad - bc}{n_1 n_2},$$
and since the pooled proportion is $\hat{p} = m_1/N$, its squared denominator is
$$\frac{m_1}{N}\cdot\frac{m_2}{N}\cdot\frac{N}{n_1 n_2} = \frac{m_1 m_2}{N n_1 n_2}.$$
Dividing,
$$z^2 = \frac{(ad - bc)^2}{n_1^2 n_2^2}\cdot\frac{N n_1 n_2}{m_1 m_2} = \frac{N(ad - bc)^2}{n_1 n_2 m_1 m_2} = X^2.$$
The two statistics are equal for every $2 \times 2$ table. Their reference distributions match as well. The chi-squared distribution with one degree of freedom is by definition the distribution of the square of a standard normal variable (The Chi-Squared and t Distributions defines the chi-squared family). So $P(\chi^2_1 \ge z^2) = P(|Z| \ge |z|)$, and the two-sided $z$ test and the chi-square test give identical $p$-values. The same relation holds for means: with two groups, the $F$ statistic of one-way analysis of variance is the square of the pooled $t$ statistic.
Each form has its uses:
- the $z$ form shows the direction of the difference, and it allows a one-sided test;
- the chi-square form generalizes to tables with more rows or columns, where several proportions are compared at once (The Chi-Square Test of Independence). When those groups are ordered, the Cochran–Armitage test splits off a one-degree-of-freedom test for a trend in the proportions, which can detect a steady rise that the overall chi-square test misses.
Small samples: Yates' correction and Fisher's exact test
Both forms rest on a normal approximation, which small samples strain. Like the one-sample test, the chi-square test approximates a discrete distribution by a continuous one. Yates' continuity correction reduces each $|O - E|$ by one half before squaring,
$$X^2_{\text{Yates}} = \sum \frac{(|O - E| - 0.5)^2}{E},$$
which for the worked table gives 9.362 and $p = 0.00221$. scipy's chi2_contingency applies this correction by default for a $2 \times 2$ table; Inference for Proportions and Means shows how such library defaults change the $p$-value reported for the same data. In both tables of this article the corrected $p$-value lies close to that of the exact test described next and above the uncorrected one: 0.00221 against 0.00193 and 0.00115 here, and 0.084 against 0.080 and 0.031 in the second worked problem.
Fisher's exact test avoids the approximation altogether. It treats the row and column totals as fixed. Then, if group and outcome are independent, the count $a$ in the first cell has the hypergeometric distribution. It is the distribution of the number of hypertensive people among $n_1$ drawn without replacement from the $N$ people, of whom $m_1$ are hypertensive:
$$P(a) = \frac{\dbinom{m_1}{a}\dbinom{m_2}{n_1 - a}}{\dbinom{N}{n_1}}.$$
The NIST handbook (§7.3.3) gives this hypergeometric probability as the basis of the test.
Why the distribution is hypergeometric. Under $H_0$ both groups have the same proportion $p$, so the two counts are independent binomials, $a \sim \text{Binomial}(n_1, p)$ and $c \sim \text{Binomial}(n_2, p)$, and their total $m_1 = a + c$ is $\text{Binomial}(N, p)$. Given the total, the probability of a particular $a$ is
$$P(a \mid m_1) = \frac{\dbinom{n_1}{a}p^a(1-p)^{n_1-a}\cdot\dbinom{n_2}{m_1 - a}p^{m_1-a}(1-p)^{n_2-m_1+a}}{\dbinom{N}{m_1}p^{m_1}(1-p)^{N-m_1}} = \frac{\dbinom{n_1}{a}\dbinom{n_2}{m_1-a}}{\dbinom{N}{m_1}}.$$
The powers of $p$ and $1 - p$ cancel, so the conditional distribution does not depend on the unknown common proportion. That is what makes the test exact. (The same conditioning, applied to each stratum of a stratified study, underlies the Mantel–Haenszel test.) The result is the hypergeometric probability of choosing which $m_1$ of the $N$ people have the outcome and finding $a$ of them in the first group. It equals the form displayed above, which chooses the $n_1$ members of the first group and finds $a$ of them among the $m_1$ with the outcome: writing all the binomial coefficients as factorials, both are $n_1!\,n_2!\,m_1!\,m_2!\,/\,(N!\,a!\,b!\,c!\,d!)$.
The two-sided $p$-value, as scipy's fisher_exact computes it, adds the probabilities of all tables with the same margins that are no more likely than the one observed. For the worked table the margins are $n_1 = n_2 = 80$, $m_1 = 35$ and $m_2 = 125$, and $a$ can be anything from 0 to 35. The observed table has
$$P(a = 26) = \frac{\dbinom{35}{26}\dbinom{125}{54}}{\dbinom{160}{80}} = 0.000736.$$
The probabilities fall away quickly beyond it: 0.000184 for $a = 27$ and 0.000038 for $a = 28$. The tables with $a \ge 26$ have total probability $0.000965$. Because the two groups are of equal size the distribution is symmetric about $m_1/2 = 17.5$, so the tables no more likely than the observed one are those with $a \ge 26$ and their mirror images with $a \le 9$, and
$$p = 2 \times 0.000965 = 0.00193.$$
With 80 per group, all four methods agree that the difference is real. The $p$-value is 0.00115 for the $z$ and chi-square tests, 0.00221 with Yates' correction and 0.00193 for Fisher's test. They part ways when expected counts are small. The second worked problem gives a case: with 15 per group and expected counts of 3.5, the $z$ test gives $p = 0.031$ and Fisher's test $p = 0.080$. Fisher's test is then the safe choice. Given the margins, its Type I error rate cannot exceed $\alpha$, by the argument given for the exact binomial test in Tests and Sample Size for a Proportion, and so it cannot exceed $\alpha$ overall. Like that test, it usually falls short of $\alpha$, which costs some power.
Measuring the difference: risk difference, relative risk, odds ratio
The test asks whether the proportions differ. Three measures say by how much, and each answers a different question:
| Measure | Definition | Population | Sample |
|---|---|---|---|
| Risk difference | $p_1 - p_2$ | 0.171 | 0.213 |
| Relative risk | $p_1/p_2$ | 2.17 | 2.89 |
| Odds ratio | $\dfrac{p_1/(1-p_1)}{p_2/(1-p_2)}$ | 2.71 | 3.80 |
The difference is what this article's interval estimates. The relative risk says young men are about twice as likely as young women to have hypertension. The odds ratio is the measure that case-control studies and logistic regression produce. The difference is zero, and the two ratios are one, exactly when $p_1 = p_2$, so the test of this article tests them all. When each measure is appropriate is the subject of Relative Risk and the Odds Ratio in the probability unit, and intervals for the ratios are the subject of Intervals and Adjustment for Relative Risks and Odds Ratios. The library's Logistic Regression estimates odds ratios that adjust for other variables, and Effect Sizes and Standardized Differences treats risk differences and odds ratios as effect sizes beside the standardized mean difference, with the conversions between them and where they fail.
Sample size for comparing two proportions
The worked study found its difference with 80 per group. This section shows that the design gave it about a three-in-four chance of doing so, and that a study planned for 80% power needs about 95 per group. The power calculation follows the one-sample case in Tests and Sample Size for a Proportion. Here is the idea: the critical value is placed with the pooled standard deviation that holds under $H_0$, and the chance of passing it is computed with the unpooled one that holds under the alternative.
Take equal groups of $n$, and write $\bar{p} = (p_1 + p_2)/2$ for the common proportion the pooled estimate approaches when the groups are of equal size. Under $H_0$ the standard deviation of $\hat{p}_1 - \hat{p}_2$ is about $\sqrt{2\bar{p}(1-\bar{p})/n}$. So the test rejects (in the direction of $p_1 > p_2$) when the difference exceeds $z_{1-\alpha/2}\sqrt{2\bar{p}(1-\bar{p})/n}$. Under the alternative the difference is approximately normal with mean $p_1 - p_2$ and standard deviation $\sqrt{[p_1(1-p_1) + p_2(1-p_2)]/n}$, so
$$1 - \beta \approx \Phi\!\left(\frac{|p_1 - p_2|\sqrt{n} - z_{1-\alpha/2}\sqrt{2\bar{p}(1-\bar{p})}}{\sqrt{p_1(1-p_1) + p_2(1-p_2)}}\right),$$
and setting the argument equal to $z_{1-\beta}$ gives the sample size per group:
$$n = \left(\frac{z_{1-\alpha/2}\sqrt{2\bar{p}(1-\bar{p})} + z_{1-\beta}\sqrt{p_1(1-p_1) + p_2(1-p_2)}}{p_1 - p_2}\right)^2.$$
A simpler and widely used version uses the alternative's variance in both places,
$$n = \frac{(z_{1-\alpha/2} + z_{1-\beta})^2\,[p_1(1-p_1) + p_2(1-p_2)]}{(p_1 - p_2)^2}.$$
This is the corresponding approximation for a test with the unpooled standard error. It is close to the pooled formula when the proportions are not far apart. It is also the two-proportion form of the formula for two means in Sample Size for Comparing Two Means, with the Bernoulli variances in place of $\sigma_1^2 + \sigma_2^2$.
For a study designed to detect the population difference between young men and young women, 0.3175 against 0.1465, with a two-sided test at 0.05:
| Power | Pooled formula | Exact power at that $n$ | Simpler formula | Exact power at that $n$ |
|---|---|---|---|---|
| 80% | 94.39, so 95 per group | 0.810 | 91.65, so 92 | 0.796 |
| 90% | 125.86, so 126 | 0.903 | 122.69, so 123 | 0.900 |
The exact power is the probability, summed over every pair of counts, that the pooled test rejects. The pooled formula's 95 per group reaches 0.81, and the smallest group size with exact power of at least 0.80 is 93. The simpler formula, here, falls just short: 0.796 at 92 per group and 0.8997 at 123, which the table rounds to 0.900. The script also drew 95 men and 95 women from the NHANES populations, 10,000 times for each of 30 seeds. The pooled test rejected at a median rate of 0.8235. That is a little above the planned power, because the samples are a large fraction of the small populations.
So the worked study, with 80 per group, had about a three-in-four chance of finding the difference it found.
Example (a difference less than half as large). Across all ages, 58.6% of men and 51.1% of women in the NHANES population have hypertension, a difference of 7.5 points. That is less than half the 17-point difference among young adults, and detecting it with 80% power takes more than four times the 95 per group. It takes $n = 693.6$, so 694 per group (690.9 by the simpler formula), more than seven times the sample for young adults. Shrinking the difference to less than half would by itself more than quadruple $n$. The Bernoulli variances add the rest: they are larger for proportions near one half, 0.586 and 0.511, than for 0.318 and 0.146. The difference to be detected dominates the answer, as it did for means.
Worked problems
The first problem is an exercise from the course text's chapter on inference on proportions (Pagano, Gauvreau and Mattie, 2022), restated in our own words with its data. The other two are posed on this unit's NHANES populations. The script named under "In Python" checks every number.
Problem 1 (tuberculin skin tests and needle sharing). In a study of factors affecting the prevalence of tuberculosis among intravenous drug users, 24.7% of 97 people who said they shared needles had a positive tuberculin skin test, against 17.4% of 161 who said they did not share needles. (a) Assuming the two population proportions are equal, estimate their common value. (b) Test the null hypothesis that the proportions with a positive test are the same in the two populations. (c) State the probability distribution of the test statistic. (d) Find the $p$-value and state the conclusion. (e) Construct a 95% confidence interval for the difference of the two proportions.
Solution. The percentages fix the counts. $0.247 \times 97 = 23.96$, so $x_1 = 24$, and $24/97 = 0.2474$; the neighboring counts 23 and 25 would give 23.7% and 25.8%. $0.174 \times 161 = 28.01$, so $x_2 = 28$, and $28/161 = 0.1739$. The difference of the sample proportions is $0.2474 - 0.1739 = 0.0735$.
(a) The pooled proportion is $\hat{p} = (24 + 28)/(97 + 161) = 52/258 = 0.2016$.
(b) The hypotheses are $H_0: p_1 = p_2$ against $H_A: p_1 \ne p_2$. The expected counts under $H_0$ are 19.6 and 77.4 in the first group and 32.4 and 128.6 in the second, all well above 5 (by the course text's form of the check, 24, 73, 28 and 133). Then
$$\operatorname{SE}_0 = \sqrt{0.2016 \times 0.7984 \times \left(\frac{1}{97} + \frac{1}{161}\right)} = 0.05156, \qquad z = \frac{0.0735}{0.05156} = 1.426.$$
(c) If $H_0$ is true and the samples are large, as they are here, $z$ has approximately the standard normal distribution. Equivalently, $z^2 = 2.033$ is the chi-square statistic of the $2 \times 2$ table, with approximately the chi-squared distribution with one degree of freedom.
(d) The two-sided $p$-value is $2P(Z \ge 1.426) = 0.154$. It is well above 0.05, so $H_0$ is not rejected. The samples do not show that the proportion with a positive skin test differs between those who share needles and those who do not. That is not evidence that the proportions are equal, as part (e) makes plain.
(e) The interval uses the unpooled standard error:
$$\operatorname{SE} = \sqrt{\frac{0.2474 \times 0.7526}{97} + \frac{0.1739 \times 0.8261}{161}} = 0.05303, \qquad 0.0735 \pm 1.960 \times 0.05303 = (-0.030,\ 0.177).$$
Newcombe's interval for the same data is $(-0.027,\ 0.181)$. Either way the data are consistent with a proportion 3 points lower among those who share needles, with no difference, and with one 18 points higher. The interval contains 0, in agreement with the test. A difference of 7 points, if real, would matter, and these samples are too small to settle it.
Problem 2 (Fisher's exact test on a small table). The script drew 15 young men and 15 young women at random from the NHANES populations (seed 1). Six of the men and one of the women had hypertension. Test whether the proportions differ, with Fisher's exact test, and compare with the $z$ test and with Yates' correction.
Solution. Fisher's test gives $p = 0.080$, Yates' corrected chi-square 0.084 and the $z$ test 0.031. The table has $n_1 = n_2 = 15$, $m_1 = 7$ with hypertension and $m_2 = 23$ without. The expected count of hypertensive people in each group is $15 \times 7/30 = 3.5$, below 5, so the normal approximation is in doubt and the exact test is the one to use.
Given the margins, the number $a$ of hypertensive men can be 0 to 7, with
$$P(a) = \frac{\dbinom{15}{a}\dbinom{15}{7 - a}}{\dbinom{30}{7}}, \qquad \binom{30}{7} = 2{,}035{,}800.$$
| $a$ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| $\binom{15}{a}\binom{15}{7-a}$ | 6,435 | 75,075 | 315,315 | 621,075 | 621,075 | 315,315 | 75,075 | 6,435 |
| $P(a)$ | 0.0032 | 0.0369 | 0.1549 | 0.3051 | 0.3051 | 0.1549 | 0.0369 | 0.0032 |
For example, $a = 1$ gives $\binom{15}{1}\binom{15}{6} = 15 \times 5005 = 75{,}075$. The observed table has $a = 6$, with probability 0.0369. The tables no more likely than it are $a = 0, 1, 6$ and 7, so
$$p = \frac{2\,(6{,}435 + 75{,}075)}{2{,}035{,}800} = \frac{163{,}020}{2{,}035{,}800} = 0.0801.$$
For the $z$ test, $\hat{p}_1 = 6/15 = 0.400$, $\hat{p}_2 = 1/15 = 0.067$ and $\hat{p} = 7/30 = 0.2333$, so
$$\operatorname{SE}_0 = \sqrt{0.2333 \times 0.7667 \times \frac{2}{15}} = 0.1544, \qquad z = \frac{0.3333}{0.1544} = 2.158, \qquad p = 0.031.$$
With Yates' correction each $|O - E|$ falls from 2.5 to 2.0, the statistic is $X^2 = 2.981$, and $p = 0.084$.
The three tests part ways. The $z$ test rejects at the 0.05 level, and the exact test and the corrected test do not. The exact $p$-value, 0.080, is the one to report: it is a sum of 4 of the 8 possible tables' probabilities and involves no approximation. The $z$ test's 0.031 treats a count that can take only eight values as if it were normal. Here the populations do differ (0.3175 against 0.1465), so the exact test's failure to reject is a Type II error, of the kind a study of 15 per group must expect.
Problem 3 (sample size for two older age groups). In the NHANES population, 58.39% of adults aged 40 to 59 and 79.97% of adults aged 60 and over have hypertension. How many adults per group does a two-sided test at $\alpha = 0.05$ need to detect that difference with 80% power, and with 90%?
Solution. The pooled formula gives 71 per group for 80% power and 95 for 90%. The difference is $0.7997 - 0.5839 = 0.2158$, and $\bar{p} = (0.5839 + 0.7997)/2 = 0.6918$. The two standard deviation terms are
$$\sqrt{2\bar{p}(1-\bar{p})} = \sqrt{2 \times 0.6918 \times 0.3082} = 0.6530, \qquad \sqrt{p_1(1-p_1) + p_2(1-p_2)} = \sqrt{0.2430 + 0.1602} = 0.6349.$$
So
$$n = \left(\frac{1.960 \times 0.6530 + 0.8416 \times 0.6349}{0.2158}\right)^2 = 70.67 \to 71, \qquad n = \left(\frac{1.960 \times 0.6530 + 1.2816 \times 0.6349}{0.2158}\right)^2 = 94.10 \to 95.$$
The exact power of the pooled test, summed over every pair of binomial counts, is 0.811 at 71 per group and 0.905 at 95, so both plans meet their targets. The simpler formula gives 67.93 and 90.94, about three per group fewer. The answer for 80% power, 71, is smaller than the 95 needed for the young men and women, although the proportions here are nearer one half and so have larger variances. The difference to be detected is larger, 21.6 points against 17.1, and the sample size falls with the square of the difference.
Summary
| Quantity | Formula |
|---|---|
| Pooled proportion | $\hat{p} = (x_1 + x_2)/(n_1 + n_2)$ |
| Test statistic | $z = (\hat{p}_1 - \hat{p}_2)\big/\sqrt{\hat{p}(1-\hat{p})(1/n_1 + 1/n_2)}$ |
| Interval for $p_1 - p_2$ | $(\hat{p}_1 - \hat{p}_2) \pm z_{1-\alpha/2}\sqrt{\hat{p}_1(1-\hat{p}_1)/n_1 + \hat{p}_2(1-\hat{p}_2)/n_2}$ |
| Chi-square for a $2 \times 2$ table | $X^2 = N(ad - bc)^2/(n_1 n_2 m_1 m_2) = z^2$, 1 degree of freedom |
| Small expected counts | Fisher's exact test |
| Sample size per group | $n = \big[z_{1-\alpha/2}\sqrt{2\bar{p}(1-\bar{p})} + z_{1-\beta}\sqrt{p_1(1-p_1) + p_2(1-p_2)}\big]^2/(p_1 - p_2)^2$ |
What we have learned
The test of two proportions uses the pooled standard error, because under the null hypothesis the groups share one variance; the interval uses the unpooled one, because it must not assume what it measures. For the 80 young men and 80 young women, $p = 0.00115$, and the difference lay between 9 and 34 percentage points. The chi-square test of the $2 \times 2$ table is the same test, since $X^2 = z^2$, and Fisher's exact test takes over when expected counts are small. Detecting the 17-point difference with 80% power needs about 95 per group.
This article ends the unit and the course. The course began with Descriptive Statistics: tables, graphs and summary numbers for the sample in hand. Rates and Standardization put rates and proportions to work in public health. Probability gave a language for chance, which Screening and Diagnostic Tests applied to diagnosis, still before any inference. Theoretical Probability Distributions supplied the binomial, Poisson and normal models, and Sampling Distributions showed how a statistic varies from sample to sample. Every unit after that used one move: compare an estimate with its sampling variability, against a reference distribution, and turn the result into an interval or a test. It served for means in Confidence Intervals, Hypothesis Testing and Comparison of Two Means, and for several means in Analysis of Variance. Nonparametric Methods replaced the normal reference distribution with one found by counting. This unit applied the move to counts of yes-or-no outcomes.
The unit's main caution reaches back over all of them. A stated confidence level or significance level is a claim about a method, and it can be checked. For proportions the check is exact, and it showed the textbook interval falling short. Regression, the natural next step, is in the library's Simple Linear Regression and Logistic Regression.
In Python
from math import sqrt
from scipy import stats
x1, n1, x2, n2 = 26, 80, 9, 80
p1, p2, p = x1 / n1, x2 / n2, (x1 + x2) / (n1 + n2)
z = (p1 - p2) / sqrt(p * (1 - p) * (1 / n1 + 1 / n2))
print(z, 2 * stats.norm.sf(abs(z))) # about 3.251, 0.00115
table = [[x1, n1 - x1], [x2, n2 - x2]]
print(stats.chi2_contingency(table, correction=False)) # statistic 10.569 = z**2
print(stats.chi2_contingency(table).pvalue) # Yates: about 0.00221
print(stats.fisher_exact(table).pvalue) # about 0.00193
Every number in this article is computed by tools/experiments/applied-statistics/comparing-two-proportions.py in the site's repository; its $z^2$ equals scipy's uncorrected chi-square statistic to ten decimal places.
References
- National Center for Health Statistics (2020). National Health and Nutrition Examination Survey 2017–2018: DEMO_J, BPX_J and BPQ_J data files. Centers for Disease Control and Prevention. https://wwwn.cdc.gov/nchs/nhanes/continuousnhanes/default.aspx?BeginYear=2017 record · 109 pages
- Newcombe, R. G. (1998). Interval estimation for the difference between independent proportions: comparison of eleven methods. Statistics in Medicine, 17(8), 873–890. record
- NIST/SEMATECH. e-Handbook of Statistical Methods, §7.3.3 (How can we determine whether two processes produce the same proportion of defectives?). National Institute of Standards and Technology. https://www.itl.nist.gov/div898/handbook/ record · 37 pages
- Ostchega, Y., Fryar, C. D., Nwankwo, T., and Nguyen, D. T. (2020). Hypertension Prevalence Among Adults Aged 18 and Over: United States, 2017–2018. NCHS Data Brief No. 364. National Center for Health Statistics. record · 7 pages
- Pagano, M., Gauvreau, K., and Mattie, H. (2022). Principles of Biostatistics (3rd ed.). CRC Press. The course text. record · 48 pages
- SciPy documentation.
scipy.stats.chi2_contingencyandscipy.stats.fisher_exact. https://docs.scipy.org/doc/scipy/reference/stats.html record
Reviewed: verified, 16 Nov 2026 · checked against sources, 23 Nov 2026 · citations checked, 16 Nov 2026 · editorial review, 23 Nov 2026 · 13 revisions