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.)
Load required libraries
library(tidyverse)
Take a glimpse at the dataset
glimpse(cars)
## Rows: 50
## Columns: 2
## $ speed <dbl> 4, 4, 7, 7, 8, 9, 10, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13, 13…
## $ dist <dbl> 2, 10, 4, 22, 16, 10, 18, 26, 34, 17, 28, 14, 20, 24, 28, 26, 34…
We can see that the dataset has 50 rows and 2 columns (speed and dist).
Summary for the dataset
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
Visualize the data
p = ggplot(cars, aes(x=speed, y=dist)) + geom_point() + theme(panel.grid.major = element_line(colour = "lemonchiffon3"),
panel.grid.minor = element_line(colour = "lemonchiffon3"),
axis.title = element_text(size = 13),
axis.text = element_text(size = 11),
axis.text.x = element_text(family = "sans",
size = 11), axis.text.y = element_text(family = "sans",
size = 11), plot.title = element_text(size = 15,
hjust = 0.5), panel.background = element_rect(fill = "gray85"),
plot.background = element_rect(fill = "antiquewhite")) +labs(title = "Stopping Distance vs Speed",
x = "Speed", y = "Stopping Distance")
p
From the scatter plot, as the speed increases, the stopping distance tend to increase as well.
Build the Model
lm_cars = lm(cars$dist ~ cars$speed)
lm_cars
##
## Call:
## lm(formula = cars$dist ~ cars$speed)
##
## Coefficients:
## (Intercept) cars$speed
## -17.579 3.932
From the output of the model, the linear function is given by:
\(dist = 3.932 * speed - 17.579\)
Evaluate the Model - Quality Evaluation
# Check the summary of the model
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
The values from the summary show that there is little variability in the data and the result is statistically significant.
Residual Analysis
Residual vs Fitted plot
plot(fitted(lm_cars),resid(lm_cars), main="Residuals vs Fitted", xlab = "Fitted", ylab = "Residuals")
abline(0, 0)
From this plot, we can say that there seems to be constant variance for the residuals and the data is not heteroscedacity.
QQ Plot
qqnorm(resid(lm_cars))
qqline(resid(lm_cars))
From the Q-Q plot, we can see that the residuals follow a nearly normal distribution.
Entire plot
par(mfrow=c(2,2))
plot(lm_cars)
From the residual analysis, we can see that the conditions for linear regression are satisfied for this data and it can be fitted by using a linear model approach. Although I am not satisfied with the R-squared value of about 0.65, acceptable values for the R-squared value is highly dependent on the use case of the model.