What is Simple Linear Regression?

  • It finds the best straight line through data
  • We use that line to predict one thing from another
  • Example: do heavier cars get worse gas mileage?
  • Data: mtcars, 32 cars that come built into R

The Data

Heavier cars get less miles per gallon

The Model

\[y = \beta_0 + \beta_1 x + \varepsilon\]

  • \(y\) = miles per gallon
  • \(x\) = weight of the car (in 1000 lbs)
  • \(\beta_0\) = intercept, where the line starts
  • \(\beta_1\) = slope, how much mpg changes when weight goes up by 1
  • \(\varepsilon\) = error, the part the line can’t explain

Finding the Best Line

R picks the line that makes the errors as small as possible

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

  • \(y_i\) = the real mpg of each car
  • \(\hat{y}_i\) = the mpg the line predicts
  • This is called the least squares method

Fitting the Model in R

mod = lm(mpg ~ wt, data = mtcars)
coef(mod)
## (Intercept)          wt 
##   37.285126   -5.344472

So the line is

\[\hat{y} = 37.29 - 5.34x\]

The Regression Line

The line goes down so more weight means less mpg

Interactive Plot

Code for the Interactive Plot

x = mtcars$wt
y = mtcars$mpg

plot_ly(x = x, y = y, type = "scatter", mode = "markers",
        name = "data", marker = list(color = "blue"),
        width = 750, height = 430) %>%
  add_lines(x = x, y = fitted(mod), name = "fitted",
            line = list(color = "red")) %>%
  layout(xaxis = list(title = "Weight (1000 lbs)"),
         yaxis = list(title = "Miles per Gallon"))
  • fitted(mod) gives the y values on the regression line
  • add_lines() draws the red line on top of the blue points

Making a Prediction

What is the mpg for a car that weighs 3000 lbs (\(x = 3\))?

\[\hat{y} = 37.29 - 5.34(3) \approx 21.3\]

So a 3000 lb car gets about 21 miles per gallon

Conclusion

  • Heavier cars get less miles per gallon
  • Every extra 1000 lbs lowers mpg by about 5.3
  • Weight explains about 75% of the change in mpg (\(R^2 = 0.75\))
  • We can use the line to predict mpg for any weight