What is Simple Linear Regression?

Simple linear regression models the relationship between two numerical variables: a predictor (x) and a response (y), by fitting a straight line through the data.

  • Used to predict a response variable from one explanatory variable
  • Assumes a linear relationship between x and y
  • Widely used in economics, biology, engineering, and more

The Model

The simple linear regression model is:

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

where \(\varepsilon \sim \mathcal{N}(0, \sigma^2)\)

  • \(\beta_0\) is the intercept
  • \(\beta_1\) is the slope
  • \(\varepsilon\) is random error

Estimating the Line: Least Squares

We estimate \(\beta_0\) and \(\beta_1\) by minimizing the sum of squared errors:

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

\[MSE = \frac{SSE}{n-2}\]

This gives us the “best fitting” line through the data.

R Code: Fitting the Model

mod <- lm(Volume ~ Girth, data = trees)
summary(mod)$coefficients
##               Estimate Std. Error   t value     Pr(>|t|)
## (Intercept) -36.943459   3.365145 -10.97827 7.621449e-12
## Girth         5.065856   0.247377  20.47829 8.644334e-19
g <- ggplot(trees, aes(x = Girth, y = Volume)) +
  geom_point() +
  geom_smooth(method = "lm", se = TRUE) +
  theme_bw() +
  labs(title = "Tree Volume vs. Girth",
       x = "Girth (inches)", y = "Volume (cubic feet)")

ggplot: Fitted Regression Line

g

ggplot: Colored by a Third Variable

ggplot(trees, aes(x = Girth, y = Volume, color = Height)) +
  geom_point(size = 3) +
  geom_smooth(method = "lm", se = FALSE, color = "black") +
  scale_color_gradient(low = "lightblue", high = "darkblue") +
  theme_minimal() +
  labs(title = "Tree Volume vs. Girth, colored by Height")

Interactive Plotly Version

x <- trees$Girth
y <- trees$Volume

fig <- plot_ly(x = x, y = y, type = "scatter", mode = "markers", name = "data") %>%
  add_lines(x = x, y = fitted(mod), name = "fitted") %>%
  layout(xaxis = list(title = "Girth"),
         yaxis = list(title = "Volume"))
fig

Summary

  • Simple linear regression fits a line \(y = \beta_0 + \beta_1 x\) to data
  • The line minimizes squared errors (least squares)
  • lm() in R fits the model; ggplot2 and plotly visualize it
  • Useful across many fields wherever two variables are linearly related