Motivating Example: You understand the t-distribution, and using it to estimate uncertainty and test whether a mean equals \(\mu_0\). But doing this by hand is a lot of work. If only there were an easier way 🤔. Here, we show how R can do this for us.
Learning Goals: By the end of this section, you will be able to:
Perform a one-sample t-test using the t.test() function.
Use the broom package (tidy(), augment()) to generate clean, predictable output from model objects.
Frame a one-sample t-test as a simple linear model using the lm() function.
Interpret the output of summary() and tidy() for a simple linear model.
“What does a one-sample t-test in R even mean?” you might ask, “since we just did one.” Let me explain…
Figure 1: R can make things easy! Image made with ChatGPT.
We just worked through the calculations for a confidence interval and a hypothesis test step by step. But R can do the entire calculation in a single line. Now that you know how it works and what R is doing, I can show you how to have R do this work for you.
The t.test() function is the easiest way to run a one-sample t-test in R. There are two relevant arguments:
x: A vector of observations.
mu: \(\mu_0\) – i.e. the expected value of x under the null hypothesis.
Note: Because x must be a vector, I pull() the column out of its tibble.
t.test(x =pull(range_shift,elevationalRangeShift) , mu =0)
One Sample t-test
data: pull(range_shift, elevationalRangeShift)
t = 7.1413, df = 30, p-value = 0.00000006057
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
28.08171 50.57635
sample estimates:
mean of x
39.32903
While these results match our calculations (and are faster and easier to do), there is one problem – they are a mess. They are stored in a format that is easy for humans to read, but harder for a computer to process. The tidy() function in the broom package offers a convenient format:
library(broom)t.test(x =pull(range_shift,elevationalRangeShift) , mu =0) |>tidy()
t.test() output piped through broom's tidy() function
estimate
statistic
p.value
parameter
conf.low
conf.high
method
alternative
39.33
7.14
6.06e-08
30
28.08
50.58
One Sample t-test
two.sided
The statistic here is the t-value. The statistic column will contain the value of whichever test statistic your analysis produces.
The general linear model framework in R
An alternative approach to the t.test() function is to use R’s framework for general linear models. Remember that a linear model predicts the value of the response variable for the \(i^{th}\) observation \(\hat{Y_i}\) as a function of things in our model. For a one sample t-test, our model contains only the mean, which we represent as the “intercept”, \(a\):
\[\hat{Y_i} = a\]
Actual observations (\(Y_i\)) will deviate from model predictions (\(\hat{Y_i}\)). This deviation, known as the “residual” is denoted by \(e_i\):
This may seem like overkill for a one-sample t-test, but the linear model framework generalizes to regression, ANOVA, and more. Seeing a one-sample t-test as a linear model prepares us for the models we’ll meet next.
lm() just returns the mean, but it actually calculates so much more! To access information like the standard error, t-value, p-value (reported as Pr(>|t|)), and degrees of freedom, pipe the model to summary():
lm(elevationalRangeShift ~1, data = range_shift) |>summary()
Call:
lm(formula = elevationalRangeShift ~ 1, data = range_shift)
Residuals:
Min 1Q Median 3Q Max
-58.629 -23.379 -3.529 24.121 69.271
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 39.329 5.507 7.141 0.0000000606 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 30.66 on 30 degrees of freedom
broom’s augment() function shows each observation’s actual value, predicted value (.fitted), and residual (.resid). I’m only showing the first three columns because they are the only ones we’ve covered so far, and I only show the first five rows to save space.
library(broom)lm(elevationalRangeShift ~1, data = range_shift) |>augment()|>select(1:3)|>slice_head(n =5)