- Simple linear regression looks at how one number affects another.
- It fits a straight line through the data.
- Analysts use it to predict things like sales, cost, or demand.
- We will build one step by step using R.
2026-09-13
A straight line through the data looks like this:
\[ y_i = \beta_0 + \beta_1 x_i + \varepsilon_i \]
R finds the best line using least squares:
\[ \hat{\beta}_1 = \frac{\sum_{i=1}^{n}(x_i-\bar{x})(y_i-\bar{y})}{\sum_{i=1}^{n}(x_i-\bar{x})^2}, \qquad \hat{\beta}_0 = \bar{y} - \hat{\beta}_1\bar{x} \]
Using the trees dataset built into R. It has the girth, height, and volume data of trees.
head(trees)
## Girth Height Volume ## 1 8.3 70 10.3 ## 2 8.6 65 10.3 ## 3 8.8 63 10.2 ## 4 10.5 72 16.4 ## 5 10.7 81 18.8 ## 6 10.8 83 19.7
We will predict Volume from Girth.
mod <- lm(Volume ~ Girth, data = trees) g <- ggplot(trees, aes(x = Girth, y = Volume)) + geom_point() g + geom_smooth(method = "lm") + theme_bw()
Girth may not be the only thing that explains Volume. Let’s check Height too.
ggplot(trees, aes(x = Height, y = Volume)) + geom_point() + geom_smooth(method = "lm") + theme_bw()
x <- trees$Girth y <- trees$Volume fig <- plot_ly(x = x, y = y, type = "scatter", mode = "markers", name = "data", height = 350) %>% add_lines(x = x, y = fitted(mod), name = "fitted") %>% config(displaylogo = FALSE) fig
fig3d <- plot_ly(x = trees$Girth, y = trees$Height, z = trees$Volume, type = "scatter3d",
mode = "markers", color = trees$Volume, height = 350) %>%
hide_colorbar()
fig3d