What is Linear Regression?

  • Technique to predict values based off of existing data points
  • Uses a best-fit line to make predictions
  • Predicts dependent variables using independent variables
  • Independent variable is on the X-axis
  • Dependent variable is on the Y-axis
  • Common Use Cases:
    • Forecasting
    • Trend Analysis
    • Predictive Modelling

Dataset

head(cars)
##   speed dist
## 1     4    2
## 2     4   10
## 3     7    4
## 4     7   22
## 5     8   16
## 6     9   10
  • Cars contains 2 variables (one independent, one dependent)
  • Ideal for linear regression

Plot Code

carsplot = plot_ly(cars, x=~speed, y=~dist, 
                   type="scatter", mode='markers') %>%
  layout(xaxis=list(title="Speed"), 
         yaxis=list(title="Stopping Distance")) %>%
  layout(title = "Speed vs Stopping Distance")

This code will plot the cars dataset.

Key Points

  • Independent Variable: Speed
  • Dependent Variable: Stopping Distance

Cars Plot

Predict

To use linear regression in R, we must first create the model.

model = lm(dist ~ speed, data = cars)
model
## 
## Call:
## lm(formula = dist ~ speed, data = cars)
## 
## Coefficients:
## (Intercept)        speed  
##     -17.579        3.932

Math

But before that, can we use math?

The linear regression equation is \(y = \beta_0 + \beta_1x\)

From the previous slide, we know that: \[ \beta_0 = -17.579\newline \beta_1 = 3.932 \] Therefore, our linear regression equation for cars is: \[y = -17.579 + 3.932x\] For a speed of 20 (\(x=20\)), we get a distance of: \[ y = -17.579 + 3.932(20) = 61.061 \]

Predict

To use R to do the same prediction, we use the model we made earlier.

Using this snippet of R code:

predicted_distance = predict(model, newdata = data.frame(speed = 20))

We get the output, which is 61.0690803 feet.

Model Plot

Using ggplot2, we can show the line of best fit.

Calculating the Slope Manually

lm() uses this formula to calculate the slope: \[ \beta_1 = \frac{\sum_{i=1}^{n}(x_i-\bar{x})(y_i-\bar{y})} {\sum_{i=1}^{n}(x_i-\bar{x})^2} \] To calculate the line using R:

x = cars$speed; y=cars$dist
x_bar = mean(x); y_bar = mean(y)
slope = sum((x-x_bar) * (y-y_bar))/sum((x-x_bar)^2)

This gives us a slope of 3.932 feet/mph.

Calculating the Intercept Manually

Now that we have the slope, we can calculate the intercept using: \[ \beta_0 = \bar{y} - \beta_1\bar{x} \] Substituting values that we already know:

\[ \beta_0 = 42.98 - 3.9324088\times15.4\newline \beta_0 \approx -17.579 \]

Plotting Our Calculated Line

Using ggplot2 once again, we can plot the line of best fit that we calculated.