Do heavier cars tend to have lower fuel efficiency?
- We will analyze data on 32 car models.
- We’ll use two ggplot charts and an interactive Plotly chart.
- We’ll fit and interpret a simple linear regression model.
Do heavier cars tend to have lower fuel efficiency?
The built-in mtcars dataset contains data on 32 cars.
| mpg | wt | hp | |
|---|---|---|---|
| Mazda RX4 | 21.0 | 2.620 | 110 |
| Mazda RX4 Wag | 21.0 | 2.875 | 110 |
| Datsun 710 | 22.8 | 2.320 | 93 |
| Hornet 4 Drive | 21.4 | 3.215 | 110 |
| Hornet Sportabout | 18.7 | 3.440 | 175 |
| Valiant | 18.1 | 3.460 | 105 |
Our model predicts fuel efficiency from car weight:
\[ \mathrm{mpg}_i = \beta_0 + \beta_1\,\mathrm{wt}_i + \varepsilon_i \]
Residuals show the difference between observed and predicted MPG.
Rotate the interactive plot and hover over a point to explore a car.
We can test whether car weight is associated with MPG:
\[ H_0: \beta_1 = 0 \qquad H_A: \beta_1 \neq 0 \]
We also summarize fit with the coefficient of determination:
\[ R^2 = 1 - \frac{\sum_i (y_i - \hat{y}_i)^2}{\sum_i (y_i - \bar{y})^2} \]
Interpretation: In this dataset, heavier cars generally have lower MPG. The slope describes the estimated relationship; it does not establish causation.
This code builds and summarizes the model used throughout the presentation.
my_model <- lm(mpg ~ wt, data = mtcars) round(coef(my_model), 2)
## (Intercept) wt ## 37.29 -5.34
The lm() function estimates the regression intercept and slope.
Takeaway: Regression and visualization help us describe patterns, assess fit, and communicate results clearly.