Book contents

STATHARBOR ANALYTICS · INTRODUCTION TO STATISTICS · CHAPTER 10

Comparing Two Groups

Many scientific questions involve comparing two groups: treatment vs control, before vs after, male vs female. This chapter discusses statistical methods for comparing means and proportions across two independent groups and paired data.

Difference of Means for Independent Samples

Suppose X1,…,XnXX_1,\dots,X_{n_X} and Y1,…,YnYY_1,\dots,Y_{n_Y} are independent samples from populations with means μX\mu_X and μY\mu_Y and variances σX2\sigma_X^2 and σY2\sigma_Y^2. We wish to test H0:μX=μYH_0: \mu_X = \mu_Y and estimate the difference Δ=μX−μY\Delta = \mu_X - \mu_Y.

Equal Variances

If we assume σX2=σY2=σ2\sigma_X^2 = \sigma_Y^2 = \sigma^2, the pooled standard error is sp=(nX−1)sX2+(nY−1)sY2nX+nY−2.s_p = \sqrt{\frac{(n_X-1)s_X^2 + (n_Y-1)s_Y^2}{n_X + n_Y - 2}}.

The test statistic for testing H0H_0 and constructing confidence intervals is t=(X‾−Y‾)−0sp1/nX+1/nY,t = \frac{(\bar{X} - \bar{Y}) - 0}{s_p \sqrt{1/n_X + 1/n_Y}}, which follows a tt distribution with nX+nY−2n_X + n_Y - 2 degrees of freedom under H0H_0.

Unequal Variances (Welch’s Test)

If the variances differ, use the Welch t-test with statistic t=X‾−Y‾sX2/nX+sY2/nY,t = \frac{\bar{X} - \bar{Y}}{\sqrt{s_X^2/n_X + s_Y^2/n_Y}}, and approximate degrees of freedom ν=(sX2/nX+sY2/nY)2(sX2/nX)2nX−1+(sY2/nY)2nY−1.\nu = \frac{\bigl(s_X^2/n_X + s_Y^2/n_Y\bigr)^2}{\frac{(s_X^2/n_X)^2}{n_X-1} + \frac{(s_Y^2/n_Y)^2}{n_Y-1}}.

Paired Samples

In paired studies, each subject provides two measurements (e.g., before and after treatment). Let Di=Xi−YiD_i = X_i - Y_i be the difference for subject ii. Then analyse the differences as a single sample: t=D‾sD/n,t = \frac{\bar{D}}{s_D/\sqrt{n}}, where D‾\bar{D} and sDs_D are the mean and standard deviation of the differences.

Difference of Proportions

Let pXp_X and pYp_Y be proportions of success in two independent samples of sizes nXn_X and nYn_Y. The pooled proportion under H0:pX=pYH_0: p_X = p_Y is p̂=X+YnX+nY,\hat{p} = \frac{X + Y}{n_X + n_Y}, and the standard error of p̂X−p̂Y\hat{p}_X - \hat{p}_Y is SE=p̂(1−p̂)(1nX+1nY).\text{SE} = \sqrt{\hat{p}(1-\hat{p})\left( \frac{1}{n_X} + \frac{1}{n_Y}\right)}. The test statistic is z=p̂X−p̂YSE.z = \frac{\hat{p}_X - \hat{p}_Y}{\text{SE}}. Approximate confidence intervals for pX−pYp_X - p_Y can be constructed using similar formulae, or exact methods (e.g. Fisher’s exact test) when sample sizes are small.

Example: Weight Loss Study

Twenty individuals are randomly assigned to Diet A (nA=10n_A=10) or Diet B (nB=10n_B=10). After 8 weeks, the weight changes (in kg) are recorded. We wish to compare the mean weight loss.

Worked example

Two-sample t-test in Python.

import scipy.stats as stats

diet_a = [3.2, 2.8, 4.1, 3.5, 2.9, 3.0, 3.3, 3.7, 2.5, 3.1]
diet_b = [1.8, 2.0, 1.5, 2.2, 2.1, 1.9, 2.3, 2.4, 1.7, 2.0]

# Welch's t-test (unequal variances)
tstat, p_val = stats.ttest_ind(diet_a, diet_b, equal_var=False)
print(tstat, p_val)

In R, use ‘t.test(diet_a, diet_b, var.equal = FALSE)‘. The output includes the test statistic, degrees of freedom and p-value.

Exercises

Practice

  1. A clinical trial compares two antihypertensive drugs. The reduction in systolic blood pressure is measured for 12 patients in each group. Perform a two-sample t-test (equal-variance and unequal-variance) using hypothetical data of your choice. Interpret the results.

  2. A matched pairs study records the cholesterol level of patients before and after a dietary intervention. Write code in R/Python/Julia to perform a paired t-test on the differences. Discuss the assumptions and advantages of the paired design.

  3. In a political poll, 240 of 400 women and 260 of 450 men support a candidate. Compute a 95% confidence interval for the difference in support. Test whether support differs by gender.

  4. For small samples (e.g. 5 successes out of 10 and 8 successes out of 10), why is Fisher’s exact test preferred over the approximate z-test for comparing proportions? Perform both tests in R or Python.

References