2026-09-13

Introduction

  • 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.

The Model

A straight line through the data looks like this:

\[ y_i = \beta_0 + \beta_1 x_i + \varepsilon_i \]

  • \(\beta_0\) is the intercept, where the line starts.
  • \(\beta_1\) is the slope, how steep the line is.
  • \(\varepsilon_i\) is the error, the part the line does not explain.

Fitting the Line

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} \]

  • Least squares picks the line that keeps the errors as small as possible.
  • In R, one function does all of this: lm()

The Data

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.

Fitting the Line with ggplot2

mod <- lm(Volume ~ Girth, data = trees)
g <- ggplot(trees, aes(x = Girth, y = Volume)) + geom_point()
g + geom_smooth(method = "lm") + theme_bw()

A Second Predictor

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()

The Same Plot with plotly

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

All Three Variables in 3D

fig3d <- plot_ly(x = trees$Girth, y = trees$Height, z = trees$Volume, type = "scatter3d", 
    mode = "markers", color = trees$Volume, height = 350) %>%
    hide_colorbar() 
fig3d

Summary

  • Simple linear regression fits a straight line through two variables.
  • The slope and intercept come from least squares, found in R with lm().
  • ggplot2 and plotly can both show the fit and plotly adds zoom and hover.
  • With more than one predictor, a 3D plot can show everything at once.