library(performance)
## Warning: package 'performance' was built under R version 4.1.3
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.)
head(cars)
## speed dist
## 1 4 2
## 2 4 10
## 3 7 4
## 4 7 22
## 5 8 16
## 6 9 10
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
To start, we plot speed on the x-axis and stopping distance on the y-axis for a quick check on a potential linear relationship.
plot(cars[,"speed"],cars[,"dist"], main="cars",
xlab="Speed", ylab="Stopping Distance")
We see a linear relationship, if an imperfect one. Let’s construct a linear model:
cars_lm <- lm(speed ~ dist, data=cars)
Initial look at the model:
cars_lm
##
## Call:
## lm(formula = speed ~ dist, data = cars)
##
## Coefficients:
## (Intercept) dist
## 8.2839 0.1656
Let’s add the abline from our metric to that initial plot we generated:
plot(speed ~ dist, data=cars)
abline(cars_lm)
Model performance metrics:
summary(cars_lm)
##
## Call:
## lm(formula = speed ~ dist, data = cars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -7.5293 -2.1550 0.3615 2.4377 6.4179
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 8.28391 0.87438 9.474 1.44e-12 ***
## dist 0.16557 0.01749 9.464 1.49e-12 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.156 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
Our coefficient for our stopping distance is roughly 10x the standard error, which is a good sign, as is the y-intercept coefficient and SE. The very low p-value shows very strong evidence of a linear relationship between speed and stopping distance. the R-squared value shows that roughly 65% of variation in stopping distance can be explained by speed.
Now we analyze residuals and other assumptions for effective linear regression.
plot(fitted(cars_lm),resid(cars_lm))
Residuals seem to vary uniformly - there’s no clear patter of variance.
qqnorm(resid(cars_lm))
qqline(resid(cars_lm))
Q-Q plot’s straight line shows that residuals are normally distributed around a mean of 0.
The performance package in R includes a nice check_model function that outputs performance metrics like discussed above, and they automatically vary based on the time of model used.
check_model(cars_lm)
For a real world model, we would have obviously done a train-test split and saved sample data for testing the model built on our training data.