Sub topic 10
Comparing Two Groups: Two-Sample and Paired t-Tests
This chapter assumes you can write a null and alternative hypothesis, that you know what alpha and a p-value mean, and that you have already checked normality and equal variance as described in earlier chapters. Here the question is narrower: your Y is continuous, your X has exactly two levels, and you want to know whether the group means differ.
Where this test sits in the Analyze phase
In Analyze you often suspect that an input (X) changes an output (Y). When X is a two-level factor, such as machine A versus machine B, old supplier versus new supplier, or before versus after a change, and Y is a continuous measurement, the t-test family is the standard tool.
Two questions pick the exact test:
- Are the two sets of measurements independent, or is each value in one group naturally linked to a value in the other?
- If they are independent, can you assume equal variances?
The two-sample t-test
The two-sample t-test compares the means of two independent groups. Independent means that no value in one group is linked to any value in the other. For example, 30 parts from machine A and 30 different parts from machine B.
The hypotheses are:
- H0: μ1 = μ2 (the population means are equal)
- Ha: μ1 ≠ μ2 (two-sided), or μ1 < μ2 / μ1 > μ2 (one-sided)
The test statistic is the difference in sample means divided by its standard error. A large absolute t means the observed difference is large compared with the noise you would expect from sampling alone.
Pooled versus Welch
The classic pooled test assumes both populations share one variance and combines the two sample variances into a single estimate. The Welch test does not make that assumption. It uses each group's own variance and adjusts the degrees of freedom downward when the variances differ.
When variances are equal, Welch loses almost nothing. When they are unequal, especially with unequal group sizes, the pooled test can give a wrong p-value. Many practitioners therefore use Welch by default and skip the Levene gate. The code below still runs the checks so you can see what they say.
import numpy as np
from scipy import stats
rng = np.random.default_rng(42)
before = rng.normal(loc=50.0, scale=2.0, size=30)
after = rng.normal(loc=51.5, scale=2.0, size=30)
# Assumption checks
print("Shapiro-Wilk before p =", stats.shapiro(before).pvalue)
print("Shapiro-Wilk after p =", stats.shapiro(after).pvalue)
print("Levene p =", stats.levene(before, after).pvalue)
# Welch two-sample t-test (does not assume equal variances)
result = stats.ttest_ind(before, after, equal_var=False)
print("t =", result.statistic, "p =", result.pvalue)Read the output in order. If both Shapiro-Wilk p-values are above your alpha (commonly 0.05), you have no evidence against normality. Levene's p-value tells you whether the spreads differ noticeably. Then compare the final p-value with alpha. If p is below alpha, reject H0 and conclude the means differ.
Setting equal_var=True gives the pooled test. Do that only when you have a reason to believe the variances match, such as the same gauge and the same process.
Assumptions in practice
- Independence. Observations within and between groups must not influence each other. This is the assumption that matters most and the one no test can check for you. Consecutive parts from one machine, measured by a drifting gauge, are not independent.
- Normality. The test is fairly forgiving. With about 30 or more observations per group, the sampling distribution of the mean is close to normal even when the data are mildly skewed. With small samples and visible skew or outliers, use a non-parametric test (covered in a later chapter).
- Continuous data. Y should be measured on a continuous scale.
The paired t-test
The paired t-test compares two measurements that are linked, usually taken on the same unit. Examples are the same 12 operators measured before and after training, or the same 20 parts measured by two gauges.
The test works on the differences. You subtract each pair (after minus before) to get one column of differences, then run a one-sample t-test asking whether the mean difference is zero.
- H0: μd = 0
- Ha: μd ≠ 0
Pairing removes the unit-to-unit variation. Operators differ a lot from one another, but each operator's change is compared only with that operator's own baseline. That usually makes the test much more sensitive than treating the data as two independent groups.
import numpy as np
from scipy import stats
rng = np.random.default_rng(1)
n = 12
operator_level = rng.normal(50.0, 5.0, n) # large person-to-person spread
before = operator_level + rng.normal(0.0, 1.0, n)
after = operator_level + 1.0 + rng.normal(0.0, 1.0, n)
diff = after - before
print("Mean difference =", diff.mean())
print("Shapiro-Wilk on differences p =", stats.shapiro(diff).pvalue)
paired = stats.ttest_rel(after, before)
print("Paired: t =", paired.statistic, "p =", paired.pvalue)
wrong = stats.ttest_ind(after, before, equal_var=False)
print("Independent: t =", wrong.statistic, "p =", wrong.pvalue)The last two lines show why pairing matters. The data contain a true shift of 1.0, but the large operator-to-operator spread hides it from the independent test. The paired test cancels that spread and usually returns a much smaller p-value.
Normality applies to the differences
For a paired test, check normality of the differences, not of the two original columns. The raw columns can look non-normal, for instance bimodal because of the operator effect, while the differences are perfectly well behaved.
Choosing between paired and independent
Decide from how the data were collected, not from which test gives the smaller p-value.
| Situation | Test |
|---|---|
| Same units measured twice (before/after) | Paired |
| Two gauges measuring the same parts | Paired |
| Matched units chosen deliberately (twin machines, same batch) | Paired |
| Different units randomly assigned to each condition | Two-sample |
| Two separate production lines, different parts | Two-sample |
Common errors
- Using a two-sample test on before/after data from the same units. This throws away the pairing and weakens the test.
- Dropping unmatched pairs silently. If an operator has a "before" value but no "after" value, remove that whole pair and record that you did so. Never shift one column to line up the rows.
- Treating a non-significant result as proof of no difference. A large p-value means you lack evidence of a difference. With a small sample, the test may simply lack power.
- Ignoring the size of the difference. A tiny shift can be statistically significant with a large sample. Report the mean difference and its practical meaning, not only the p-value.
- Choosing one-sided after seeing the data. Decide the direction of Ha before looking at results. If you are unsure, use the two-sided test.
- Repeated measurements on the same unit counted as separate samples. Ten readings from one part are not ten independent parts. Average them first or use a design that accounts for the repeats.
Reporting the result
A defensible summary has four parts: the test used and why, the sample sizes, the estimated difference with its direction, and the p-value against the stated alpha. For example: "A Welch two-sample t-test compared 30 parts per machine. Machine B averaged 1.5 units higher (p below 0.05), so we reject the hypothesis of equal means." Then say whether a 1.5-unit shift matters for the specification.
Exercise
Eight technicians each ran the same calibration check with the old procedure and then with the new procedure. You want to know if the new procedure changes the average reading. Which test do you use, what do you check for normality, and what are H0 and Ha?
Show answer
Use the paired t-test, because the same technicians produce both readings. Compute the difference for each technician and check normality of those eight differences (for example with a Shapiro-Wilk test and a normal probability plot), not of the two raw columns. H0: the mean difference is 0 (μd = 0). Ha: the mean difference is not 0 (μd ≠ 0), a two-sided test because you only asked whether the procedure changes the reading. With only eight differences, look closely for outliers, and consider a non-parametric test if the differences are clearly skewed.
Exercise
Two production lines each yield 40 parts. Line 1 has a standard deviation of 1.0 and Line 2 has a standard deviation of 3.0. You are unsure whether the variances are equal. Which t-test variant do you run, and why?
Show answer
Run the Welch two-sample t-test (equal_var=False in SciPy). The sample standard deviations differ by a factor of three, which suggests unequal variances, and Welch stays reliable in that case. It also performs about as well as the pooled test when variances are equal, so using it by default carries little cost.