Understanding the t-distribution
When performing a t-test, you first calculate your t-statistic using the familiar formula:
$$ t = \frac{X - M}{SE} $$
\(X\) is the observed value, \(M\) is the expected value under the null hypothesis (or population mean), and \(SE\) is the standard error. Once you've computed the t-statistic, you then compare it to the so-called critical value, which comes from the relevant t-distribution.
The shape of a t-distribution, and thus the critical value, is determined entirely by its degrees of freedom. To demonstrate this, let's draw some density plots for t-distributions using different degrees of freedom.
This exercise is part of the course
Intro to Statistics with R: Student's T-test
Exercise instructions
- Create a vector
x
that contains a sequence of length 100 between -4 and 4. See?seq
for help. - Use
dt()
to generate t-distributions with 4, 6, 8, 10, and 12 degrees of freedom (in that order). The first argument todt()
is the vector of values at which to evalute the t-distribution (x
from above) and the second argument (df
) is the degrees of freedom. - Plot each of the t-distributions. Once the inital
plot()
is created, you'll uselines()
to plot each additional distribution. The two arguments tolines()
are the same as the first two arguments toplot()
, except that you'll have to substitute the appropriate y-values. Use the color black for 4 degrees of freedom, red for 6, orange for 8, green for 10, and blue for 12. - Add a
legend()
to your plot. The legend should be situated at the top right corner of your plot and should have the title "t-distributions". This is done by setting the first argument to"topright"
and thetitle
argument to"t-distributions"
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Generate a vector of 100 values between -4 and 4
x <- seq(___, ___, length = ___)
# Simulate the t-distribution
y_1 <- dt(x, df = ___)
y_2 <- dt(x, df = ___)
y_3 <- dt(x, df = ___)
y_4 <- dt(x, df = ___)
y_5 <- dt(x, df = ___)
# Plot the t-distributions
plot(x, y_1, type = "l", lwd = 2, xlab = "t-value", ylab = "Density",
main = "Comparison of t-distributions", col = "black")
lines(___, ___, col = "red")
lines(___, ___, col = "orange")
lines(___, ___, col = "green")
lines(___, ___, col = "blue")
# Add a legend
legend(___, c("df = 4", "df = 6", "df = 8", "df = 10", "df = 12"),
col = c("black", "red", "orange", "green", "blue"),
title = ___, lty = 1)