2026-10-18

Why Linear Regression?

When our goal is to analyze the relationship between two variables, we want to find a way to identify trends and correlations within the data so that we may predict an output based on a hypothetical input.

One way to do this is with a simple linear regression model.

Linear Regression

We use the simple linear model:

\(y = \beta_0 + \beta_1 x\)

  • \(\beta_0\) represents the intercept
  • \(\beta_1\) represents the slope
  • This model can also be represented in the form \(y=mx+b\) , or the form \(y=\theta_0 + \theta_1 x\)

Least Squares

How do we find \(\beta_0\) and \(\beta_1\) for our model?

For the simple linear model, we can use the method of least squares.

\(\theta = (X^T X)^{-1} X^T y\), where \(\theta = [ \beta_0 , \beta_1 ]^T\)

For more complex models, we use the method of gradient descent. (Not covered in these slides)

Code example in R: Cylinders vs MPG

library(dplyr)
library(plotly)
data("mtcars")
mod = lm(mpg ~ cyl, data = mtcars)
x = mtcars$cyl; y = mtcars$mpg
font = list(family = "Modern Computer Roman")
xax = list(title = "Cylinders", 
           titlefont = font)
yax = list(title = "Miles Per Gallon", 
           titlefont = font)
plot_ly(x=x, 
        y=y, 
        type="scatter",
        mode="markers",
        name="data") %>% 
  add_lines(x=x,
            y=fitted(mod),
            name="fitted") %>% 
  layout(xaxis = xax,
         yaxis = yax)

Plot example in R: Cylinders VS MPG

Our linear regression line shows a negative correlation between a car’s number of cylinders and it’s miles per gallon.

Month VS Temp

This linear regression example shows that the temperature tends to increase from May through September.

Iris Petal Length VS Petal Width

This linear regression example shows how petal length and petal width are correlated with each of the three types of iris flower. Since the versicolor species has the highest slope, it has the strongest correlation. Likewise, the setosa species has the weakest correlation between petal length and petal width.