Jugami Statistics

Jugami › Statistics and Probability › Comparing Two Groups

Statistics · Unit 10

Comparing Two Groups

Is the difference between two groups real, how big is it, and does it matter?

Most real questions compare: a drug against a placebo, one teaching method against another, plants grown one way against plants grown another. This unit builds on Inference, where you estimated and tested one proportion or one mean. Now there are two groups. You'll build confidence intervals and tests for a difference, shuffle the data to see what chance alone can do, learn why paired data need their own method, and see why a tiny p-value can sit next to a small effect.

Two groups of dots, blue and rose, with a curved arrow showing their labels being shuffled, above a small histogram of shuffled differences with the real difference marked far out in the tail. Real, or just the shuffle? real Gray bars: differences from shuffled labels.

Aspirin, 1988

On September 20, 1982, the Physicians' Health Study assigned its first participant. It was a randomized, double-blind, placebo-controlled trial: 22,071 healthy male doctors in the United States were randomly assigned to take either 325 mg of aspirin every other day or a placebo that looked the same. Nobody, not the doctors taking pills and not the researchers checking them, knew who had which.

The trial was meant to run for years. But on December 18, 1987, its independent monitoring board recommended stopping the aspirin part early because the benefit against heart attacks was already statistically extreme, and January 25, 1988 became its official end. The final report, in the New England Journal of Medicine in 1989, gave these counts after an average of about five years (60.2 months):

Heart attacks (myocardial infarctions) in the Physicians' Health Study
GroupDoctorsHeart attacksProportion
Aspirin11,0371391.26%
Placebo11,0342392.17%

The report summed this up as a 44% reduction in risk (relative risk 0.56, 95% confidence interval 0.45 to 0.70, P < 0.00001). That is three questions at once. Is the difference between 1.26% and 2.17% bigger than chance would produce? How big is it, with a margin of error? And how much does it matter to one person deciding whether to take a pill? This unit answers all three.

From one group to two

In Inference every question was about one group: one proportion (one-proportion z test) or one mean (one-sample t test). With two groups the parameter is a difference: p₁ − p₂ or μ₁ − μ₂. The pattern stays the same:

One new fact does most of the work. For two independent samples, variances add:

SE(difference) = √( SE₁² + SE₂² )

Standard errors do not add, but their squares do. The difference is less precise than either estimate alone, which makes sense: two sources of chance error instead of one. Where a difference comes from also matters. A randomized experiment like the aspirin trial lets a significant difference be read as caused by the treatment; an observational comparison does not (see Sampling and Surveys).

Difference of two proportions

Call the placebo group 1 and the aspirin group 2. Then p̂₁ = 239 ÷ 11,034 ≈ 0.02166 and p̂₂ = 139 ÷ 11,037 ≈ 0.01259, so the estimated difference is p̂₁ − p̂₂ ≈ 0.00907: about 0.9 percentage points fewer heart attacks with aspirin.

Confidence interval

(p̂₁ − p̂₂) ± z* √( p̂₁(1 − p̂₁)/n₁ + p̂₂(1 − p̂₂)/n₂ )

Here SE = √(0.02166 × 0.97834 ÷ 11,034 + 0.01259 × 0.98741 ÷ 11,037) ≈ 0.00175. A 95% interval is 0.00907 ± 1.96 × 0.00175, or about 0.0056 to 0.0125. Aspirin lowered the five-year heart attack rate by somewhere between about 0.6 and 1.2 percentage points. Zero is not in the interval.

Significance test

H₀: p₁ = p₂ (no difference) against Hₐ: p₁ ≠ p₂. If H₀ is true the two groups share one rate, so the test uses the pooled proportion: p̂ = (239 + 139) ÷ (11,034 + 11,037) = 378 ÷ 22,071 ≈ 0.01713.

z = (p̂₁ − p̂₂) ÷ √( p̂(1 − p̂)(1/n₁ + 1/n₂) )

SE = √(0.01713 × 0.98287 × (1/11,034 + 1/11,037)) ≈ 0.00175, so z ≈ 0.00907 ÷ 0.00175 ≈ 5.19. A z of 5.19 is far in the tail: the two-sided p-value is about 0.0000002. Chance alone almost never splits 378 heart attacks this unevenly between two groups of equal size.

Conditions: two independent random samples or random assignment; at least about 10 successes and 10 failures in each group (139 and 239 both pass); each sample less than 10% of its population if sampling without replacement.

Difference of two means

For quantitative data, compare x̄₁ − x̄₂. Each sample has its own standard deviation, and the standard error combines them:

SE = √( s₁²/n₁ + s₂²/n₂ )    t = (x̄₁ − x̄₂) ÷ SE    interval: (x̄₁ − x̄₂) ± t* × SE

This is the two-sample t procedure, often called Welch's t. Software finds the degrees of freedom with a long formula. By hand, a safe choice is the smaller of n₁ − 1 and n₂ − 1, which gives a slightly wider interval and a slightly larger p-value than software.

Worked example (made-up data). A school tries a new homework app in one randomly chosen group. 25 students using it average 72.4 on a test (s = 8.1); 30 who don't average 67.9 (s = 9.5).

The interval includes 0, matching p > 0.05: the data can't rule out no effect. It also includes 9 points, a big effect. “Not significant” here means “not enough data to tell”, not “the app does nothing”.

Conditions: independent random samples or random assignment; each group roughly normal, or large enough (about 30 or more) unless strongly skewed or with outliers; the two groups independent of each other.

Randomization tests: shuffle the labels

Formulas aren't the only way. If the treatment truly does nothing, each person's result would have been the same whichever group they landed in, so the group labels are just random tags. A randomization test (or permutation test) asks: if we shuffled the labels again and again, how often would the difference be as large as the one we saw?

  1. Compute the real difference between the groups.
  2. Pool all the values, shuffle, and deal them back into two groups of the original sizes.
  3. Compute the difference for the shuffled groups. Repeat thousands of times.
  4. The p-value is the fraction of shuffles at least as extreme as the real difference.

Try it. The first data set is made up: typing speeds (words per minute) of 16 students randomly split into 8 typing with music and 8 in silence.

Randomization tests need no normal curve and few conditions, only that labels could have been swapped under H₀. They are also exactly how random assignment protects an experiment: the p-value comes from the same coin flips that built the groups.

Paired versus independent samples

In 1876 Charles Darwin published an experiment on corn (Zea mays). He grew plants from cross-fertilized and self-fertilized seed in four pots, pairing a crossed plant with a self-fertilized one, and measured their final heights to the nearest eighth of an inch. Fifteen pairs, in four pots:

Darwin's 15 pairs of corn plants: self-fertilized height joined to cross-fertilized height Slope chart. Each line joins the self-fertilized plant on the left to its cross-fertilized partner on the right, in inches. Thirteen lines slope up (cross taller) and two slope down (pairs 2 and 15). The mean difference is 2.62 inches. Self-fertilized Cross-fertilized height (inches) 10 15 20 25 solid: cross taller (13) · dashed: self taller (2)
Darwin's corn, 1876: 15 pairs grown in the same pots. Each pair is one line. Data: Darwin (1876), as in R's HistData ZeaMays.

These are paired data. The two plants in a pair shared a pot, soil and light, so they are not independent, and the natural question is about the differences within pairs. Subtract (cross − self) in each pair and you have a single list of 15 numbers. Then use the one-sample methods from Inference on that list:

The randomization version flips signs instead of shuffling groups. If crossing made no difference, each pair's difference was equally likely to come out positive or negative. With 15 pairs there are 2¹⁵ = 32,768 sign patterns. In R. A. Fisher's 1935 book The Design of Experiments, he used Darwin's data (which Darwin had asked Francis Galton to examine) both for a t test on the differences and, later in the same chapter, for this sign-flipping test, one of the first permutation tests. Counting all 32,768 patterns, 1,726 give a mean difference at least as far from 0 as 2.62 inches, a two-sided p-value of about 0.053, very close to the t test. Switch the simulator to Darwin's pairs to watch it.

How to tell which you have

Analysing paired data as if independent is a real mistake, because it uses the wrong standard error. Usually pairing removes variation between pairs and makes the comparison sharper. In Darwin's data, the pots didn't differ much, so the independent analysis happens to give a smaller p-value (about 0.02), but that number answers a question about 30 unrelated plants that the experiment never asked. Use the method that matches the design.

Practical versus statistical significance

Statistically significant means “hard to explain by chance”. It says nothing about size. With 22,071 people, even a modest effect gives a huge z. So always ask how big the difference is, in units that matter.

The aspirin result can be stated two true ways:

Both are correct; a headline that gives only the relative one is telling half the story (see Misleading Statistics). And benefits must be weighed against harms measured in the same trial: the aspirin group had 23 hemorrhagic strokes against 12 on placebo (P = 0.06, not significant at 5%), and 169 ulcers against 138. Total deaths were nearly equal, 217 against 227. The report also found the heart attack benefit only among men aged 50 or older. Whether a 0.9-point drop is worth it is a medical judgment, not a statistical one. This page is about statistics, not medical advice.

The reverse also happens. A small study, like the homework app above, can find a large difference that isn't significant. Report the confidence interval: it shows the size and the uncertainty together, as the Inference unit advises, rather than a bare p-value.

Choosing a method

Which procedure for which comparison
DataDesignMethod
Yes/no outcomeTwo independent groupsTwo-proportion z interval or test
MeasurementTwo independent groupsTwo-sample t interval or test
MeasurementMatched pairsOne-sample t on the differences
AnyRandom assignment, few assumptionsRandomization test (shuffle labels, or flip signs for pairs)
Counts in several categoriesTwo or more groupsChi-square test

Common mistakes

Practice

History

Quiz

Ten questions. Pick an answer for each, then check.

1. In the Physicians' Health Study, 139 of 11,037 men on aspirin and 239 of 11,034 on placebo had heart attacks. The difference in proportions is about

2. For two independent samples, the standard error of a difference is found by

3. When testing H₀: p₁ = p₂, the pooled proportion is used because

4. Which situation calls for a paired analysis?

5. Darwin's 15 pairs of corn plants had a mean difference (cross − self) of 2.62 inches. To test whether crossing matters, you should

6. In a randomization test, each shuffle of the group labels shows

7. A randomization test gives 30 of 1,000 shuffles at least as extreme as the real difference. The estimated p-value is

8. A drug lowers a risk from 2% to 1%. Which statement is true?

9. A study of 1 million students finds a 0.2-point difference in average test scores, p < 0.0001. The best conclusion is

10. A two-sample t interval for μ₁ − μ₂ is (−0.4, 9.4). Which is correct?

Talk about it

Sources