What is simple linear regression?

  • Simple linear regression studies the relationship between one predictor and one numerical outcome.
  • It fits a straight line through observed data.
  • We can use the line to describe a trend and make predictions.

Our question: Do heavier cars tend to get fewer miles per gallon (MPG)?

Our data: 32 cars

We will use the built-in R dataset mtcars, which contains measurements of 32 cars.

  • Predictor (x): wt, car weight in thousands of pounds
  • Outcome (y): mpg, fuel efficiency in miles per gallon
  • Each dot on a graph represents one car.

This is an observational dataset, so a relationship does not automatically prove causation.

First look: a scatterplot

The dots generally slope downward: heavier cars tend to have lower MPG.

The regression equation

The general equation for a simple linear regression line is

\[\hat{y} = b_0 + b_1 x\]

  • \(\hat{y}\) is the predicted outcome.
  • \(b_0\) is the intercept, or predicted outcome when \(x=0\).
  • \(b_1\) is the slope, or predicted change in the outcome for a one-unit increase in \(x\).

For our data, \(x\) is weight and \(\hat{y}\) is predicted MPG.

The fitted line

The line summarizes the average trend; the shaded band shows uncertainty about the mean fitted line.

Interpreting the slope and intercept

Our estimated equation is

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

  • Slope: About 5.34 fewer MPG for each additional 1,000 pounds of car weight.
  • Intercept: 37.29 MPG at zero weight. This is not a realistic car, so the intercept is not practically useful here.
  • Prediction: A 3,000-pound car has a predicted fuel efficiency of about 21.3 MPG.

Residuals: how far off are predictions?

A residual is the difference between an observed value and its prediction:

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

The least-squares method chooses the line that minimizes the sum of squared residuals:

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

A positive residual means the observed MPG is higher than predicted. A negative residual means it is lower.

An interactive Plotly chart

Hover over a point to see that car’s weight and MPG. You can also zoom into the chart.

R code used for the analysis

The following R code fits the regression model, shows its coefficients, and predicts MPG for a 3,000-pound car.

model <- lm(mpg ~ wt, data = mtcars)
coef(model)
## (Intercept)          wt 
##   37.285126   -5.344472
predict(model, newdata = data.frame(wt = 3))
##        1 
## 21.25171

lm() fits a linear model; predict() uses the fitted model to estimate an outcome.

How well does the model fit?

The coefficient of determination, \(R^2\), describes the share of variation in the outcome explained by the model.

\[R^2 = 1 - \frac{SSE}{SST}\]

For our regression, $R^2 = 0.753.

That means weight accounts for about 75.3% of the observed variation in MPG in this sample. Other factors also influence fuel efficiency.

Key takeaways

  • Simple linear regression helps describe and predict a numerical outcome using one predictor.
  • In these 32 cars, greater weight is associated with lower MPG.
  • The fitted slope gives an interpretable change in MPG for each additional 1,000 pounds.
  • Predictions are estimates, not guarantees; this association alone does not establish cause and effect.

Data source: R built-in mtcars dataset (?mtcars).