MathsStatistics & Probability

T-Test
calculator.

Test whether a mean differs from a target, or two means differ from each other.

How it's worked out

Every version of the test divides a gap by its standard error, which is how much that gap would wobble from sample to sample. A large t means the gap is big compared with that wobble. The p-value is the chance of a t at least this extreme if the true gap were zero.

One sample. Compare one mean with a target μ0\mu_0, such as a fill weight of 500 g. Here x‾\bar{x} is the sample mean, ss its standard deviation, and nn the count, with n−1n - 1 degrees of freedom.

t\displaystyle t=x‾−μ0s/n\displaystyle {}= \frac{\bar{x} - \mu_0}{s / \sqrt{n}}

Paired. Use this for the same subjects measured twice. It takes each second value minus its first, then runs the one sample test on those differences against zero. The R notes on Student's sleep data stress that this classic set is 10 people, not two groups.

Welch. Use this for two separate groups. It gives each group its own variance, and its degrees of freedom usually are not whole numbers. Call the two variance terms AA and BB.

A\displaystyle A=s12n1,\displaystyle {}= \frac{s_1^2}{n_1},B\displaystyle B=s22n2\displaystyle {}= \frac{s_2^2}{n_2}
t\displaystyle t=x‾1−x‾2A+B\displaystyle {}= \frac{\bar{x}_1 - \bar{x}_2}{\sqrt{A + B}}
ν\displaystyle \nu=(A+B)2A2n1−1+B2n2−1\displaystyle {}= \frac{(A + B)^2}{\frac{A^2}{n_1 - 1} + \frac{B^2}{n_2 - 1}}

Pooled. This is the classic Student test. It assumes the two groups share one variance, combines them into sps_p, and uses n1+n2−2n_1 + n_2 - 2 degrees of freedom.

sp2\displaystyle s_p^2=(n1−1)s12+(n2−1)s22n1+n2−2\displaystyle {}= \frac{(n_1 - 1)s_1^2 + (n_2 - 1)s_2^2}{n_1 + n_2 - 2}
t\displaystyle t=x‾1−x‾2sp1n1+1n2\displaystyle {}= \frac{\bar{x}_1 - \bar{x}_2}{s_p \sqrt{\frac{1}{n_1} + \frac{1}{n_2}}}

The interval is the gap plus or minus a critical t times the standard error. It is always two-sided at your confidence level, even for a one-tailed test. Cohen's d is the gap in standard deviation units: over the sample SD for one sample, over the pooled SD for two samples, and over the SD of the differences for paired data, where it is called dz.

Worked examples

Student's sleep drugs, tested as pairs

In Student's 1908 data, ten patients each tried two sleeping drugs, and the numbers are extra hours of sleep. The second drug gives a mean of 1.58 extra hours over the first. With a standard error of 0.389, t is 4.062 on 9 degrees of freedom.

That gives p = 0.0028, and the 95% interval for the gain runs from 0.7 to 2.46 hours. It leaves out 0, as it must when a two-tailed test at the same level rejects. At 1.28, dz is large.

Open this example in the tool

The same numbers, tested as two groups

Treat those lists as if they came from 20 different people, and Welch's test gives t = −1.861 and p = 0.079, no longer significant at 5%. The pooled version agrees, with p = 0.079. The gap in means is the same 1.58 hours, but its standard error rises from 0.389 to 0.849.

The extra wobble comes from the large differences between patients, and pairing is what removes them. Here the design, not the arithmetic, decides the conclusion. The test has to match how the data were collected.

Open this example in the tool

Ten heights against 170 cm

Ten people have a mean height of 169.7 cm, which is 0.3 cm below the target. With an SD of 4.95 cm, t is only −0.192, and p = 0.852. The 95% interval for the gap runs from −3.84 to 3.24 cm, so these data fit a true mean of 170 cm and several others nearby.

Open this example in the tool

Reading your result

Compare t with the critical value. With 9 degrees of freedom at the 5% level, a two-tailed test needs t beyond ±2.262, and a one-tailed test needs 1.833 in the chosen direction. The p-value holds the same information, in a form that does not depend on the degrees of freedom.

Use the interval to judge size. A narrow interval far from 0 is strong and useful evidence. A wide one that barely misses 0 tells you little about how big the effect is. The interval also has the units of your data, which p does not.

Read d against what matters to you. Cohen's bands call 0.20 small, 0.50 medium and 0.80 large, and Lakens warns against applying them rigidly. A very large sample can give a tiny p for a d too small to care about.

Read the sign as a direction only. Swap the two lists in the sleep data and t goes from 4.062 to −4.062, with the same p. The size of t decides significance in a two-tailed test.

Common mistakes

  • Treating paired data as independent. The sleep data show the cost: a clear result turns into a nonsignificant one. The reverse mistake, pairing unrelated lists, is worse, as it invents a link that is not there.
  • Testing variances first, then choosing. Delacre, Lakens and Leys say this two-step habit should not be used, because the variance test often has too little power to warn you. They recommend Welch as the default.
  • Choosing a one-tailed test after seeing the data. A one-tailed test puts all of α in one direction, so the sleep data's p falls from 0.0028 to 0.0014. UCLA's guidance is that picking one just to reach significance is not appropriate.
  • Running many t-tests on several groups. With several groups, every pair you test is one more chance to be fooled at the 5% level. For three or more groups, use the one-way ANOVA calculator and follow up from there.
  • Ignoring outliers in a small sample. The test works from means and SDs, and one wild value can move both. Look at the values first, and try the Mann-Whitney calculator if the data are strongly skewed.

Questions people ask

Let the design decide. Use one sample for a mean against a fixed value, paired when each value in one list belongs with a value in the other, and a two-sample test for separate groups. A paired test has no Welch version, because it works on one list of differences with one variance.

Sources