2025-06-08

What is Simple Linear Regression?

Simple linear regression is a tool that is used in statitistics to represent the relationship between two continuous variables.

Equation/Rules

The equation for linear regression:

\[y = \beta_0 + \beta_1 X + \epsilon\]

Where: * \(Y\) is the dependent variable * \(X\) is the independent variable * \(\beta_0\) is the y-intercept * \(\beta_1\) is the slope * \(\epsilon\) is the random error term

We need to find the best estimates for \(\beta_0\) and \(\beta_1\)

Example Description

We will use the built in cars dataset for our first example. In the example we will be examining the relationship between a car’s speed and the distance it takes to stop

  • ‘speed’: the speed of the car in mph
  • ‘dist’: the distance it takes to stop in ft

Here is what the dataset looks like:

head(cars)
##   speed dist
## 1     4    2
## 2     4   10
## 3     7    4
## 4     7   22
## 5     8   16
## 6     9   10

Example Graph

To best represent the data we should use a scatter plot because it best shows the relationship between ‘speed’ and ‘dist’

Example Building Regression Model

This slide shows how we are building the linear regression model in R using the ‘lm()’ function

#building the model using lm()
model = lm(dist ~ speed, data = cars)
#display the summary for viewers
summary(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

Example Linear Regression Line

From the model summary we get our coefficients which are:

  • Intercept (\(\hat{\beta}_0\)): ‘-17.58’
  • Slope (\(\hat{\beta}_1\)): ‘3.93’

This gives us the equation:

\[\widehat{dist} = -17.58 + 3.93 \times speed\]

Example line Plotted

Here is the line on our scatter plot:

Graph with Specific Values

Using the ‘plotly’ library we can add the extra functionality of being able to zoom into the graph to see specific values.