Why Simple Linear Regression?

  • Statistics can help us quantify how two variables are related.
  • Simple linear regression uses one explanatory variable to predict one response.
  • Our example: Can vehicle weight predict fuel efficiency?
We use the built-in R dataset mtcars, containing 32 cars.

Meet the Data

  • Response (Y): mpg, miles per US gallon (fuel efficiency).
  • Predictor (X): wt, vehicle weight in thousands of pounds.
  • There are 32 observations (cars).
  • We expect heavier cars to have lower fuel efficiency.
Weight (1,000 lb) MPG
Mazda RX4 2.62 21.0
Mazda RX4 Wag 2.88 21.0
Datsun 710 2.32 22.8
Hornet 4 Drive 3.21 21.4
Hornet Sportabout 3.44 18.7

The Linear Model

We model the average response as a straight line:

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

  • \(\beta_0\): intercept, the predicted response when \(X=0\).
  • \(\beta_1\): slope, the change in predicted response for a one-unit increase in \(X\).
  • \(\varepsilon_i\): unexplained variation (error).

Exploring the Relationship

## `geom_smooth()` using formula = 'y ~ x'

Estimating the Regression Line

We choose the line that minimizes the sum of squared residuals:

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

For these data, the fitted line is:

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

Interpretation: Each additional 1,000 lb is associated with about 5.34 fewer MPG, on average.

Interactive Plotly View

Hover over each point to see the car model, weight, and MPG.

How Well Does It Fit?

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

\[ R^2 = 1 - \frac{\sum_i (y_i-\hat y_i)^2}{\sum_i(y_i-\bar y)^2} \]

For our example, \(R^2 = 0.753\) — approximately 75.3% of the variation in MPG is explained by a linear relationship with weight.

This is a description of model fit, not proof of causation.

Checking Residuals

Residuals are observed minus predicted values: \(e_i=y_i-\hat y_i\).

R Code: Fit and Plot the Model

library(ggplot2)

fit <- lm(mpg ~ wt, data = mtcars)
summary(fit)

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point(color = "#8C1D40", size = 3) +
  geom_smooth(method = "lm", se = TRUE,
              color = "#147D86") +
  labs(x = "Weight (1,000 lb)", y = "MPG") +
  theme_minimal()

Worked Example: Predict MPG

Suppose a car weighs 3,000 lb, so its wt value is 3.0.

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

The model predicts approximately 21.3 MPG for a car weighing 3,000 lb.

A prediction is an estimate, not a guarantee for any individual car.

Key Takeaways

  • Weight and MPG have a negative linear association in mtcars.
  • Regression provides an interpretable slope and useful predictions.
  • \(R^2\) summarizes fit; residual plots help diagnose problems.
  • Association is not causation, and predictions outside the observed weight range can be unreliable.
Data source: R built-in mtcars dataset. Analysis: lm(mpg ~ wt).