Statistics Chapter 11 of 15

Chapter 11: Comparing Two Groups

Two proportions, two means, paired data, permutation tests

All chapters
On this page
What you will be able to do after this chapter
  • Decide whether a comparison involves two independent samples or paired data, and why the distinction matters.
  • Test and estimate the difference between two proportions.
  • Test and estimate the difference between two means using the two-sample (Welch) t procedure.
  • Analyze paired data with a one-sample t procedure on the differences, and explain when pairing increases precision.
  • Carry out and interpret a permutation test, and explain why it needs few assumptions.
  • Measure and interpret effect size (Cohen's d) alongside a p-value.
  • Draw appropriate conclusions about causation from comparative studies.

Real questions are rarely about one group. Does a new drug outperform the standard treatment? Do students taught with method A score higher than those taught with method B? Is the click rate higher for website design X than for design Y? The methods of Chapters 9 and 10 extend directly: we build a confidence interval or a test for the difference between two parameters, using the sampling distribution of the difference between two statistics.

11.1

Two samples: independent or paired?

The first step in any comparison is to identify how the data were collected, because it dictates the method.

Independent samplesPaired (matched) data
StructureTwo separate groups; no natural link between an observation in one group and one in the otherEach observation in one condition is naturally matched with one in the other
ExamplesTreatment group versus control group with different subjects; men versus women; two random samples of productsBefore and after on the same person; twins; left and right eye; two measurements of the same item by two methods
MethodTwo-sample procedures (this chapter, Sections 11.2 and 11.3)Compute the difference within each pair, then use one-sample procedures on the differences (Section 11.4)
The most common mistake

Using an independent-samples method on paired data throws away the matching and typically reduces power (Section 11.4 shows how much). Using a paired method on independent samples is meaningless because there are no pairs. Ask yourself: is there a natural one-to-one link between the observations in the two groups?

11.2

Comparing two proportions

Let \(\hat{p}_1 = x_1/n_1\) and \(\hat{p}_2 = x_2/n_2\) be the sample proportions of two independent samples. Since variances of independent quantities add (Chapter 6), the difference has

\[\mu_{\hat{p}_1 - \hat{p}_2} = p_1 - p_2, \qquad \sigma_{\hat{p}_1 - \hat{p}_2} = \sqrt{\frac{p_1(1-p_1)}{n_1} + \frac{p_2(1-p_2)}{n_2}},\]

and it is approximately normal when both samples meet the large-counts condition.

Two-proportion procedures

Confidence interval (uses each sample's own \(\hat{p}\)):

\[(\hat{p}_1 - \hat{p}_2) \pm z^* \sqrt{\frac{\hat{p}_1(1-\hat{p}_1)}{n_1} + \frac{\hat{p}_2(1-\hat{p}_2)}{n_2}}.\]

Test of \(H_0\!: p_1 = p_2\) (if the null is true both groups share one proportion, so we pool the data to estimate it):

\[\hat{p}_c = \frac{x_1 + x_2}{n_1 + n_2}, \qquad z = \frac{\hat{p}_1 - \hat{p}_2}{\sqrt{\hat{p}_c(1-\hat{p}_c)\left(\dfrac{1}{n_1} + \dfrac{1}{n_2}\right)}}.\]

Conditions: random samples or randomized experiment; independence within and between groups (each \(n\) less than 10 percent of its population); at least 10 successes and 10 failures in each group (for the test, using the pooled proportion; for the interval, using the sample counts).

Example 11.1: An A/B test

A website randomly shows visitors one of two page designs (hypothetical data). Design A: 168 of 1,200 visitors click "buy." Design B: 210 of 1,300 click. Is there convincing evidence that the click rates differ? Estimate the difference.

\(\hat{p}_A = 0.1400\), \(\hat{p}_B = 0.1615\), difference \(\hat{p}_B - \hat{p}_A = 0.0215\). All four counts (168, 1,032, 210, 1,090) exceed 10.

Test. \(H_0\!: p_A = p_B\), \(H_a\!: p_A \ne p_B\). Pooled \(\hat{p}_c = 378/2500 = 0.1512\). \(\text{SE} = \sqrt{0.1512(0.8488)(1/1200 + 1/1300)} = 0.01434\). \(z = 0.02154/0.01434 = 1.502\); two-sided p-value \(= 0.133\). We fail to reject \(H_0\): the data do not provide convincing evidence that the click rates differ.

Interval. Unpooled \(\text{SE} = 0.01430\). A 95 percent interval for \(p_B - p_A\) is \(0.0215 \pm 1.96(0.0143) = (-0.006,\ 0.050)\). It contains 0, in agreement with the test, but also contains differences as large as 5 percentage points. The honest summary is not "the designs are the same" but "this experiment could not tell; a larger sample is needed if a 1 to 2 point difference matters."

11.3

Comparing two means

For independent samples with means \(\bar{x}_1, \bar{x}_2\), SDs \(s_1, s_2\) and sizes \(n_1, n_2\), the difference \(\bar{x}_1 - \bar{x}_2\) estimates \(\mu_1 - \mu_2\) with standard error

\[\text{SE} = \sqrt{\frac{s_1^2}{n_1} + \frac{s_2^2}{n_2}}.\]
Two-sample t procedures (Welch) \[\text{CI: } (\bar{x}_1 - \bar{x}_2) \pm t^*\sqrt{\frac{s_1^2}{n_1} + \frac{s_2^2}{n_2}}, \qquad \text{Test } H_0\!: \mu_1 = \mu_2:\ \ t = \frac{\bar{x}_1 - \bar{x}_2}{\sqrt{s_1^2/n_1 + s_2^2/n_2}}.\]

The degrees of freedom come from the Welch-Satterthwaite formula:

\[\text{df} = \frac{\left(\dfrac{s_1^2}{n_1} + \dfrac{s_2^2}{n_2}\right)^2}{\dfrac{(s_1^2/n_1)^2}{n_1 - 1} + \dfrac{(s_2^2/n_2)^2}{n_2 - 1}}.\]

Software computes this; by hand, a conservative choice is \(\text{df} = \min(n_1 - 1,\ n_2 - 1)\). Conditions: two independent random samples (or randomized experiment); each population approximately normal or each sample size large (same guidelines as the one-sample t: no strong skew or outliers for small samples).

Example 11.2: Two teaching methods

Students are randomly assigned to two teaching methods (hypothetical data). The 15 students taught with method A score \(\bar{x}_1 = 78.4\) with \(s_1 = 9.2\); the 12 students taught with method B score \(\bar{x}_2 = 69.4\) with \(s_2 = 11.5\). Test for a difference in mean scores at \(\alpha = 0.05\) and give a 95 percent interval.

State. \(H_0\!: \mu_A = \mu_B\), \(H_a\!: \mu_A \ne \mu_B\).

Do. \(s_1^2/n_1 = 5.643\) and \(s_2^2/n_2 = 11.021\), so \(\text{SE} = \sqrt{16.664} = 4.082\). \(t = 9.0/4.082 = 2.205\). The Welch degrees of freedom are \(20.85\). Two-sided p-value \(= 0.039\).

Conclude. Since \(0.039 \lt 0.05\), there is convincing evidence that the mean scores differ; method A appears higher. Because the students were randomly assigned, we may attribute the difference to the teaching method (subject to the usual cautions).

Interval. \(t^*_{20.85} = 2.081\), so \(9.0 \pm 2.081(4.082) = (0.51,\ 17.49)\): we are 95 percent confident that method A raises the mean score by between about half a point and 17.5 points. The interval excludes 0, consistent with the test, but it is wide, so the size of the benefit is very uncertain.

Pooled versus Welch

An older method, the pooled t test, assumes the two populations have equal variances and combines the two sample variances: \(s_p^2 = \dfrac{(n_1-1)s_1^2 + (n_2-1)s_2^2}{n_1 + n_2 - 2}\), with \(\text{df} = n_1 + n_2 - 2\). When the variances really are equal it is very slightly more powerful; when they are not (especially with unequal sample sizes) it can be badly wrong. Welch's test loses almost nothing when variances are equal and is safe when they are not, so it is the modern default. For Example 11.2 the pooled test gives \(t = 2.261\), \(p = 0.033\): here similar, but the Welch answer is the one to trust.

11.4

Paired data

When measurements come in natural pairs, compute the difference \(d_i = (\text{after})_i - (\text{before})_i\) within each pair, and treat the \(n\) differences as a single sample. Then use the one-sample t procedures of Chapters 9 and 10 with \(\bar{d}\), \(s_d\), and \(\text{df} = n - 1\):

\[\bar{d} \pm t^*\frac{s_d}{\sqrt{n}}, \qquad t = \frac{\bar{d} - 0}{s_d/\sqrt{n}}.\]

The conditions concern the differences: a random sample of pairs (or random order of the two conditions within each subject), and differences that are approximately normal (no strong skew or outliers) or \(n\) large.

Example 11.3: Before and after a course

Eight students take a diagnostic test before and after a short revision course (synthetic data).

Student12345678
Before6270556874606671
After6875577479666578
Difference652656-17

\(\bar{d} = 4.5\), \(s_d = 2.673\), \(\text{SE} = 2.673/\sqrt{8} = 0.945\). To test \(H_0\!: \mu_d = 0\) against \(H_a\!: \mu_d \ne 0\): \(t = 4.5/0.945 = 4.762\), \(\text{df} = 7\), two-sided p-value \(= 0.0021\). The 95 percent interval is \(4.5 \pm 2.365(0.945) = (2.27,\ 6.73)\): scores rose by about 2 to 7 points on average.

Why pairing matters. Suppose we had wrongly treated the two rows as independent samples. The before and after scores each vary by 6 to 7 points across students (some students are simply stronger), which swamps the 4.5-point average gain: \(\text{SE} = 3.49\), \(t = 1.29\), p-value \(= 0.22\). The student-to-student differences cancel when we work within each pair, which is why the paired analysis (\(p = 0.002\)) detects an effect the independent analysis misses. The before and after scores here have correlation 0.94.

When does pairing help?

The variance of a difference between two variables with correlation \(\rho\) is \(\sigma_1^2 + \sigma_2^2 - 2\rho\sigma_1\sigma_2\). The stronger the positive correlation within pairs, the smaller the variance of the differences, and the more precise the paired analysis. Pairing pays off when the pairing variable (subject, family, item) explains a lot of the variation. If pairs are uncorrelated it gains nothing (and loses a few degrees of freedom).

11.5

Permutation tests

The t procedures assume roughly normal populations. A permutation test (randomization test) needs almost no distributional assumptions. Its logic mirrors the null hypothesis directly:

The permutation test
  1. \(H_0\) says the group labels do not matter: whichever group a value came from, it would have been the same.
  2. Compute the observed difference (say in means) between the two groups.
  3. Pool all the values and randomly reassign them to the two groups, keeping the original group sizes. Compute the difference for this shuffle.
  4. Repeat many times to build the permutation distribution: what differences arise by chance alone when labels are meaningless.
  5. The p-value is the fraction of shuffles whose difference is at least as extreme as the observed one.
Example 11.4: An exact permutation test with six values

Group A: 12, 15, 18. Group B: 9, 10, 14. Observed difference in means: \(15 - 11 = 4\). If labels do not matter, every way of choosing which 3 of the 6 values form "group A" is equally likely: \(\binom{6}{3} = 20\) arrangements. Enumerating them, exactly 2 arrangements give a difference of 4 or more (the observed arrangement \(A = \{12, 15, 18\}\) and the arrangement \(A = \{14, 15, 18\}\)), and 2 give a difference of \(-4\) or less (\(A = \{9, 10, 14\}\) and \(A = \{9, 10, 12\}\)). So the one-sided p-value is \(2/20 = 0.10\) and the two-sided p-value is \(4/20 = 0.20\). With only six observations no arrangement could ever give a p-value below \(1/20 = 0.05\) for a one-sided test: tiny samples have little power under any method.

Permutation tests can use any statistic (the difference in medians, a ratio, a maximum), are valid for small samples and skewed data, and, for randomized experiments, mirror the actual randomization performed. For samples of moderate size they usually agree closely with the two-sample t test.

11.6

Effect size

A p-value depends on both the size of the effect and the sample size, so it cannot tell us how big an effect is. An effect size expresses the difference on a scale independent of the sample size. For two means the most common is Cohen's d, the difference in means in units of the pooled standard deviation:

\[d = \frac{\bar{x}_1 - \bar{x}_2}{s_p}, \qquad s_p = \sqrt{\frac{(n_1 - 1)s_1^2 + (n_2 - 1)s_2^2}{n_1 + n_2 - 2}}.\]

Cohen's rough benchmarks are \(d \approx 0.2\) (small), \(0.5\) (medium), \(0.8\) (large), but these are conventions, not laws: what counts as important depends on the field and the stakes. In Example 11.2, \(s_p = 10.28\) and \(d = 9.0/10.28 = 0.88\): a large effect, though estimated imprecisely from 27 students.

Causation in comparative studies

The conclusion "the difference is real" must be separated from "the treatment caused the difference." A randomized experiment (Chapter 2) balances lurking variables across groups, so a significant difference can be attributed to the treatment. In an observational comparison (for example, users of a supplement versus non-users), a significant difference may reflect confounding: the groups differed before the treatment. The statistics are identical; the interpretation is not.

11.7

Choosing the procedure

SituationProcedureKey formula
Two independent proportionsTwo-proportion z (pooled for tests, unpooled for intervals)\(z = \dfrac{\hat{p}_1 - \hat{p}_2}{\sqrt{\hat{p}_c(1-\hat{p}_c)(1/n_1 + 1/n_2)}}\)
Two independent meansTwo-sample (Welch) t\(t = \dfrac{\bar{x}_1 - \bar{x}_2}{\sqrt{s_1^2/n_1 + s_2^2/n_2}}\)
Paired measurementsOne-sample t on differences\(t = \dfrac{\bar{d}}{s_d/\sqrt{n}}\), \(\text{df} = n - 1\)
Small or odd data, any statisticPermutation testp-value = fraction of shuffles as extreme as observed
Three or more meansANOVA (Chapter 13)F test
Two categorical variablesChi-square (Chapter 12)\(\chi^2\) test
11.8

Chapter summary

Key ideas and formulas
  • First decide: independent samples, or paired? The analysis follows from the design.
  • Two proportions: interval uses \(\sqrt{\hat{p}_1(1-\hat{p}_1)/n_1 + \hat{p}_2(1-\hat{p}_2)/n_2}\); test uses the pooled \(\hat{p}_c\).
  • Two means (Welch): \(\text{SE} = \sqrt{s_1^2/n_1 + s_2^2/n_2}\), Welch df (or \(\min(n_1-1, n_2-1)\) by hand). Default to Welch rather than the pooled test.
  • Paired: take differences, then a one-sample t on \(\bar{d}\) and \(s_d\). Pairing helps when within-pair correlation is high.
  • Variances of independent estimates add, so the standard error of a difference is larger than either alone.
  • Permutation test: shuffle the labels, build the null distribution of the statistic, and count how often it is as extreme as observed.
  • Report effect size (Cohen's d, or the difference with its interval) with the p-value.
  • Only randomized experiments support causal conclusions from a significant difference.

Key terms

TermMeaning
Independent samplesTwo groups with no link between individual observations
Paired dataObservations naturally matched one-to-one
Pooled proportionCombined success rate used for the standard error when \(H_0\!: p_1 = p_2\)
Welch t testTwo-sample t test that does not assume equal variances
Permutation testTest based on reshuffling group labels to build the null distribution
Cohen's dStandardized difference in means (difference over pooled SD)
11.9

Practice