- Statistics can help us quantify how two variables are related.
- Simple linear regression uses one explanatory variable to predict one response.
- Our example: Can vehicle weight predict fuel efficiency?
mtcars, containing 32 cars.mtcars, containing 32 cars.mpg, miles per US gallon (fuel efficiency).wt, vehicle weight in thousands of pounds.| Weight (1,000 lb) | MPG | |
|---|---|---|
| Mazda RX4 | 2.62 | 21.0 |
| Mazda RX4 Wag | 2.88 | 21.0 |
| Datsun 710 | 2.32 | 22.8 |
| Hornet 4 Drive | 3.21 | 21.4 |
| Hornet Sportabout | 3.44 | 18.7 |
We model the average response as a straight line:
\[ Y_i = \beta_0 + \beta_1 X_i + \varepsilon_i \]
## `geom_smooth()` using formula = 'y ~ x'
We choose the line that minimizes the sum of squared residuals:
\[ \min_{b_0,b_1}\sum_{i=1}^{n}\left(y_i-b_0-b_1x_i\right)^2 \]
For these data, the fitted line is:
\[ \widehat{\mathrm{mpg}} = 37.29 - 5.34\,\mathrm{wt} \]
Interpretation: Each additional 1,000 lb is associated with about 5.34 fewer MPG, on average.
Hover over each point to see the car model, weight, and MPG.
The coefficient of determination, \(R^2\), describes the fraction of variation in the response explained by the model.
\[ R^2 = 1 - \frac{\sum_i (y_i-\hat y_i)^2}{\sum_i(y_i-\bar y)^2} \]
For our example, \(R^2 = 0.753\) — approximately 75.3% of the variation in MPG is explained by a linear relationship with weight.
Residuals are observed minus predicted values: \(e_i=y_i-\hat y_i\).
library(ggplot2)
fit <- lm(mpg ~ wt, data = mtcars)
summary(fit)
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point(color = "#8C1D40", size = 3) +
geom_smooth(method = "lm", se = TRUE,
color = "#147D86") +
labs(x = "Weight (1,000 lb)", y = "MPG") +
theme_minimal()
Suppose a car weighs 3,000 lb, so its wt value is 3.0.
\[ \widehat{\mathrm{mpg}} = 37.29 - (5.34)(3.0) \approx 21.25 \]
The model predicts approximately 21.3 MPG for a car weighing 3,000 lb.
mtcars.mtcars dataset. Analysis: lm(mpg ~ wt).