BCompare groups · Guide test 008
One sample Student t test
Tests a mean against a specified value when the population variance is estimated.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- One-sample t-test (or z test) — Does the mean differ from a known or hypothesized value?
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 · One-sample t test
The population mean the data come from.
The value H₀ says the mean equals.
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 = .126).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- the sample mean
- the reference value
- the sample SD
- degrees of freedom of the t reference
The verdict
t(14) = 1.63, p = .126, d = 0.42
p > α = .050: compatible with H₀
Effect size: Cohen's d d = 0.420
Like z, but s replaces σ; the extra uncertainty is why the reference is t with n − 1 degrees of freedom and heavier tails.
- Statistic
- 1.626
- p-value
- 0.1262
One-sample t test on Sample: n = 15: t(14) = 1.63, p = .126, d = 0.42. The p-value is above α = .050, so the data are compatible with H₀.
Question and hypotheses
H0: μ=μ0; H1: μ differs in the specified direction.
Assumptions
An independent random sample from a normal population gives an exact test. Mean inference may be approximately robust in adequate samples, but strong skew and influential observations can undermine small sample calibration.
Formula
Parameters and notation
n is sample size, x̄ the mean, s² the sample variance, and μ0 the hypothesized mean.
Structure of the method
The sample mean and unbiased sample variance create a studentized mean difference. Under independent Gaussian sampling, division by the estimated standard error yields an exact t pivot with n-minus-one degrees of freedom. Confidence intervals invert the same family of tests rather than requiring a separate fitting algorithm.
Estimated and nuisance parameters
Estimate the mean and variance from the analyzed sample. The hypothesized mean and significance level are fixed inputs. The variance divisor is n-minus-one; replacing it with n without adjusting the statistic alters calibration. The reported standard deviation differs from the standard error of the mean.
Settings and design choices
Specify the target, alternative direction, confidence level, exclusion policy, and sampling unit. These are test-design choices, not learnable hyperparameters. If trimming or a transformation is scientifically motivated, predefine it and state that it may change the estimand; do not tune it to obtain significance.
Fitting and adaptation
For a revised sample, recompute both mean and variance and update degrees of freedom. A bootstrap should target a studentized mean under the relevant null, with resampling units matching sampling units. Independent replication is more informative than repeatedly changing preprocessing on the same small sample.
Sensitivity and robustness
The mean is sensitive to extreme observations, and small skewed samples may have poor t calibration. Zero or nearly zero variability makes the usual pivot undefined or unstable. Independence cannot be diagnosed from a histogram alone; cluster or temporal sampling needs a modified variance model and reference.
How to use and calibrate
Calculate t and its t distribution tail. The usual two sided interval is x̄±t1−α/2,n−1s/√n. Use at least two observations and nonzero estimated variability.
Example application
For n=25, x̄=12, s=5, and μ0=10, t=2 with 24 degrees of freedom; p is about 0.0569.
Interpretation and pitfalls
Watch out
This tests a mean, not a median. Removing outliers after looking at significance changes the analysis. A nonsignificant test does not establish equality with μ0.
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_1samp(x,5))
Output
TtestResult(statistic=np.float64(1.5434872662825787), pvalue=np.float64(0.15710800088610902), df=np.int64(9))
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,mu=5))
Output
One Sample t-test
data: x
t = 1.5435, df = 9, p-value = 0.1571
alternative hypothesis: true mean is not equal to 5
95 percent confidence interval:
4.860316 5.739684
sample estimates:
mean of x
5.3Sources
- R Student t testsstat.ethz.ch
- t.teststat.ethz.ch
- 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 008, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT008.