The Main Question

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.

Understanding the Data

The built-in mtcars dataset contains data on 32 cars.

  • mpg: miles per gallon (fuel efficiency)
  • wt: weight, measured in thousands of pounds
  • hp: horsepower
A sample of six vehicles
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

The Regression Equation

Our model predicts fuel efficiency from car weight:

\[ \mathrm{mpg}_i = \beta_0 + \beta_1\,\mathrm{wt}_i + \varepsilon_i \]

  • \(\beta_0\): predicted MPG when weight is zero (a mathematical intercept)
  • \(\beta_1\): predicted MPG change for an additional 1,000 pounds
  • \(\varepsilon_i\): unexplained variation

ggplot 1: Weight vs. Fuel Efficiency

ggplot 2: Residuals

Residuals show the difference between observed and predicted MPG.

Plotly: Explore Three Variables in 3D

Rotate the interactive plot and hover over a point to explore a car.

Hypothesis Test and Model Fit

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} \]

Regression Results

  • Estimated slope: -5.34 MPG per additional 1,000 pounds
  • \(R^2\): 0.753
  • Slope p-value: 1.29e-10

Interpretation: In this dataset, heavier cars generally have lower MPG. The slope describes the estimated relationship; it does not establish causation.

R Code: Fit the Linear Model

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.

Limitations and Takeaways

  • The sample contains only 32 car models, so results may not generalize to all cars.
  • Weight is not the only possible predictor; engine and vehicle design can also matter.
  • We observed a negative association between weight and fuel economy.

Takeaway: Regression and visualization help us describe patterns, assess fit, and communicate results clearly.