The question and the data

Do heavier cars tend to get fewer miles per gallon?

  • R’s built-in mtcars dataset contains 32 automobiles.
  • mpg measures fuel economy in miles per US gallon.
  • wt measures weight in thousands of pounds.
  • These historical data come from the 1974 Motor Trend US magazine.

This example describes an association. The cars were not randomly assigned different weights.

Weight and MPG

The relationship is negative, with noticeable variation around the trend.

The simple linear regression model

\[Y_i = \beta_0 + \beta_1 x_i + \varepsilon_i\]

  • \(Y_i\): fuel economy for car \(i\).
  • \(x_i\): weight of that car in thousands of pounds.
  • \(\beta_0\): intercept, the mean response at \(x=0\).
  • \(\beta_1\): change in mean MPG for a one-unit increase in \(x\).
  • \(\varepsilon_i\): variation that weight alone does not explain.

We estimate the coefficients using the observed cars.

How the line is fitted

Least squares chooses the line that minimizes squared residuals:

\[\min_{b_0,b_1}\sum_{i=1}^{n}\left[y_i-(b_0+b_1x_i)\right]^2\]

A residual is the observed value minus the fitted value:

\[e_i=y_i-\widehat{y}_i\]

A positive residual means a car gets more MPG than the line predicts.

The fitted regression line

The line summarizes the average trend within this sample.

Interpreting the coefficients

\[\widehat{\mathrm{MPG}} = 37.29 - 5.34\times\mathrm{Weight}\]

An additional 1,000 lb is associated with about 5.34 fewer MPG in the fitted model.

The intercept is 37.29 MPG, but a zero-weight car is outside the data and has no practical interpretation.

\(R^2 = 0.753\): the line explains about 75% of the sample variation in MPG.

A prediction for a 3,000 lb car

Substitute \(x=3\) into the fitted equation:

\[\widehat{\mathrm{MPG}} = 37.29 - 5.34(3) \approx 21.25\]

The predicted fuel economy is 21.25 MPG.

Under the model assumptions, a 95% prediction interval for one additional comparable car is about 14.93 to 27.57 MPG.

The interval is wider than a confidence interval for the mean because individual cars vary around the line.

Interactive view of the cars

Hover to identify a car. Drag to zoom and double-click to reset.

Checking the residuals

Some curvature remains. A straight line may miss part of the relationship.

R code for the regression plot

library(ggplot2)
cars <- datasets::mtcars
fit <- lm(mpg ~ wt, data = cars)

ggplot(cars, aes(x = wt, y = mpg)) +
  geom_point(size = 2.8, color = "black") +
  geom_smooth(method = "lm", formula = y ~ x,
              se = FALSE, color = "blue") +
  labs(x = "Weight (1,000 lb)",
       y = "Fuel economy (miles/US gallon)")

lm() estimates the coefficients. geom_smooth() draws the fitted line.

Assumptions and limits

  • The mean response should be approximately linear in weight.
  • Errors should be independent with constant variance. Normal errors support the usual small-sample intervals.
  • A residual plot alone cannot verify every assumption.
  • Engine size and other features may help explain MPG.
  • These 32 historical cars do not represent all modern vehicles.

Association alone does not establish that changing weight causes the estimated change in MPG.

Summary and sources

Simple linear regression turns a scatterplot into an interpretable equation.

In this sample, heavier cars tend to have lower MPG. Predictions still have uncertainty, and residuals help assess the model.

Data: R datasets::mtcars, documented in help("mtcars").

Original data reference: Henderson, H. V. and Velleman, P. F. (1981). Building multiple regression models interactively. Biometrics, 37, 391-411.

R documentation: help("lm") and help("predict.lm") explain model fitting and prediction intervals.