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 the Data
head(cars)
##   speed dist
## 1     4    2
## 2     4   10
## 3     7    4
## 4     7   22
## 5     8   16
## 6     9   10
# Plotting Data
# In this linear model, speed is the independent variable and stopping distance is the dependent variable.
plot(cars, xlab = "Speed", ylab = "Stopping distance")

## Linear Regression Model linear model is based on a single factor regression. speed is the independent variable and stopping distance is the dependent variable.

cars.lm <- lm(dist ~ speed, data = cars)
summary(cars.lm)
## 
## Call:
## lm(formula = dist ~ speed, data = cars)
## 
## 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 *  
## 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

Summary

The residuals distribution suggests that the distribution is normal.

The standard error for the speed coefficient is ~ 9.4 (3.93/.42) times the coefficient value, which is good. For a good model, we typically would like to see a standard error that is at least five to ten times smaller than the corresponding coefficient.

The probability that the speed coefficient is not relevant in the model is 1.49e-12 (p-value), which means that speed is very relevant in modeling stopping distance.

The p-value of the intercept is 0.0123, which means the intercept is pretty relevant in the model.

The multiple R-squared is 0.6511, which means that this model explains 65.11% of the data’s variation.

Linear Model Plotting

plot(cars, xlab = "Speed", ylab = "Stopping distance")
abline(cars.lm)

## Residuals Plotting The residuals appear to be uniformly scattered above and below zero in the below plot.

plot(fitted(cars.lm), resid(cars.lm))

## Normal Q-Q Plot

qqnorm(resid(cars.lm))
qqline(resid(cars.lm))

## The plot above suggests that there’s some skew to the right.