Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 57 ยท Hypothesis Testing

t-Tests: One-Sample, Two-Sample and Paired

Learn the one-sample, two-sample (Welch) and paired t-tests: hypotheses, the t statistic, degrees of freedom, assumptions and effect size, with runnable Python examples.

The t-test is the everyday workhorse for comparing means when the population standard deviation is unknown (which is nearly always). It works like the z-test but uses the sample standard deviation and the t distribution to account for the extra uncertainty. There are three versions: one sample against a fixed value, two independent groups, and paired measurements.

One-sample t-test

Tests whether a population mean equals a claimed value mu0.

TEXT
t = (x-bar - mu0) / (s / sqrt(n)),   df = n - 1

Example: a manufacturer claims its battery lasts 10 hours. Does a sample disagree?

Python One-sample t-test

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two-sample t-test (independent groups)

Compare the means of two unrelated groups. H0: mu1 = mu2.

TEXT
t = (x-bar1 - x-bar2) / sqrt(s1^2 / n1 + s2^2 / n2)

There are two flavors. The classic pooled (Student) test assumes equal variances and uses df = n1 + n2 - 2. Welch's t-test does not assume equal variances and uses an adjusted (non-integer) df. Welch's test loses almost nothing when variances are equal, so it is the safer default; in SciPy use equal_var=False.

Python Student versus Welch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Paired t-test

When measurements come in natural pairs (the same people before and after, two tests on the same device, twins), analyze the differences. Compute d = after - before for each pair, then run a one-sample t-test on d against 0.

TEXT
t = d-bar / (s_d / sqrt(n)),   df = n - 1

Pairing removes the person-to-person variation and often gives far more power than treating the groups as independent.

Python Paired versus independent analysis

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The paired test shows a clear effect; ignoring the pairing hides it behind the large spread between people.

Assumptions and checks

  1. Random or representative sampling, and independent observations (within each group, and between groups for the independent test).
  2. Approximately normal data (or for the paired test, normal differences). With n above about 30 per group the CLT makes the test robust. For small samples with obvious skew or outliers, use a nonparametric alternative: Wilcoxon signed-rank (paired), Mann-Whitney U (independent), or a permutation test.
  3. For the Student version: equal variances (check with Levene's test, or just use Welch).
Python Checking assumptions and a nonparametric backup

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Effect size

A p-value does not tell you how large a difference is. Cohen's d divides the mean difference by a pooled standard deviation. Rough labels: 0.2 small, 0.5 medium, 0.8 large (use them cautiously; context matters).

Python Cohen's d

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

flowchart TD
    A["Compare means"] --> B{"How many groups and how related?"}
    B -- "One sample vs a value" --> C["One-sample t"]
    B -- "Two independent groups" --> D["Welch t (or Student if variances equal)"]
    B -- "Same subjects twice" --> E["Paired t"]
    B -- "Three or more groups" --> F["ANOVA"]

Choosing the right t-test.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Ten patients have blood pressure measured before and after a drug. Which test fits?

    The two measurements come from the same patients, so analyze the differences with a paired test.
  2. 2. Which test is safest when the group variances may differ?

    Welch's test does not assume equal variances.
  3. 3. What are the degrees of freedom for a one-sample t-test with n = 20?

    df = n - 1 = 19.
  4. 4. A tiny p-value in a huge study can still correspond to:

    Large samples detect tiny differences, so always check the effect size.

Summary

  • One-sample, two-sample and paired t-tests all use t = (difference) / (standard error) with the t distribution.
  • Prefer Welch's version for independent groups; use the paired test whenever data come in matched pairs.
  • Check normality and outliers, and fall back to Wilcoxon, Mann-Whitney or permutation tests for small, skewed data.
  • Report an effect size and a confidence interval, not only the p-value.

Next: Chi-Square Tests