What is simple linear regression?

Simple linear regression uses a straight line to describe the relationship between two numerical variables.

Example: Does car weight help predict the fuel economy?

Must use R’s built-in mtcars dataset, which contains 32 cars.

  • x: Weight in thousands of pounds (wt).
  • y: Miles per gallon (mpg).

The regression equation

\[Y = \beta_0 + \beta_1x + \varepsilon\]

  • \(\beta_0\) is the intercept.
  • \(\beta_1\) is the slope: the change in mean \(Y\) when \(x\) increases by 1.
  • \(\varepsilon\) represents unexplained variation.

The estimated line is

\[\widehat y = b_0 + b_1x.\]

R’s lm() function estimates the intercept and slope.

Scatterplot of the data

Heavier cars usually have lower fuel economy in this dataset.

Adding a regression line

An extra 1,000 lb is associated with about 5.34 fewer mpg. This association does not prove causation.

Example: predicting fuel economy

For a car weighing 3,000 lb, use \(x = 3\).

The fitted equation, with rounded coefficients, is

\[\widehat y = 37.285 - 5.344x.\]

Substitute \(x = 3\):

\[\widehat y = 37.285 - 5.344(3) \approx 21.25.\]

The predicted fuel economy is about 21.25 mpg. An individual car’s actual mpg can differ.

Interactive Plotly plot

Hover over a point to see its values. Drag to zoom in.

R code for the regression plot

library(ggplot2)
cars <- mtcars

model <- lm(mpg ~ wt, data = cars)

ggplot(cars, aes(x = wt, y = mpg)) +
  geom_point() +
  geom_smooth(method = "lm", formula = y ~ x,
              se = FALSE) +
  labs(x = "Weight (1,000 lb)",
       y = "Miles per gallon")

predict(model, data.frame(wt = 3))