Check the first 6 rows of the cars dataset.
head(cars)
## speed dist
## 1 4 2
## 2 4 10
## 3 7 4
## 4 7 22
## 5 8 16
## 6 9 10
#summary(cars$dist)
Summary of linear regression model. The y-intercept is -17.579 and the slope is 3.932. “The residuals are the differences between the actual measured values and the corresponding values on the fitted regression line. A good model’s residual should be roughly balanced around from the mean of zero.”
The standard error should also be five to ten times smaller thand the corresponding coefficient for a good model.
lmcar <-lm(cars$dist ~ cars$speed)
lmcar
##
## Call:
## lm(formula = cars$dist ~ cars$speed)
##
## Coefficients:
## (Intercept) cars$speed
## -17.579 3.932
summary(lmcar)
##
## 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
{plot(cars$speed,cars$dist,main = "1920 Cars speed and number of Feet Taken to Stop",
col = 'navyblue', pch = 16, xlab = "Speed (mph)", ylab = "Stopping Distance (feet)")
abline(lmcar, col = "red")}
Plotting the residuals from above model to analyze
plot(fitted(lmcar),resid(lmcar),col = 'navyblue', pch = 16, main = "The residual values vs. input values: \n well fitted model will have uniformly distribution around zero")
Plotting Q-Q plot: quantile-versus-quantile. Q-Q plot is used for visually examining whether the residuals are normally distributed. Notes: points forming along the qqline indicates normally distributed residuals.
qqnorm(resid(lmcar))
qqline(resid(lmcar), col = "red")