2024-02-04

Overview of Simple Linear Regression

  • Statistical method that allows statisticians to study relationships between two variables that are continuous and quantitative
  • One variable (commonly x) is the predictor, or independent variable
  • One variable (commonly y) is the response, or dependent variable
  • A best-fitting line is created from the linear relationship between these two variables
  • This line predicts what the dependent variable could be as if it was a function

What linear regression IS NOT

  • A functional relationship (can determine y through a function and any x).
  • So a graph showing the relationship between circumference and diameter will not work with simple linear regression since \[ C = \pi \cdot d \]
  • Example of graph:

Defining Simple Linear Regression

  • Best used with scatter plots, or statistical relationships in which the independent variable does not give the dependent variable with some formula.

  • The equation for the best fitting line is \[ \hat{y}_i = b_0 + b_1 x_i\] where:

  • \(\hat{y}_i\) is the predicted response for experimental unit \(i\)

  • \(x_i\) is the independent variable (predictor) for experimental unit \(i\)

  • \(b_0\) is the y-intercept (constant term)

  • \(b_1\) is the slope coefficient.

Defining \(b_0\) and \(b_1\)

The formula for the slope (\(b_1\)) in simple linear regression is given by: \[ b_1 = \frac{\sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=1}^{n} (x_i - \bar{x})^2} \] And the formula for the y-intercept (\(b_0\)) is: \[ b_0 = \bar{y} - b_1 \bar{x} \]

An Example

Given this data:

##   Miles Gallons
## 1   204      10
## 2   312      15
## 3   144       7
## 4   266      12
## 5   180       9
## 6   192      10
## 7   366      17
## 8   165       8

We can create a scatterplot with linear regression in play

An Example pt. 2

The best fitted line can be determined with lm() in R

An Example of Multiple Linear Regression

Straying from simple linear regression, it is possible to do multiple linear regression in which there are two independent variables.

##   Miles Weight Gallons
## 1   204   3000      10
## 2   312   3500      15
## 3   144   2800       7
## 4   266   3200      12
## 5   180   2900       9
## 6   192   3100      10
## 7   366   3800      17
## 8   165   2600       8

An Example of Multiple Linear Regression Pt. 2

The code for the previous slide:

library(plotly)
miles <- c(204, 312, 144, 266, 180, 192, 366, 165)
gallons <- c(10, 15, 7, 12, 9, 10, 17, 8)
weight <- c(3000, 3500, 2800, 3200, 2900, 3100, 3800, 2600)
mileage_data <- data.frame(Miles = miles, Weight = weight, 
                           Gallons = gallons)
# Fit the linear regression model
model <- lm(Gallons ~ Miles + Weight, data = mileage_data)
grid <- expand.grid(Miles = seq(min(miles), max(miles), length.out = 10),
                    Weight = seq(min(weight), max(weight), length.out = 10))
grid$Gallons <- predict(model, newdata = grid)
# Creating the 3D scatter plot
p <- plot_ly(mileage_data, x = ~Miles, y = ~Weight, z = ~Gallons, 
             type = "scatter3d")
# Adding the regression plane
p <- add_trace(p, x = ~Miles, y = ~Weight, z = ~Gallons, data = grid, 
               type = "mesh3d", opacity = 0.5)
# Print
p