The t-test compares a mean with a fixed value, or two means with each other. W. S. Gosset made it in 1908 for the small samples of a brewery in his paper with the pen name “Student” (Chapter 1). A small sample needs a different reference curve from a large one, and the simulation in the intuitive solution shows the reason.
4.1 The problem
A trial randomises patients to a new antihypertensive drug or to a placebo. After 8 weeks we measure systolic blood pressure (SBP) in mmHg. The trial is small, with 12 patients in each group. The data are simulated. In the simulation, some patients respond to the drug and some do not.
set.seed(801)n <-12placebo <-rnorm(n, mean =140, sd =10)drug <-c(rnorm(7, mean =141, sd =4), rnorm(5, mean =116, sd =4)) # 7 non-responders, 5 respondersbp <-tibble(group =factor(rep(c("Placebo", "Drug"), each = n), levels =c("Placebo", "Drug")),sbp =c(placebo, drug))bp |>group_by(group) |>summarise(n =n(), mean =mean(sbp), median =median(sbp), sd =sd(sbp))
# A tibble: 2 × 5
group n mean median sd
<fct> <int> <dbl> <dbl> <dbl>
1 Placebo 12 140. 140. 8.59
2 Drug 12 131. 133. 13.3
Did the drug lower SBP?
The mean SBP is 140.2 mmHg with placebo and 131.3 mmHg with the drug. The drug group is lower by 8.9 mmHg. The standard deviations (SDs) are 8.6 and 13.3 mmHg. The difference is about as large as the placebo SD and smaller than the drug SD, so the two groups overlap a lot.
4.2 Intuitive solution
4.2.1 A plot of the data
A boxplot shows the median, the quartiles and the range. It does not show the mean, the number of patients or the shape of the distribution. A violin plot with the points shows all three.
Show code
cols <-c(Placebo ="grey60", Drug ="steelblue")p_box <-ggplot(bp, aes(group, sbp, fill = group)) +geom_boxplot(width =0.5, alpha =0.6) +scale_fill_manual(values = cols, guide ="none") +labs(x =NULL, y ="SBP at 8 weeks (mmHg)", title ="Boxplot")p_violin <-ggplot(bp, aes(group, sbp, fill = group)) +geom_violin(alpha =0.4, trim =FALSE) +geom_jitter(width =0.06, size =1.8, alpha =0.8) +stat_summary(fun = mean, geom ="point", shape =23, size =3.5, fill ="white") +scale_fill_manual(values = cols, guide ="none") +labs(x =NULL, y =NULL, title ="Violin plot")p_box + p_violin
Figure 4.1: Simulated SBP at 8 weeks as a boxplot (left) and as a violin plot with each patient as a point (right). The white diamond is the group mean. The drug group has two clusters.
The boxplot suggests that the drug group is a little lower. The violin plot also shows the shape. Most patients on the drug look like the placebo patients, but a small group has a much lower SBP. These responders pull the mean of the drug group down.
4.2.2 The gap compared with the noise
A gap in means has a meaning only when we compare it with its uncertainty. The standard error (SE) of a mean is \(s/\sqrt{n}\), where \(s\) is the sample SD. For two independent groups,
The ratio of the gap to its SE tells us how many standard errors apart the means are.
se <-sqrt(var(drug) / n +var(placebo) / n)t_obs <- diff_mean / sec(difference = diff_mean, se = se, ratio = t_obs)
difference se ratio
-8.872135 4.581668 -1.936442
The means are 1.94 standard errors apart. A large-sample z-test compares this ratio with the standard normal curve and uses the limit 1.96. We divided by \(s\), an estimate from 12 patients, and the true SD \(\sigma\) is unknown.
4.2.3 A simulation under the null hypothesis
We can find the correct reference curve by simulation. We take many samples of size 5 from a normal population with mean \(\mu = 0\) and SD \(\sigma = 1\). Here the null hypothesis is true. For each sample we calculate two ratios. The first uses the true \(\sigma\). The second uses the sample SD \(s\), as we must do with real data.
Figure 4.2: The ratio (mean − μ)/(s/√n) for 20,000 samples of size 5 from N(0, 1), so the null hypothesis is true. The histogram follows the t curve with 4 degrees of freedom (red). The normal curve (blue, dashed) is too low in the tails.
The normal curve fits well in the middle. In the tails, it is too low. Now suppose we use the normal limit 1.96 when the null hypothesis is true.
With the true \(\sigma\), the limit 1.96 rejects the null hypothesis in 5% of samples, as it must. With the sample SD \(s\), the same limit rejects it in 12.2% of samples. This rate is above the nominal 5%. The t limit of 2.78 brings the rate back to 4.9%.
4.3 Formal solution
4.3.1 The t statistic
Suppose \(x_1, \dots, x_n\) are independent draws from a normal distribution with mean \(\mu\). Let \(\bar{x}\) be the sample mean and \(s\) the sample SD with divisor \(n - 1\). Then
\[
t = \frac{\bar{x} - \mu}{s/\sqrt{n}}
\]
follows a t distribution with \(n - 1\)degrees of freedom (df). The df count the independent pieces of information in \(s\). The \(n\) deviations \(x_i - \bar{x}\) always add up to 0, so only \(n - 1\) of them are free.
The test assumes that the observations are independent and that each group comes from a normal distribution. For a large sample the normality assumption matters less, because the sample mean is approximately normal (the central limit theorem).
4.3.2 Why the tails are fat
With the true \(\sigma\), the ratio \((\bar{x} - \mu)/(\sigma/\sqrt{n})\) is exactly standard normal. With \(s\) in place of \(\sigma\), the denominator is also random. In some samples \(s\) is much smaller than \(\sigma\). A small denominator gives a large ratio, and these large ratios make the tails fat.
With \(n = 5\) and \(\sigma = 1\), the sample SD is below 0.5 in 8.9% of samples. In each of these samples, the ratio is more than twice as large as the ratio with \(\sigma\).
4.3.3 Three forms of the test
The one-sample t-test compares one mean with a fixed value \(\mu_0\). It uses the formula above with \(\mu = \mu_0\) and \(n - 1\) df. In R: t.test(x, mu = mu0).
The paired t-test is for paired data, for example the same patient before and after treatment. Calculate the difference \(d_i\) inside each pair. Then do a one-sample t-test of \(H_0: \text{mean}(D) = 0\) on the \(d_i\), with \(n - 1\) df, where \(n\) is the number of pairs. In R: t.test(x, y, paired = TRUE). Student’s own example, the sleep data, is a paired t-test (Chapter 1).
The two-sample t-test is for two independent groups. There are two versions.
Student’s version assumes that the two groups have the same variance. It pools the two SDs:
with \(n_1 + n_2 - 2\) df. In R: t.test(x, y, var.equal = TRUE).
Welch’s version does not assume equal variances. It uses the SE of the intuitive solution, and it approximates the df with the Welch–Satterthwaite formula:
The df \(\nu\) is usually not a whole number. t.test(x, y) uses Welch’s version by default.
4.3.4 The test on our data
Our two groups are independent, so we use the two-sample test. We do the Welch test first.
welch <-t.test(drug, placebo)welch
Welch Two Sample t-test
data: drug and placebo
t = -1.9364, df = 18.774, p-value = 0.06801
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-18.4694808 0.7252104
sample estimates:
mean of x mean of y
131.2800 140.1522
Welch Two Sample t-test: the default two-sample test in R.
t: -1.936. This is the ratio that we calculated by hand, -1.936.
df: 18.77, from the Welch–Satterthwaite formula. Student’s version uses \(12 + 12 - 2 = 22\).
p-value: 0.068. This is the area in both tails of the t distribution beyond \(\pm 1.94\).
95 percent confidence interval: the interval for the difference in means (Drug minus Placebo), -18.5 to 0.7 mmHg.
Student’s version gives p = 0.0658 with 22 df. With equal group sizes, the two versions have the same t statistic. Only the df differ. The normal curve gives p = 0.0528. This is smaller than the t-test p-value, because the normal curve has thinner tails.
Show code
df_w <-unname(welch$parameter)grid <-tibble(x =seq(-5, 5, length.out =1000), t =dt(x, df_w), normal =dnorm(x))ggplot(grid, aes(x)) +geom_area(data =filter(grid, x <=-abs(t_obs)), aes(y = t), fill ="firebrick", alpha =0.4) +geom_area(data =filter(grid, x >=abs(t_obs)), aes(y = t), fill ="firebrick", alpha =0.4) +geom_line(aes(y = t), colour ="firebrick", linewidth =1) +geom_line(aes(y = normal), colour ="steelblue", linetype ="dashed") +geom_vline(xintercept = t_obs, colour ="grey30") +labs(x ="t", y ="density")
Figure 4.3: The observed t statistic (vertical line) on the t distribution with the Welch df (red). The shaded area is the two-sided p-value. The dashed blue curve is the standard normal.
The p-value is 0.068, which is above 0.05. The confidence interval contains 0. At the 5% level, the data do not give enough evidence that the two means differ.
4.3.5 How the t distribution approaches the normal
As \(n\) increases, \(s\) becomes a more accurate estimate of \(\sigma\). The t distribution then comes closer to the normal distribution.
Figure 4.4: The t distribution for 1, 3, 10 and 30 degrees of freedom, and the standard normal (dashed). As the df increase, the tails become thinner and the curve approaches the normal.
With 1 df, the 97.5% point is 12.71. With 4 df, it is 2.78. With 29 df, it is 2.05, and with 99 df it is 1.98. The normal value is 1.96. For a sample of a few hundred, the t-test and the z-test give almost the same answer. For a sample of 5, they differ.
The simulation of the intuitive solution shows this. We repeat it for several sample sizes and use the normal limit 1.96 each time.
With \(n = 3\) the normal limit rejects a true null hypothesis in 19% of samples. With \(n = 100\) the rate is 5.1%, close to 5%.
4.4 Clusters and outliers
The t-test compares means. In our data, the drug group has two clusters, and its mean falls between them. The t-test assumes a normal distribution in each group. With 12 patients in each group, the central limit theorem gives little protection. A rank test (Chapter 5, Chapter 6) does not need the normal assumption. A rank test asks a different question from the t-test, so the two tests can disagree on the same data.
The t-test is also sensitive to outliers. One extreme value changes both the mean and \(s\). Plot the data, as in Figure 4.1, before you choose a test.
4.5 Summary
The t statistic is the difference in means divided by its standard error, which uses the sample SD \(s\).
The t distribution has fatter tails than the normal, because \(s\) is itself noisy. The normal limit 1.96 gives too many false positives in small samples.
The paired t-test is the test for paired data, and the Welch two-sample test (the R default) is the test for independent groups.
The t distribution approaches the normal as the df increase.