2024-01-21

Simple Linear Regression

  • Linear regression is the process of determining the relationship between input variable, also known as a predictor, and output variable, also known as an outcome.
  • This method can be used to predict unknown outputs based on known inputs.

Linear Regression Math

  • The formula for linear regression is

\(Y_i = b_1X_i + b_0 + \varepsilon_i\)

Where \(b_1\) is slope, \(X_i\) represents each increase in x value, or predictor, \(b_0\) is the y-intercept, and \(\varepsilon_i\) is the residual.

ScatterPlot without linear regression

This is an example of a plot that has not had any regression added. The shape of the plot indicates that it would be a candidate for linear regression due to the relative linearity of the plot.

ScatterPlot with linear regression

This is an example of a plot that has linear regression added. The blue line is our estimated output for that value of x. The grey shaded area is the confidence interval

ScatterPlot with linear regression plotlyfied

This is an example of a plot that has linear regression added that is now interactive. Hovering over certain points will give more information.

Linear Model

The summary on the next slide tells us that our original formula of:

\(Y_i = b_1X_i + b_0 + \varepsilon_i\)

now has the following values:

\(stopping distance = 3.9324*speed - 17.5791 \pm .41\)

Summary of Linear Model

Call:
lm(formula = dist ~ speed, data = cars)

Residuals:
    Min      1Q  Median      3Q     Max 
-29.069  -9.525  -2.272   9.215  43.201 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) -17.5791     6.7584  -2.601   0.0123 *  
speed         3.9324     0.4155   9.464 1.49e-12 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 15.38 on 48 degrees of freedom
Multiple R-squared:  0.6511,    Adjusted R-squared:  0.6438 
F-statistic: 89.57 on 1 and 48 DF,  p-value: 1.49e-12

Code used for this presentation:

knitr::opts_chunk$set(echo = FALSE)
library(ggplot2)
library(dplyr)
library(plotly)
data("cars")
g <- ggplot(cars, aes(speed, dist)) + geom_point()
g

g <- ggplot(cars, aes(speed, dist)) +
     geom_point() + 
     geom_smooth(method = "lm")
g

ggplotly(g)

stopping_distance_lm <- lm(dist~speed, data = cars)
summary(stopping_distance_lm)