2024-03-21

What is Simple Linear Regression?

Simple Linear Regression is a model used to predict a variable for other given variables. Below is the formula.

\(\mathcal{y} = \alpha + \beta \mathcal{x}\)

  • \(\mathcal {y}\) is the dependent variable we want to find.
  • \(\alpha\) is the y-intercept, used to better fit the graph.
  • \(\beta\) is the slope of the function.
  • \(\mathcal{x}\) is the independent variable used to find the dependent variable.

The data we will use

head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Above is an example of the data we will use. We will first predict the mpg based off the number of cylinders, then predict the mpg based off of the horsepower.

What is the Formula?

The two formulas will be as below

  • \(\text{MPG} = \alpha + \beta \text{Cylinders}\)
  • \(\text{MPG} = \alpha + \beta \text{Horsepower}\)

This will allow us to predict the MPG given two different variables.

Graph of Linear Regression

Clearly the Above graph doesn’t look quite right. The data is all centered in under different categories, showing a limit to Simple Linear Regression. It still shows the strong correlation between lower Cylinders and higher MPG though.

Behind this Graph

#This is used to create the plot with cylinders 
#on the x-axis and mpg on the y-axis.
ggplot(data = mtcars, aes(x = cyl, y = mpg)) 

#This creates the points on the line and then creates 
#a regression line to predict values.
+ geom_point() + geom_smooth(method="lm", se = FALSE) 

#This creates the labels for the graph.
+ labs(x="Cylinders", y="MPG", title="Cylinders vs MPG")

2nd Graph of Linear Regression

This graph allows us to see the power behind a Linear Regression model. It clearly shows the relationship between horsepower and MPG and shows that the lower the horsepower, the higher the MPG.

Behind this Graph

#This is used to create the plot with horsepower 
#on the x-axis and mpg on the y-axis.
ggplot(data = mtcars, aes(x = cyl, y = hp)) 

#This creates the points on the line and then creates 
#a regression line to predict values.
+ geom_point() + geom_smooth(method="lm", se = FALSE) 

#This creates the labels for the graph.
+ labs(x="Horsepower", y="MPG", title="Horsepower vs MPG")

More Advanced Graphs

With more than a simple regression, regressions can add more variables and become even more useful. Many types of regression are used in machine learning and can be used to predict important data.