The example

How does fuel economy vary with car weight?

  • Data: 32 cars, 1973-74 models, in R’s built-in mtcars dataset.
  • Predictor: wt, weight in thousands of pounds.
  • Response: mpg, miles per US gallon.
  • These observational data illustrate a method. They do not represent today’s entire car market.

Source: R datasets documentation: mtcars.

Heavier cars tend to have lower mpg

The line summarizes the trend. Shading shows a 95% confidence interval for the mean response under the model.

The regression model

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

  • \(\beta_0\): mean response at \(x=0\).
  • \(\beta_1\): change in the mean response for a one-unit increase in \(x\).
  • \(\varepsilon_i\): variation around the line, with \(E(\varepsilon_i\mid x_i)=0\).

Here, one unit of \(x\) means 1,000 pounds. The fitted line estimates mean mpg at a given weight.

How least squares chooses the line

\[\hat{\beta}_1 = \frac{\sum_{i=1}^{n}(x_i-\bar{x})(y_i-\bar{y})} {\sum_{i=1}^{n}(x_i-\bar{x})^2}, \qquad \hat{\beta}_0 = \bar{y}-\hat{\beta}_1\bar{x}\]

\[\operatorname{SSE} = \sum_{i=1}^{n}(y_i-\hat{y}_i)^2\]

Least squares chooses the intercept and slope that minimize SSE.

A residual is the vertical difference between an observed mpg value and the fitted line.

The fitted relationship

\[\widehat{\mathrm{mpg}} = 37.29 - 5.34\,\mathrm{wt}\]

  • An additional 1,000 lb is associated with 5.34 fewer mpg, on average, in this fitted model.
  • \(R^2 = 0.753\): weight accounts for about 75.3% of the observed variation in mpg around its sample mean.
  • A zero-weight car lies outside the data. The intercept has no useful physical interpretation here.

A worked prediction at 3,000 lb

\[\hat{y} = 37.2851 - 5.3445(3) \approx 21.25\ \mathrm{mpg}\]

Quantity Result
Estimated mean mpg 21.25
95% CI for mean mpg 20.12 to 22.38
95% prediction interval: one car 14.93 to 27.57

The prediction interval is wider because individual cars vary around the mean. Both intervals depend on the regression assumptions.

Residuals reveal what the line misses

The curved smooth suggests the straight line misses some structure. Several cars differ from the fitted value by more than 5 mpg.

An interactive view of the cars

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

R code for the first plot

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

ggplot(cars, aes(wt, mpg)) +
  geom_point(color = "#007C83", size = 3, alpha = 0.85) +
  geom_smooth(method = "lm", formula = y ~ x,
              color = "#8C1D40", fill = "#8C1D40", alpha = 0.13) +
  labs(x = "Weight (1,000 lb)", y = "Fuel economy (mpg)")

lm() estimates the coefficients. geom_smooth() draws the same fitted line and its confidence band.

Assumptions and limits

  • The model assumes a linear conditional mean and independent errors with constant variance.
  • Exact small-sample t intervals also assume normally distributed errors. The residual plot alone cannot verify all assumptions.
  • Weight may track other features, such as horsepower. This analysis does not establish that weight alone causes the mpg difference.
  • Predictions are most defensible within the observed weight range: 1.51 to 5.42 thousand lb.

What the example teaches

The fitted slope describes a strong negative association between weight and fuel economy in these 32 cars.

At 3,000 lb, the fitted mean is about 21.25 mpg, but the interval for an individual car is much wider.

The residual pattern makes a useful next question: would a curved relationship or an additional predictor improve the model?

Data: R datasets: mtcars. Methods: R’s stats::lm and stats::predict.lm. Plots: ggplot2 and Plotly. All numerical results are computed from the included R code.