2025-10-16

Simple Linear Regressions

My presentation will be on simple linear regressions

What is a Simple Linear Regression

  • A simple linear regression exists to approximate a target value \(y\)
  • The regression line is given by \(y = \beta_0 + \beta_1 x_1 + ... + \beta_n x_n\)
  • The beta value are called weights and the x values are called regressors. The beta values scale how important the regressors are to the equation

OLS (Ordinary Least Squares)

  • When using a simple linear regression, we want to minimize the difference between the error between the true value and the estimated value.
  • This is called the sum of squared residuals
  • It is calculated by the equation: \[SSE = \sum_{i=1}^{n} (y_i - \hat{y}_i)^2\]

Example one variable linear regression

Code for Previous Plot

library(ggplot2)

ggplot(trees, 
       aes(x=Height, y=Volume))
+ geom_point() 
+ geom_smooth(method="lm", 
              color="darkgreen") 
+ labs(title="Linear Regression of Volume on Girth", 
       x ="Tree Height (ft)", 
       y="Tree Volume (ft^3)")

Another Example

Multiple Variable Linear Regression

Code for previous plot

library(plotly)

model = lm(mpg ~ hp + qsec, data=mtcars)

horsepowerseq = seq(min(mtcars$hp), max(mtcars$hp), length.out = 30)
timeseq = seq(min(mtcars$qsec), max(mtcars$qsec), length.out=30)
grid = expand.grid(hp = horsepowerseq, qsec = timeseq)
grid$mpg_est = predict(model, newdata = grid)
matr = matrix(grid$mpg_est, 
                nrow=length(horsepowerseq),
                ncol=length(timeseq))

p = plot_ly() %>%
  add_markers(
    data = mtcars,
    x = ~hp,
    y = ~qsec,
    z= ~mpg,
    marker = list(size=8),
    name = "Observed Data"
  ) %>%
  add_surface(
    x=horsepowerseq,
    y=timeseq,
    z=matr,
    name = "Regression Plane"
  ) %>%
    layout(
      title="Predicting Fuel Efficiency",
      scene = list(
        xaxis = list(title="Horsepower"),
        yaxis = list(title="1/4 Mile Time"),
        zaxis = list(title = "MPG")
      )
    )
p