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 and are independent samples from populations with means and and variances and . We wish to test and estimate the difference .
Equal Variances
If we assume , the pooled standard error is
The test statistic for testing and constructing confidence intervals is which follows a distribution with degrees of freedom under .
Unequal Variances (Welch’s Test)
If the variances differ, use the Welch t-test with statistic and approximate degrees of freedom
Paired Samples
In paired studies, each subject provides two measurements (e.g., before and after treatment). Let be the difference for subject . Then analyse the differences as a single sample: where and are the mean and standard deviation of the differences.
Difference of Proportions
Let and be proportions of success in two independent samples of sizes and . The pooled proportion under is and the standard error of is The test statistic is Approximate confidence intervals for 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 () or Diet B (). 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
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.
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.
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.
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
Altman DG. Practical Statistics for Medical Research. Chapman & Hall; 1991.
Newcombe RG. Interval estimation for the difference between independent proportions: comparison of eleven methods. Statistics in Medicine. 1998;17(8):873–890.
Cummings P. The relative merits of risk ratios and odds ratios. Archives of Pediatrics & Adolescent Medicine. 2009;163(5):438–445.