This slideshow presentation is about simple linear regression using R
2024-03-02
This slideshow presentation is about simple linear regression using R
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\)
The steps for implementing linear regression using R go as follows:
#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
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 ...
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
## ## 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
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)
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
## Warning: `line.width` does not currently support multiple values.
## `geom_smooth()` using formula = 'y ~ x'