2024-03-02

Simple Linear Regression

This slideshow presentation is about simple linear regression using R

But First, what is it?

  • Simple Linear Regression is a regression model that depicts a relationship between two continuous variables.

  • The general formula for simple linear regression is:\(y\) \(=\) \(\alpha\) \(+\) \(\beta\) \(x\), where \(x\) and \(y\) are coordinate, \(\alpha\) is the y-intercept and \(\beta\)

Steps for Linear Regression in R

The steps for implementing linear regression using R go as follows:

    1. Import/Install all necessary packages and libraries (ggplot2, plotly)
    2. Load up the data (will be using the ‘cars’ data-set)
    3. Explore the data (pour over the data-set to get a better understanding of it)
    4. Create a model for the desired data
    5. Make a summary of the data model
    6. Formulate a hypothesis
    7. Uses the libraries in step 1 to plot and visualize the data model

Step 1, Install the right packages

  • In order to start, we need to import the necessary libraries
#first we install the packages 
#install.packages("ggplot2")
#install.packages("plotly")
#install.packages("dplyr")

#then we import the libraries 
library(ggplot2) #used for most data visualizations 
library(plotly) #used for for interactive web graphics 
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(dplyr) #used for data manipulation 
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Step 2: Import the dataset

  • Normally, we would import a .csv file to read from using a dataframe, but since ‘cars’ is a pre-installed template, we will be using that. This is a summary of the cars data-set.
str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

Step 4, create a model

  • Now let’s create a model!

  • In order to do so, we need to establish a linear relationship between two variables

  • In this case, we are going to see the relationship between the horsepower (hp) and number of cylinders (cyl) in the engines of cars

Step 5: Model Summary

## 
## Call:
## lm(formula = cyl ~ hp, data = mtcars)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.27078 -0.74879 -0.06417  0.63512  1.74067 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 3.006795   0.425485   7.067 7.41e-08 ***
## hp          0.021684   0.002635   8.229 3.48e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.006 on 30 degrees of freedom
## Multiple R-squared:  0.693,  Adjusted R-squared:  0.6827 
## F-statistic: 67.71 on 1 and 30 DF,  p-value: 3.478e-09
  • This is the data for the model

The data visualized (sorta)

library(ggplot2)

ggplot(mtcars, aes(x = cyl, y = hp, group = cyl, fill = cyl)) + geom_boxplot() + stat_summary(fun = "mean", geom = "point",shape = 8, size = 2) 

Step 6, make predictions

  • The next step is to make predictions with the data we’ve got so far.

  • The formula for predicting values: \(\hat{y}\) \(=\) \(ax + b\), where \(\hat{y}\) can be used to predict any \(x\)

  • For instance, we can use the predict() function in R to see how many cylinders we would need to get 1000 horsepower out of an engine

newData <- data.frame(hp = 1000)
predictedHP <- predict(dataModel, newData)
print(predictedHP)
##        1 
## 24.69034
  • According to this data, we need an engine 25 cylinders to acheive 1000 horsepower

Step 7, visualize the model by plotting the data

## Warning: `line.width` does not currently support multiple values.

Complete chart (with ggplot)

## `geom_smooth()` using formula = 'y ~ x'

Conclusion

  • What we can derive from this data is that there is a positive correlation between the number of cylinders in an engine and the horsepower it can produce. The more cylinders an engine has, the more horsepower it can produce.