2024-02-04

What is linear regression?

Today we will be looking at linear regression and how we can use it as a tool to make predictions based off of data.

On a most basic level, linear regression is simply finding the linear relationship between two different variables, if one exists.

In order to show how linear regression works we will be looking at a data set of car information that includes their fuel efficiency. This is a popular data set for this topic as there are many things that have a direct impact on fuel efficiency such as car weight and engine displacement.

How Do We Calculate a Regression Line?

Remember we are taking a set of data points and we are trying to fit it to a line of the form \(y = mx + b\). We all know this is the equation of a simple line. But how do we calculate the slope \(m\) and the y-intercept \(b\)?

The most used method for calculating these values is based on the least squares method. If you’ve taken linear algebra this might sound very familiar as you might have learned the more generalized polynomial regression. There is an entire process involving a matrix equation and a bunch of calculations but luckily the equations for a simple linear regression come out nicely.

The Equations

Now that we have some back story on how this method was derived lets look at some equations!

Here they are:

  • \(a = \bar{y} - b\bar{x}\) (here a is the y-int and b is the slope.)

  • \(\bar{x} = \frac{\Sigma x}{n}\) (\(\bar{x}\) is the sample mean of the x values)

  • \(\bar{y} = \frac{\Sigma y}{n}\) (\(\bar{y}\) is the mean of the y variable)

  • \(b = \frac{\Sigma xy - \frac{\Sigma x\Sigma y}{n}}{\Sigma x^2 - \frac{\Sigma x^2}{n}}\)

If the equation for \(b\) looks familiar that is because the numerator and denominator are standard deviations :)

Lets Look At Cars!

First Let’s import the data set I obtained from kaggle:

library(plotly) 
library(ggplot2)
data <- read.csv("/Users/arosamond/downloads/auto-mpg.csv", header = TRUE)
head(data, 2)
##   mpg cylinders displacement horsepower weight acceleration model.year origin
## 1  18         8          307        130   3504         12.0         70      1
## 2  15         8          350        165   3693         11.5         70      1
##                    car.name
## 1 chevrolet chevelle malibu
## 2         buick skylark 320

We can see that we have multiple variables such as MPG, engine displacement, power, weight, etc. We are going to focus on MPG and how it relates to displacement, weight and power.

Car Weight vs MPG

Let’s look at a scatter plot of car weight vs mpg: We see a definite downward trend of mpg as a car gets heavier which makes perfect sense.

Let’s Add a Regression Line!

Luckily R gives us a function for doing a linear regression:

lin_reg <- lm(mpg ~ weight, data = data)

fig <- plot_ly(x = data$weight, y = data$mpg, type = "scatter", 
               mode = "markers") %>%
  add_lines(x = data$weight, y = fitted(lin_reg))

The lm() function does our linear regression for us. I am using plotly for this one to make it interactive. For the input we chose mpg and weight to compare and then we set the data parameter equal to our imported data set. We can then use the add_lines function on our plotly figure to append the regression line onto the scatter plot.

Plot With The Line

Here we can clearly see the regression line! It isn’t a perfect fit and that is expected as the data points aren’t super concentrated around that line but nonetheless it does show a trend.

How About Engine Displacement

Here I added a fitted curve of form \(y = \frac{a}{x}\) (red curve) to show that the linear regression isn’t always the best way to fit data to an equation.

Lastly, Horsepower

Recap

Linear regression is a very powerful tool and it is very commonly used in statistics and it is especially important in AI and machine learning applications. However, not all data is suited for linear regression.

Remember the mpg vs weight graph, that one was okay but not optimal since there was wide variation around the line. Even worse was mpg vs engine displacement. While there was a downward trend there it definitely wasn’t very much linear at all.

Thi shows that while linear regression can be powerful, it is only as powerful as how “linear” the relationship between the variables are.