2024-03-17

Overview

A simple linear regression is used to draw a relationship between an explanatory variable, or x, and a quantiative response variable, y.

The straight line model

Simple linear regression models can be represented by the following equation, where

  • \(y\) represents the dependent variable
  • \(\beta_0\) represents the y intercept of the regression line
  • \(\beta_1\) represents the slope of the regression line
  • \(\epsilon\) is the error term

The equation for a simple linear regression model is:

\[ y = \beta_0 + \beta_1x + \epsilon \]

Simple linear regression example

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

cars$predicted_dist <- predict(model)

p <- plot_ly(data = cars, x = ~speed, y = ~dist, type = 'scatter', mode = 'markers')
p <- add_trace(p, x = ~speed, y = ~predicted_dist, type = 'scatter', mode = 'lines')
p

Evaluating a regression line

In order to evaluate a regression line, we can use something called the coefficient of determination, which is denoted by “r-squared”:

  • \(abs(R^2) > 0.7\) is considered strong
  • \(R^2 > 0\) indicates a positive correlation
  • \(R^2 < 0\) indicates a negative correlation

Computing R-Squared

The coefficient of determination, \(R^2\), is computed as:

\[ R^2 = 1 - \frac{SS_{\text{res}}}{SS_{\text{tot}}} \]

where:

  • \(SS_{\text{res}}\) is the sum of squares of residuals
  • \(SS_{\text{tot}}\) is the total sum of squares.

Evaluating a strong simple linear regression model

Evaluating a weak simple linear regression model pt. 2