2-sample t-test: Welch's test explained with an example
The 2-sample t-test checks whether two independent groups, such as two machines, two suppliers or two settings, have different means. It divides the difference between the sample means by its standard error and reads the p-value from a t distribution. The default today is Welch's t-test, which does not assume the two groups have the same standard deviation; the older pooled version does.
When to use it
- You want to compare two machines, lines, shifts, suppliers or recipes on a continuous measurement.
- The two samples are independent: different parts in each group, with no natural pairing. If each unit is measured under both conditions, use the paired t-test.
- You need an estimate of how big the difference is, with a confidence interval.
- For three or more groups, use one-way ANOVA instead of several t-tests.
How it works
Hypotheses, with μ₁ and μ₂ the two process means:
- H₀: μ₁ − μ₂ = 0 (or another stated difference δ₀).
- H₁: μ₁ − μ₂ ≠ 0, or > 0, or < 0.
Welch's test (unequal variances, the default):
t = (x̄₁ − x̄₂) / √(s₁²/n₁ + s₂²/n₂)Its degrees of freedom come from the Welch–Satterthwaite formula, and are usually not a whole number:
df = (s₁²/n₁ + s₂²/n₂)² / [ (s₁²/n₁)²/(n₁ − 1) + (s₂²/n₂)²/(n₂ − 1) ]The confidence interval for the difference is (x̄₁ − x̄₂) ± t(1 − α/2, df) × √(s₁²/n₁ + s₂²/n₂).
Pooled test (equal variances assumed): combine the two variances into sₚ² = [(n₁ − 1)s₁² + (n₂ − 1)s₂²]/(n₁ + n₂ − 2), use the standard error sₚ√(1/n₁ + 1/n₂) and n₁ + n₂ − 2 degrees of freedom. It is slightly more powerful when the standard deviations really are equal, but it can give misleading p-values when they are not, especially with unequal sample sizes. Welch's test loses very little when the variances are equal, so it is the safer default.
Assumptions: independent observations within and between groups; each group roughly normal, or large enough (about 15 or more per group) for the means to be close to normal.
Example
An electronics plant runs two wire bonders. A process engineer suspects Bonder B makes weaker bonds. She pulls 10 bonds from Bonder A and 12 from Bonder B and records the pull strength in grams-force (gf):
| Bonder A (gf) | Bonder B (gf) |
|---|---|
| 9.3, 9.6, 10.0, 9.2, 9.4, 8.9, 9.9, 10.1, 9.8, 9.6 | 10.5, 9.2, 8.4, 9.0, 8.3, 9.3, 8.9, 7.5, 9.5, 7.3, 8.8, 9.3 |
| Group | N | Mean | StDev |
|---|---|---|---|
| Bonder A | 10 | 9.580 | 0.382 |
| Bonder B | 12 | 8.833 | 0.875 |
Welch's test, two-sided, α = 0.05:
- Difference: 9.580 − 8.833 = 0.747 gf.
- Standard error: √(0.382²/10 + 0.875²/12) = 0.280.
- t = 0.747/0.280 = 2.67.
- df = 15.61 from the Welch–Satterthwaite formula.
- p-value = 0.017.
- t(0.975, 15.61) = 2.124, so the 95% CI for μ_A − μ_B is 0.747 ± 2.124 × 0.280 = 0.15 to 1.34 gf.
For comparison, the pooled test gives sₚ = 0.698, t = 2.50 with 20 df, p = 0.021, and a 95% CI of 0.12 to 1.37 gf. The conclusion is the same here, but the pooled version claims 20 degrees of freedom it does not have, because Bonder B's standard deviation is more than twice Bonder A's.
How to read the results
- The p-value of 0.017 is below 0.05: the bonders differ in mean pull strength. Bonder B's bonds are weaker on average.
- The interval puts the difference between 0.15 and 1.34 gf. The lower end is small; the upper end is more than three times the standard deviation of Bonder A. More data would sharpen the estimate.
- Look at the spread too. Bonder B is not only lower but much more variable, and its two weakest bonds (7.3 and 7.5 gf) drive the risk of failing a minimum pull specification. Check the spread formally with the 2 variances test, and look for the cause (capillary wear, bond force or ultrasonic power settings).
- Act on the engineering question. The likely next step is to compare the two bonders' settings and maintenance records, then confirm a fix with a new sample.
Common mistakes
- Using the 2-sample test on paired data. If the same parts or the same patients are measured twice, the pairing removes part-to-part variation; ignoring it throws away power.
- Defaulting to the pooled test. When the standard deviations differ and the group sizes differ, the pooled p-value can be badly off. Welch's test avoids the problem.
- Running many t-tests across several groups. Each test has a 5% false-alarm risk, and they add up. Use ANOVA with multiple comparisons.
- Comparing means and ignoring the tails. For pull strength, burst pressure or other minimum-spec characteristics, a group with a good mean and a large spread can be the worse one.
Frequently asked questions
Should I use Welch's or the pooled t-test?
Welch's, unless you have strong reasons to believe the standard deviations are equal. Welch's test is almost as powerful when they are equal and much more reliable when they are not.
Why are the degrees of freedom not a whole number?
The Welch–Satterthwaite formula approximates the distribution of the t statistic when the variances differ, and the best-fitting t distribution usually has a fractional df. The t distribution is defined for any positive df, so this is not a problem.
What if my data are not normal?
With 15 to 20 or more per group, the test is robust to moderate skewness. For small skewed samples or outliers, use the Mann–Whitney test, which compares the groups by ranks.
How many samples per group do I need?
That depends on the difference you want to detect and the spread. See sample size for the 2-sample t.