Using the “cars” dataset in R, build a linear model for stopping distance as a function of speed and replicate the analysis of your textbook chapter 3 (visualization, quality evaluation of the model, and residual analysis).
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.4.3
## -- Attaching packages ---------------------------------- tidyverse 1.2.1 --
## v ggplot2 2.2.1 v purrr 0.2.4
## v tibble 1.4.1 v dplyr 0.7.4
## v tidyr 0.7.2 v stringr 1.2.0
## v readr 1.1.1 v forcats 0.2.0
## Warning: package 'tibble' was built under R version 3.4.3
## Warning: package 'tidyr' was built under R version 3.4.3
## Warning: package 'readr' was built under R version 3.4.3
## Warning: package 'purrr' was built under R version 3.4.3
## Warning: package 'dplyr' was built under R version 3.4.2
## Warning: package 'forcats' was built under R version 3.4.3
## -- Conflicts ------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
Take a look at the cars dataset with the glimpse function from the tidyverse
glimpse(cars)
## Observations: 50
## Variables: 2
## $ speed <dbl> 4, 4, 7, 7, 8, 9, 10, 10, 10, 11, 11, 12, 12, 12, 12, 13...
## $ dist <dbl> 2, 10, 4, 22, 16, 10, 18, 26, 34, 17, 28, 14, 20, 24, 28...
We see that we have 2 variables, (speed, dist) and 50 observations.
First, using the summary feature in R we will view several descriptive statistics.
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
According to https://www.youtube.com/watch?v=KsVBBJRb9TE&feature=youtu.be the x variable is the explanatory variable, while the y variable is the response variable. In this case, the explanatory variable is speed while the response variable is dist
Next we will plot the two variables
plot(cars$speed, cars$dist,
xlab="Speed (MPH)",
ylab="Distance FT (Stopping)",
main="Stopping Distance vs. Speed",
col="blue")
Using the lm() function we will create the relationship model between the variables.
cat("The Relationship Model Between Speed & Distance","\n")
## The Relationship Model Between Speed & Distance
(lm_cars <-lm(cars$dist ~ cars$speed))
##
## Call:
## lm(formula = cars$dist ~ cars$speed)
##
## Coefficients:
## (Intercept) cars$speed
## -17.579 3.932
Visualize the Regression In this plot we add the abline which is based off of our findings from lm_cars
plot(cars$speed, cars$dist,
xlab="Speed (MPH)",
ylab="Distance FT (Stopping)",
main="Stopping Distance vs. Speed",
col="blue")
abline(lm_cars)
Next, using the summary feature in R we will view the descriptive statistics for lm_cars
summary(lm_cars)
##
## Call:
## lm(formula = cars$dist ~ cars$speed)
##
## Residuals:
## Min 1Q Median 3Q Max
## -29.069 -9.525 -2.272 9.215 43.201
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -17.5791 6.7584 -2.601 0.0123 *
## cars$speed 3.9324 0.4155 9.464 1.49e-12 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 15.38 on 48 degrees of freedom
## Multiple R-squared: 0.6511, Adjusted R-squared: 0.6438
## F-statistic: 89.57 on 1 and 48 DF, p-value: 1.49e-12
Residual Analysis
plot(lm_cars$fitted.values, lm_cars$residuals,
xlab = "Fitted Values",
ylab = "Residuals")
abline(0,0)