Method · Statistics Library

Two-sample t-test

The two-sample t-test asks whether two independent groups differ in their population means, given how much the observations vary within each group.

Chapter 4: Tests, estimates and comparisonsReading 4.5

Understand the idea

You have one measured number (body mass, wait time, yield) and one column that splits the rows into exactly two groups. You want to know whether the two group averages are far enough apart to take seriously.

'Far enough apart' has to be judged against the spread. A 200 g gap is convincing if every bird in a group is within 50 g of its own average, and meaningless if birds vary by 800 g. The t statistic is exactly that comparison: the gap between the averages divided by how much the gap itself would bounce around from sample to sample.

A big t (far from zero) means the groups are far apart relative to the noise. KernelStats turns t into a p-value, and also reports the difference in the original units with a confidence interval, which is the part you should actually quote, because it says how big the difference is, not just that it exists.

KernelStats runs the Welch t-test by default, a version that does not assume the two groups have the same spread; an option switches to the classic Student's t-test, which does assume equal spreads. This test is also called the independent samples t-test, because the two groups contain different, unrelated individuals. Use it when you want to compare two means from two separate groups.

Practitioner explanation

KernelStats runs Welch's unequal-variance t by DEFAULT (the equal-variances option off), with Satterthwaite degrees of freedom. This is deliberate: Welch matches Student's t closely when variances are equal, and holds its nominal Type I error rate when they are not, so pre-testing variances and then choosing a test, which inflates the error rate of the whole procedure, is unnecessary. Setting equal_var = true switches to Student's pooled-variance t with nu = n1 + n2 - 2, and every surface (degrees of freedom, the CI, the APA sentence, all four code exports) follows the switch.

Independence is the assumption that cannot be repaired after the fact; paired or clustered data need the paired t or a mixed model, not this test. Normality matters chiefly through the tails at small n: the CLT makes the test robust for moderate n with mild skew, but strong skew or influential outliers at small n distort both the p-value and the interval, and the rank-based Mann-Whitney is the usual fallback.

The card is also stamped by the independent double-run verifier for the default Welch path: t, df, p and the interval are recomputed through a second, separate route and disagreement suppresses the verification badge rather than being silently smoothed.

When to use it

Consider it when

One continuous response, one grouping column with exactly two independent groups, and a question about a difference in means.

Use caution when

When the two columns are repeated measures on the same units (use the paired t), when there are three or more groups (use one-way ANOVA rather than several t-tests), when the response is ordinal or heavily skewed at small n (use Mann-Whitney), or when observations are clustered or repeated (use a mixed model).

Formulas and symbols

Welch's t statistic

t=xˉ1−xˉ2s12n1+s22n2t = \frac{\bar{x}_1 - \bar{x}_2}{\sqrt{\dfrac{s_1^2}{n_1} + \dfrac{s_2^2}{n_2}}}

x-bar are the group means, s the group standard deviations, n the group sizes.

Satterthwaite degrees of freedom

ν=(s12n1+s22n2)2(s12/n1)2n1−1+(s22/n2)2n2−1\nu = \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}}

Generally non-integer; this is why Welch output shows fractional degrees of freedom.

Pooled standard deviation (equal-variance option)

sp=(n1−1)s12+(n2−1)s22n1+n2−2s_p = \sqrt{\frac{(n_1-1)s_1^2 + (n_2-1)s_2^2}{n_1 + n_2 - 2}}

Used when equal_var = true, and always used as the denominator of Cohen's d.

Difference in means

xˉ1−xˉ2\bar{x}_1 - \bar{x}_2

Reported in the original measurement units: the quantity to quote.

Assumptions and checks

  • Independence of observations. Standard errors are too small, so p-values are too small and intervals too narrow. This cannot be fixed by a different two-sample test; the design must be modeled (paired t, mixed model).

    In KernelStats: Not checkable from the data alone: it is a property of how the data were collected.

  • Normality (of residuals, not of the raw data). At small n, p-values and interval coverage drift, especially with strong skew or outliers. At moderate to large n the CLT makes the test robust.

    In KernelStats: Shapiro-Wilk and Anderson-Darling per group, reported as assumption checks on the card.

  • Homogeneity of variance (homoscedasticity). Only relevant to the equal_var = true option, where unequal variances with unequal group sizes distort the Type I error rate. The default Welch test does not require it.

    In KernelStats: Levene's / Brown-Forsythe via Equal Variances Test; not required for the default path.

Alternatives

  • Paired t-Test. The two measurements come from the same unit: before/after, matched pairs, the same subject under two conditions. Pairing must be reflected in the analysis or the standard error is wrong.
  • Mann-Whitney U Test. Normality is clearly violated at small n (strong skew or influential outliers), or the response is ordinal rather than interval.
  • One-Way ANOVA. There are three or more groups. Running all pairwise t-tests instead inflates the family-wise error rate.

Worked example

Scenario

Comparing body mass (g) between 151 Adelie and 123 Gentoo penguins, using species as the grouping column.

Result

Welch's t = -23.39 on 249.64 degrees of freedom, p < .001, mean difference -1375 g, 95% CI [-1491, -1260], Cohen's d = -2.87.

Interpretation

Gentoo penguins are heavier than Adelie by roughly 1.4 kg, and the interval places that difference between about 1.26 and 1.49 kg. The effect is very large by any conventional benchmark, so the conclusion here rests on the size of the difference, not on the p-value.

Dataset: penguins.csv

Common misconceptions

Misconception

You should test for equal variances first, then pick Student's or Welch's t.

Correction

That two-stage procedure inflates the overall Type I error rate, because the choice of test is itself data-dependent. Welch's t is the recommended default regardless: it is nearly as powerful as Student's when variances are equal and correct when they are not. KernelStats therefore defaults to Welch.

Misconception

A non-significant t-test shows the two groups are the same.

Correction

It shows the data do not distinguish them. Read the confidence interval for the difference: if it spans values from trivial to large, the study was simply not precise enough to conclude anything. Demonstrating equivalence requires an equivalence test with a pre-specified margin.

Read more: Misconception: the p-value is the probability the null hypothesis is true

References

  1. NIST/SEMATECH e-Handbook of Statistical Methods, §7.3.1 Do two processes have the same mean?, NIST/SEMATECH (2012), §7.3.1.
  2. The use of Welch's t-test instead of Student's t-test as default, Delacre, M., Lakens, D. and Leys, C. (2017).