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
cor(cars$dist,cars$speed)## [1] 0.8068949
Looking at the data of the cars dataset and running the cor function we see that there is a high correlation between the speed and stopping distance.
cars_lm <- lm(cars$dist ~ cars$speed)
summary(cars_lm)##
## 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
Looking at the summary of the linear model we see that there is a R-squared of .6511 meaning that this model explains around 65% of the data’s variation.
Our standard error for speed (0.4155) is 9.5 times less than the coefficient value (3.9324) which is good since a good model has the standard error that is at least 5 to 10 times smaller than the coefficient.
plot(cars$speed, cars$dist, xlab = "Speed (mph)", ylab = "Stopping Distance (ft)",main="Relationship Between Speed and Stopping Distance")
abline(cars_lm) Our least squares regression line is:
\[stopping distance = -17.58 + 3.93(speed)\]
plot(cars_lm$residuals ~ cars$speed, xlab='Fitted Values', ylab='Residuals',main="Speed vs Linear Model Residuals")
abline(h=0, lty=3) Our residuals look to be uniformally scattered above and below 0. This says that speed is factor in the stopping distance and can explain the data.
qqnorm(cars_lm$residuals)
qqline(cars_lm$residuals) The Q-Q plot also shows that speed is a factor in stopping distance as it is almost in a straight line.