What is simple linear regression?

  • Simple linear regression looks at how one variable is related to another.
  • It uses a straight line to help explain the relationship.
  • We can also use the line to make predictions.

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

The data we’re using

  • We will use mtcars, a dataset that comes with R.
  • wt is the car’s weight in 1,000 pounds.
  • mpg is its fuel efficiency in miles per gallon.
  • There are 32 cars in the dataset.
##                      wt  mpg
## Mazda RX4         2.620 21.0
## Mazda RX4 Wag     2.875 21.0
## Datsun 710        2.320 22.8
## Hornet 4 Drive    3.215 21.4
## Hornet Sportabout 3.440 18.7

First, let’s look at the data

This scatterplot shows weight and miles per gallon for each car.

The regression equation

We describe a straight-line relationship with:

\[\hat{y} = b_0 + b_1x\]

  • \(\hat{y}\): the predicted value
  • \(b_0\): where the line crosses the vertical axis
  • \(b_1\): how much the prediction changes when \(x\) goes up by 1
  • \(x\): the value we use to make a prediction

Finding the best-fitting line

We use lm() in R to fit the regression line.

For our data, the fitted line is approximately:

\[\widehat{mpg} = 37.29 - 5.34(wt)\]

  • The negative slope means heavier cars generally get lower MPG.
  • For every extra 1,000 pounds, predicted MPG drops by about 5.34.

Seeing the regression line

The line shows the overall trend, not a perfect prediction for every car.

An interactive Plotly graph

Move your mouse over the dots to explore individual cars.

Example: Predicting MPG

Let’s predict the fuel efficiency of a car weighing 3,000 pounds.

Since weight is measured in thousands of pounds, \(x = 3\).

\[\widehat{mpg} = b_0 + b_1(3)\]

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

So our model predicts about 21.3 MPG for that car.

The R code behind the model

This is the main code used to fit the line and make a prediction.

# Use the built-in mtcars dataset
fit <- lm(mpg ~ wt, data = mtcars)

# See the regression results
summary(fit)

# Predict MPG for a 3,000-pound car
predict(fit, newdata = data.frame(wt = 3))

What did we learn?

  • In this dataset, heavier cars tend to have lower MPG.
  • A regression line helps us describe that pattern.
  • We can use the equation to predict MPG for a given weight.
  • The predictions are estimates, not guarantees.

Data source: R’s built-in mtcars dataset.