Skip to content

BCompare groups · Guide test 009

Independent samples Student t test

Compares two means using a pooled variance estimate and an equal variance assumption.

Runs in CodeSplashPython example validatedR example validated

Do it in CodeSplash

In the app, set it up in Analyze with this method:

Do it in CodeSplash Run this test on your own data, in Python or R.

Bench

Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.

Bench · Independent-samples Student t test

20
20
0.60
1.0
1.0
0.050

Alternative

The statistic's distribution if the design's true effect is real.

Your data

Drag a point or use ↑/↓ to change it
Group 1 · n = 20 · mean 0.83Group 2 · n = 20 · mean 0.04

If H₀ were true

t = 2.34
  1. The reference: how t varies across samples when H₀ is true.
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = .025).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

t=xˉ1−xˉ2sp1/n1+1/n2,ν=n1+n2−2t = \dfrac{\bar x_1-\bar x_2}{s_p\sqrt{1/n_1+1/n_2}},\qquad \nu = n_1+n_2-2

With your numbers

t=0.83−0.041.071/20+1/20=2.34,ν=38t = \dfrac{0.83-0.04}{1.07\sqrt{1/20+1/20}} = 2.34,\qquad \nu = 38
xˉ1,xˉ2\bar x_1, \bar x_2
the group means
sps_p
the pooled SD (assumes equal variances)
n1,n2n_1, n_2
the group sizes

The verdict

t(38) = 2.34, p = .025, d = 0.74

p ≤ α = .050: reject H₀

Effect size: Cohen's d d = 0.740

Pooling the SDs is only right when the variances match; unequal SDs with unequal sizes miscalibrate it. Build the null by simulation with sd2 ≠ sd1 and unequal n to see.

Statistic
2.341
p-value
0.0246
Independent-samples Student t test on Group 1: n = 20; Group 2: n = 20: t(38) = 2.34, p = .025, d = 0.74. The p-value is below α = .050, so the data are hard to reconcile with H0.

Independent-samples Student t test on Group 1: n = 20; Group 2: n = 20: t(38) = 2.34, p = .025, d = 0.74. The p-value is below α = .050, so the data are hard to reconcile with H₀.

Question and hypotheses

H0: μ1−μ2=Δ0; H1: the means differ from that contrast.

Assumptions

Independent groups and independent observations; normal population errors for exact inference; equal population variances. The common variance assumption is substantive, not established merely by a nonsignificant variance test.

Formula

sp2=(n1−1)s12+(n2−1)s22n1+n2−2,t=xˉ1−xˉ2−Δ0sp1/n1+1/n2s_p^2=\frac{(n_1-1)s_1^2+(n_2-1)s_2^2}{n_1+n_2-2},\qquad t=\frac{\bar x_1-\bar x_2-\Delta_0}{s_p\sqrt{1/n_1+1/n_2}}

Parameters and notation

sp² is pooled variance; n1,n2 are sample sizes; ν=n1+n2−2 is the reference t degrees of freedom.

Structure of the method

Compute two means and a common within-group variance estimate weighted by residual degrees of freedom. Standardize their difference by the pooled standard error and compare with t having n1-plus-n2-minus-two degrees of freedom. It is equivalent to a two-level classical one-way ANOVA for a two-sided comparison.

Estimated and nuisance parameters

Both group means and one common population variance are estimated. Sample sizes determine the pooled variance weights; they are not evidence that the two population variances are equal. The target mean difference, grouping, and alternative direction are specified independently of the observed data.

Settings and design choices

Set equal-variance modeling, null difference, sidedness, alpha, and the analysis population in the protocol. A preliminary variance test followed by choosing pooled or Welch testing creates a data-dependent two-stage procedure. Prefer a defensible design choice or a robustly justified default instead of treating that pretest as proof.

Fitting and adaptation

Refit the group summaries and pooled variance whenever rows or transformations change. For several planned contrasts, extend the common linear model and its simultaneous inference rather than rerunning unadjusted t tests. If a new population has different group variability, reconsider pooling and recalibrate accordingly.

Sensitivity and robustness

Unequal variances are especially problematic when sample sizes are unequal and the smaller group is more variable. Heavy tails and influential cases can also affect the mean and pooled scale. Correlated pairs require a paired analysis; equal marginal variances do not rescue an independence violation.

How to use and calibrate

Estimate the pooled noise variance, standardize the mean contrast, and use tν. Report the unstandardized difference and interval, optionally adding a standardized effect size.

Example application

Compare two independent groups measured on the same quantitative scale when a common population variance is defensible.

Interpretation and pitfalls

Watch out

Unequal variances combined with unequal sample sizes can seriously distort error rates. Welch is generally preferable if the equal variance assumption is doubtful; do not select the test solely by a preliminary variance p value.

What changed in edition 2

  • New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.

Python and R

Two-sided test; report the estimated mean or mean difference and its confidence interval alongside the p-value.

Python · native

Validated — Executed successfully in the validation environment · needs numpy, scipy

import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
x = np.array([4.2,5.1,5.4,4.8,6.0,5.8,4.9,5.6,6.2,5.0])
y = np.array([4.0,4.4,4.8,5.0,5.1,4.3,4.7,4.6,5.2,4.9])
print(stats.ttest_ind(x,y,equal_var=True))
Output
TtestResult(statistic=np.float64(2.6255698469620965), pvalue=np.float64(0.01715084426037767), df=np.float64(18.0))
R

Validated — Executed successfully in the validation environment · needs Base/standard library

x <- c(4.2,5.1,5.4,4.8,6.0,5.8,4.9,5.6,6.2,5.0)
y <- c(4.0,4.4,4.8,5.0,5.1,4.3,4.7,4.6,5.2,4.9)
print(t.test(x,y,var.equal=TRUE))
Output
	Two Sample t-test

data:  x and y
t = 2.6256, df = 18, p-value = 0.01715
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 0.1198935 1.0801065
sample estimates:
mean of x mean of y 
      5.3       4.7

Sources

Statistics guide v4 (historical):
R: Executed successfully in the validation environment
Reference's record:
Edition 2 review: Python: executed · R: not executed (Rscript unavailable). Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.

From the CodeSplash statistics guide (v4), entry 009, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT009.