What is simple linear regression?

  • A way to fit a straight line through data
  • The line shows how one variable \(y\) changes when another variable \(x\) changes
  • We can then use the line to make predictions

The equation

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

  • \(\widehat{y}\) is the predicted value of \(y\)
  • \(b_0\) is the intercept, the value of \(\widehat{y}\) when \(x = 0\)
  • \(b_1\) is the slope, how much \(\widehat{y}\) goes up when \(x\) goes up by 1

Finding the best line

We could draw lots of different lines, so we pick the one where the points are as close to the line as possible. This is the line with the smallest sum of squared errors:

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

  • \(y_i - \widehat{y}_i\) is how far point \(i\) is from the line
  • Squaring makes every distance positive, so points above and below the line don’t cancel out

Example: stopping distance of cars

The built-in R data set cars has the speed (mph) and stopping distance (ft) of 50 cars.

Fitting the line in R

fit = lm(dist ~ speed, data = cars)
coef(fit)
## (Intercept)       speed 
##  -17.579095    3.932409

So the fitted line is

\[\widehat{y} = -17.58 + 3.93 x\]

Each extra mph adds about 3.9 feet to the stopping distance.

The fitted line

Making a prediction

How far does a car going 20 mph need to stop?

We plug \(x = 20\) into the line:

\[\widehat{y} = -17.58 + 3.93 \times 20 \approx 61\]

So it would need about 61 feet to stop.

Going further: two variables

With two \(x\) variables, \(\widehat{y} = b_0 + b_1 x_1 + b_2 x_2\), the line becomes a plane.

For example, predicting a tree’s volume from its girth and height:

Summary

  • Simple linear regression fits a straight line \(\widehat{y} = b_0 + b_1 x\) through data
  • The best line is the one with the smallest sum of squared errors
  • For example, in the cars data, each extra mph adds about 3.9 feet of stopping distance
  • With two \(x\) variables, the same idea gives a plane instead of a line