BCompare groups · Guide test 009
Independent samples Student t test
Compares two means using a pooled variance estimate and an equal variance assumption.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- Two-sample t-test (Welch / Student, or z test) — Do two independent groups differ in their mean?
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
Alternative
The statistic's distribution if the design's true effect is real.
Your data
Drag a point or use ↑/↓ to change itIf H₀ were true
- The reference: how t varies across samples when H₀ is true.
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = .025).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- the group means
- the pooled SD (assumes equal variances)
- 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 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
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.
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))
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.7Sources
- SciPy independent t testsdocs.scipy.org
- scipy.stats.ttest_inddocs.scipy.org
- 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.