Sub topic 9
How much data is enough: sample size and power
This chapter assumes you can run a two-sample t-test or a chi-square test, read a p-value, and name Type I and Type II errors. It covers how many observations you need before a test can reliably confirm or refute a suspected root cause. You will use the data collection plan from earlier chapters and the error ideas from the p-value chapter.
Why sample size matters
A hypothesis test can fail in two ways. If you reject a true "no effect" claim, you make a Type I error. Its probability is the significance level, alpha, which you set before looking at the data. If you fail to reject a "no effect" claim when the cause really does matter, you make a Type II error. Its probability is beta.
Power is the probability that the test detects an effect of a given size when that effect is real. Power equals 1 − beta. A common target is 0.80, which means that if the cause really shifts the output by the amount you care about, you will see a significant result in about 8 of 10 repeats of the study.
Sample size is the lever you control most easily. With too little data, a real cause can produce a non-significant p-value. The tempting conclusion is "this cause is not the problem", and it is wrong. An underpowered test can't tell "no effect" from "not enough data to see the effect".
Too much data has its own cost. Collecting 5,000 units to test a 0.01-minute difference in cycle time costs money and may flag a difference that has no practical value. Planning the sample size ahead of time keeps both problems in check.
The four quantities that determine sample size
Sample size, alpha, power, effect size and variation are linked. Fix any four and the fifth follows.
| Quantity | Meaning | Effect on required sample size |
|---|---|---|
| Alpha | Accepted risk of a false positive | Smaller alpha needs more data |
| Power | Desired chance of detecting a real effect | Higher power needs more data |
| Effect size | Smallest difference worth detecting | Smaller effect needs more data |
| Variation | Spread of the data, for example the standard deviation | More variation needs more data |
The effect size is a business and engineering decision, not a statistical one. Ask: what is the smallest shift in the process output Y that would justify acting? If a cycle time difference of 0.3 minutes would not change cost or customer experience, don't plan a test that hunts for 0.05 minutes.
For continuous data, the effect size is often expressed as a standardized difference, Cohen's d: the difference in means divided by the common standard deviation. A d of 0.2 is small, 0.5 is medium and 0.8 is large. These labels are only rough conventions. In a process setting, work from real units (minutes, millimetres, percentage points) and convert to d only when a tool needs it.
Estimating variation
The standard deviation is the input you usually don't know in advance. Common sources, from best to weakest:
- Historical data from the same process and measurement system.
- A small pilot sample, for example 15 to 30 units, taken under the same conditions.
- A subject-matter estimate, such as the expected range divided by 4 as a rough standard deviation guess.
Use a conservative estimate. If you underestimate the standard deviation, the real study will be underpowered. Pilot standard deviations are themselves noisy, so a modest safety margin, such as 10 to 20 percent, is reasonable.
Measurement error adds to the observed variation. If the data quality chapter showed a noisy gauge, its error inflates the standard deviation and raises the sample size you need. Improving the measurement system is sometimes cheaper than collecting more data.
Sample size for comparing two means
For a two-sided, two-sample t-test with equal group sizes, the approximate sample size per group is:
n = 2 × ((z(1−alpha/2) + z(power)) × sigma / delta)²
Here sigma is the standard deviation, delta is the smallest difference worth detecting, and the z values come from the standard normal distribution. For alpha = 0.05 and power = 0.80, the z values are about 1.96 and 0.84. The sum is about 2.80, and its square is about 7.85, so n is roughly 15.7 × (sigma / delta)² per group.
Suppose the cycle time standard deviation is 0.5 minutes and you want to detect a shift of 0.5 minutes. Then sigma / delta is 1, and you need about 16 per group. This is a normal-approximation answer, and an exact calculation based on the t distribution gives a slightly larger number. The code below does the exact calculation.
from statsmodels.stats.power import TTestIndPower
sigma = 0.5 # standard deviation of cycle time, minutes
delta = 0.5 # smallest shift worth detecting, minutes
effect_size = delta / sigma # Cohen's d
analysis = TTestIndPower()
n_per_group = analysis.solve_power(
effect_size=effect_size,
alpha=0.05,
power=0.80,
ratio=1.0,
alternative="two-sided",
)
print(f"Units needed per group: {n_per_group:.1f}")The result is about 17 per group, so you round up to 17 and plan for at least 34 measurements in total. Halving the shift you want to detect to 0.25 minutes doubles d's denominator and quadruples the sample size, to about 64 per group. This square-law relationship is the most useful rule of thumb in this chapter: halve the effect, and you need roughly four times the data.
Reading a power curve
You can also fix the sample size and ask what power you have. This is the right question when the data already exist, for example when only 10 units per shift are available.
from statsmodels.stats.power import TTestIndPower
analysis = TTestIndPower()
for n in [8, 10, 15, 20, 30, 50]:
power = analysis.power(effect_size=0.8, nobs1=n, alpha=0.05, ratio=1.0)
print(f"n per group = {n:>2} power = {power:.2f}")With a large effect (d = 0.8), 8 units per group give a power of roughly 0.35 to 0.40, and about 26 per group are needed to reach 0.80. The earlier chapter's cycle time example used 8 units per setup. That worked because the difference there was very large relative to the spread, but with a more typical effect the same sample would miss a real cause most of the time.
Sample size for proportions and counts
When the output is a defect rate, the same logic applies, but the variation depends on the proportion itself. You specify the baseline proportion and the proportion you want to detect.
from statsmodels.stats.power import NormalIndPower
from statsmodels.stats.proportion import proportion_effectsize
p_a = 0.09 # defect rate on shift A (baseline)
p_b = 0.20 # defect rate on shift B (suspected worse)
effect_size = proportion_effectsize(p_b, p_a) # Cohen's h
n_per_group = NormalIndPower().solve_power(
effect_size=effect_size,
alpha=0.05,
power=0.80,
ratio=1.0,
alternative="two-sided",
)
print(f"Units needed per shift: {n_per_group:.0f}")For rates near 9% versus 20%, you need on the order of 100 units per shift. Rare defects are costly to study. To detect a change from 1% to 2%, you need roughly 2,300 units per group. When defects are rare, consider these options:
- Sample from a period or lot known to have a higher defect rate, if that doesn't bias the comparison.
- Use a continuous measurement that precedes the defect, such as a dimension close to the tolerance limit, instead of the pass/fail flag. Continuous data carries more information per unit.
- Accept a larger minimum detectable difference.
The chi-square example from the earlier chapter used 200 units per shift, and its 9% versus 20.5% result sits comfortably above the roughly 100-unit requirement for this effect size.
Common errors
- Reading "not significant" as "no effect". Report the power or the confidence interval for the difference. A wide interval that includes both zero and a practically important effect means the data are inconclusive.
- Computing power after the fact from the observed effect. "Post hoc power" based on the observed difference is just a restatement of the p-value. Compute power for the smallest effect you care about, ideally before collecting data.
- Ignoring non-independence. Sample size formulas assume independent observations. Ten consecutive parts from one machine run are closer to one observation than ten. Spread the sampling across time, operators and batches. This ties back to the data collection plan.
- Stopping when the p-value turns significant. Checking repeatedly and stopping at the first p below 0.05 inflates the Type I error rate well above 5%. Fix the sample size in advance, or use a method designed for sequential looks.
- Choosing the effect size to fit the budget. Working backward from the number of units you can afford to an "effect size" gives a test that looks planned but may answer nothing useful. If the affordable sample can only detect a huge effect, say so in the plan.
- Forgetting dropouts. Missing values, rejected measurements and invalid records reduce usable data. Add a margin, for example 10%.
- Using many comparisons without adjustment. If you test five suspected causes at alpha = 0.05 each, the chance of at least one false positive is about 23%. Plan for it in alpha or in the confirmatory step.
Recording the plan
Write the sample size reasoning into the data collection plan before collecting data. A short record is enough:
| Item | Example entry |
|---|---|
| Output Y | Cycle time, minutes |
| Comparison | Old setup versus suspected setup |
| Test | Welch two-sample t-test, two-sided |
| Alpha | 0.05 |
| Smallest shift worth detecting | 0.5 minutes |
| Standard deviation estimate | 0.5 minutes, from last quarter's data plus 10% margin |
| Power target | 0.80 |
| Units per group | 17, plus 10% margin, rounded to 20 |
| Sampling method | Alternate setups across three days, two operators |
This record lets a reviewer judge a later "not significant" result fairly. It also stops the team from changing the question after seeing the numbers.
Exercise
A team suspects a new fixture raises mean flatness by 0.04 mm. The standard deviation from past data is 0.06 mm. Using alpha = 0.05 and power = 0.80 with the normal-approximation formula, about how many parts per group do they need? Then, if the shift of interest were only 0.02 mm, how would the answer change?
Show answer
Here sigma / delta = 0.06 / 0.04 = 1.5, so n ≈ 15.7 × 1.5² ≈ 35 parts per group. For a shift of 0.02 mm, sigma / delta = 3, so n ≈ 15.7 × 9 ≈ 141 per group. Halving the shift roughly quadruples the sample size.
Exercise
A pilot with 6 units per shift gave a p-value of 0.21 for a difference in defect rate. The team concludes shift is not a cause. What is wrong with this conclusion, and what should they do?
Show answer
With only 6 units per group the test has very low power, so a non-significant result is expected even if the shift effect is real. The result is inconclusive, not negative. The team should decide the smallest defect rate difference that matters, calculate the required sample size, collect that data using a sampling plan spread across time, and then test again. Reporting a confidence interval for the difference would also show how wide the uncertainty is.