Theoretical Background: Confidence Intervals for Two Independent Samples
When comparing the means of two independent populations (e.g., comparing the effectiveness of a new drug against a placebo, or academic performance of students from two different teaching methods), we often want to estimate the difference between their population means, \(\mu_1 - \mu_2\). A confidence interval provides a range of plausible values for this difference.
From Raw Data to Summary Statistics
Given raw data for each sample, the calculator first computes the sample mean and the sample standard deviation for each group:
These summary statistics are then used exactly as they would be in a summary-data confidence interval calculation.
Checking for Equality of Variances (F-Test)
An important consideration is whether the variances of the two populations (\(\sigma_1^2\) and \(\sigma_2^2\)) can be assumed to be equal. This assumption affects the formula used for the confidence interval. An F-test is commonly used to check this assumption.
The F-statistic is calculated as the ratio of the sample variances, with the larger variance in the numerator to simplify p-value calculation:
If the p-value of the F-test is less than the chosen significance level (e.g., \(\alpha = 0.05\)), we reject \(H_0\) and conclude the variances are likely different.
Case 1: Population Variances Assumed Equal (Pooled t-Interval)
If we assume \(\sigma_1^2 = \sigma_2^2\), we calculate a pooled estimate of the common variance, \(s_p^2\):
The confidence interval uses \(df = n_1+n_2-2\) degrees of freedom:
Case 2: Population Variances Not Assumed Equal (Welch's t-Interval)
If we cannot assume \(\sigma_1^2 = \sigma_2^2\), Welch's t-interval is more appropriate. The degrees of freedom are approximated by the Welch-Satterthwaite equation:
Welch's method is generally robust and recommended if there is doubt about the equality of variances.
Interpreting the Confidence Interval
A \( (1-\alpha) \times 100\% \) confidence interval for \(\mu_1 - \mu_2\) is interpreted as: "We are \( (1-\alpha) \times 100\% \) confident that the true difference between the population means lies between the lower bound and the upper bound of the interval." If the interval contains zero, there is not enough evidence to conclude a significant difference; if it does not, the difference is statistically significant at that confidence level.
Worked Example
Using the default data above: Sample 1 has n=10, mean ≈ 23.98, sd ≈ 1.42; Sample 2 has n=10, mean ≈ 20.16, sd ≈ 0.86. Assuming equal variances, the pooled standard error works out to about 0.524, giving a 95% CI for the difference of roughly [2.62, 5.02]. Since this interval is entirely positive and does not contain zero, there is statistically significant evidence that the mean of Sample 1 is higher than that of Sample 2.
How to Do This in R
sample1 <- c(23.1, 25.4, 22.8, 24.0, 26.1, 21.9, 24.7, 25.9, 23.5, 22.4)
sample2 <- c(20.5, 19.8, 21.2, 20.0, 18.9, 21.6, 19.4, 20.8, 19.1, 20.3)
# Welch's t-interval (default)
t.test(sample1, sample2, conf.level = 0.95)
# Pooled t-interval (if variances assumed equal)
t.test(sample1, sample2, var.equal = TRUE, conf.level = 0.95)
How to Do This in Python
from scipy import stats
import numpy as np
sample1 = [23.1, 25.4, 22.8, 24.0, 26.1, 21.9, 24.7, 25.9, 23.5, 22.4]
sample2 = [20.5, 19.8, 21.2, 20.0, 18.9, 21.6, 19.4, 20.8, 19.1, 20.3]
result = stats.ttest_ind(sample1, sample2, equal_var=False) # Welch's
ci = result.confidence_interval(confidence_level=0.95)
print(ci.low, ci.high)
How to Do This in SPSS
Analyze → Compare Means → Independent-Samples T Test. Move your outcome variable into "Test Variable(s)" and your group variable into "Grouping Variable." SPSS automatically runs Levene's Test for Equality of Variances alongside both the pooled and Welch's t-test confidence intervals side by side.
How to Do This in JASP
T-Tests → Independent Samples T-Test. Move your outcome into the Dependent Variable box and your group variable into the Grouping Variable box. Under Assumption Checks, enable Equality of variances (Levene's), and under Statistics enable Confidence Interval.
Assumptions for Two-Sample t-Procedures
- Independence of Samples: The two samples must be independent of each other.
- Random Sampling: Data in each sample should be drawn randomly from their respective populations.
- Normality: Both populations should be approximately normally distributed, OR both sample sizes should be sufficiently large (e.g., \(n_1 \ge 30\) and \(n_2 \ge 30\)) for the Central Limit Theorem to apply. t-procedures are reasonably robust to violations of normality, especially with larger sample sizes.