- 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
mtcars, 32 cars that come built into RHeavier cars get less miles per gallon
\[y = \beta_0 + \beta_1 x + \varepsilon\]
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\]
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 line goes down so more weight means less mpg
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 lineadd_lines() draws the red line on top of the blue pointsWhat 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