Sample size for comparing two proportions (defect rates)
Sample size for a 2-proportions test is the number of units per group needed so that a 2-proportions test detects a given difference between two rates, such as scrap rates before and after a change or yields of two lines, with a chosen power. The usual planning formula is a normal approximation; a continuity correction brings it in line with exact and corrected tests.
When to use it
- You plan to compare the defective rate, yield or failure rate of two independent groups: two processes, two suppliers, old versus new method.
- The outcome for each unit is pass or fail.
- You need to know, before running the trial, whether the number of units you can afford can show the difference you hope for.
How it works
Let p₁ and p₂ be the two proportions you want to tell apart, and p̄ = (p₁ + p₂)/2. For a two-sided test at level α with power 1 − β, the number per group is
n = ( z₁₋α/₂ √(2 p̄ (1 − p̄)) + z₁₋β √(p₁(1 − p₁) + p₂(1 − p₂)) )² / (p₁ − p₂)²The first term reflects the spread under the null hypothesis (both rates equal to p̄), the second the spread under the alternative. This formula matches the pooled z test without continuity correction. If you will use Fisher's exact test or the chi-square test with Yates's continuity correction, which are more conservative, use the corrected size (Fleiss):
n_cc = (n/4) ( 1 + √( 1 + 4 / (n |p₁ − p₂|) ) )²What drives n is the difference relative to the rates themselves. Going from 6% to 3% is a large relative change but a small absolute one, and small rates mean few events per unit inspected.
Example
A press line scraps 6% of its parts. A new die-cleaning routine is expected to cut that to 3%. The plant will run the old and new routines on separate sets of parts and compare the scrap rates with a two-sided test at α = 0.05, with 80% power.
Here p̄ = 0.045, √(2 × 0.045 × 0.955) = 0.2932 and √(0.06 × 0.94 + 0.03 × 0.97) = 0.2924:
n = (1.960 × 0.2932 + 0.842 × 0.2924)² / 0.03² = 748.4 → 749 parts per routineThat is 1,498 parts in all. With the continuity correction the answer is 814 per routine.
A simulation checks both. Over 400,000 simulated trials (NumPy, seed 99), the uncorrected pooled z test with 749 parts per group rejected in 81.0% of the trials, slightly above the planned 80%, so the formula is a little conservative for that test. The continuity-corrected test with 814 per group rejected in 80.9%.
If the new routine only brought scrap down to 4%, the same formula asks for 1,863 parts per routine. The curves show how power grows with the number of parts for both cases:
How to read the results
- Plan for about 750 parts per routine to have an 80% chance of confirming a drop from 6% to 3%, or about 815 if the analysis will use a corrected or exact test.
- A smaller improvement costs far more. Halving the difference (6% to 4.5%) multiplies n by more than four, to about 3,470 per group; even 6% to 4% needs more than 1,800.
- Low power means inconclusive, not "no effect". With 300 parts per routine, power is only 43%. A non-significant result from such a trial says little.
- Count the events, not just the parts. At 749 parts and rates of 6% and 3%, you expect about 45 and 22 scrapped parts. The information is in those counts.
Common mistakes
- Using the formula for very small rates without checking. When the expected number of defectives per group is below about 10, use an exact method or a simulation.
- Comparing periods instead of randomized groups. "Last month versus this month" mixes the routine with everything else that changed. Alternate the routines or randomize which parts get which.
- Clustering. Parts from the same coil or the same shift tend to fail together. The binomial model then understates the variation, and the real power is lower. Sample across many coils and shifts.
- Ignoring pairing. If both conditions are applied to the same parts, the data are paired and need a different test, such as McNemar's test.
Frequently asked questions
Why do proportions need so many more units than measurements?
A pass/fail result carries little information. If you can measure the characteristic behind the failure (a dimension, a force), a 2-sample t test on that measurement usually needs far fewer parts.
Should I use the corrected or uncorrected sample size?
Use the one that matches the test you will run. The uncorrected size suits the plain z test or chi-square test; the corrected size suits Fisher's exact test or Yates's corrected chi-square test.
Can the groups have different sizes?
Yes. Power depends on both sizes, and for a fixed total it is highest when they are equal. Unequal groups make sense when one condition is cheaper to run.
What about a one-sided test?
Replace z₁₋α/₂ with z₁₋α. Use it only if a change in the other direction would not interest you, and decide that before seeing the data.