Question 1
You have been hired as a data analyst by the police department in your city. They ask you to run a regression analysis that allows them to predict the speed at which a car was driving based on its stopping distance. The police department states that it is easy for them to measure the stopping distance of cars involved in accidents but it is hard for them to figure out the distance at which they were traveling. Therefore, they want your help to find an equation that allows them to predict speed based on distance.
?cars
## starting httpd help server ... done
cars
str(cars)
## 'data.frame': 50 obs. of 2 variables:
## $ speed: num 4 4 7 7 8 9 10 10 10 11 ...
## $ dist : num 2 10 4 22 16 10 18 26 34 17 ...
Y (dependent variable): speed (the speed at which cars were traveling in an accident) X (independent variable): dist (the stopping distance of cars involved in accidents)
cor(cars$speed, cars$dist)
## [1] 0.8068949
The values of r reveal a fairly positive correlation of 0.81 between the distance of a car traveled and its speed. This correlation could symbolize a statistical and perhaps practical significance. This implies that the larger the distance (ft) it takes a car to stop before impact the higher the speed (mph) at which the car was traveling.
plot(cars$dist, cars$speed, xlab = "Distance", ylab="Speed")
# If you want to include the least square line in the plot, add the following:
abline(lm(speed ~ dist, data = cars), col = "red")
We can confirm from the scatter plot has a positive slope and therefore, the relationship is indeed a positive relation as previously revealed by r.
The scatter plot shows us that the linear relationship is acceptable (it fits the data points quite well).We conclude that it is reasonable to continue with the linear regression analysis even with the presence of a few outliers.
speed_simple_R = lm(speed ~ dist, data=cars)
summary(speed_simple_R)
##
## 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
Predicted speed = 8.28391 + 0.16557 * (stop distance ft)
Ho: The slope of the estimated equation is NOT statistically different from zero; thus, there is no linear relationship between dist and speed.
Ha: The slope of the estimated equation is statistically different from zero; thus, there is a linear relationship between dist and speed.
The p-value that results from the test for the b1 coefficient is very small (p-value < 1.49e-12). This p-value is less than alpha (0.05). Therefore, we can reject Ho and support Ha. The data give us evidence to conclude that there is a statistically significant linear relationship between dist and speed.
The slope of the equation is 0.16557, so when the braking distance increases by 1 ft, we expect the speed to increase on average by 0.16557 mph.
#RSE
summary(speed_simple_R)$sigma
## [1] 3.155753
The RSE is 3.156 mph. On average the values of speed deviate from the regression equation by 3.156 mph. Therefore, we can assume our prediction errors to be off by 3.156 mph. The result of the RSE is smaller than 5 mph.
#proportion
3.156/mean(cars$speed)
## [1] 0.2049351
#percentage
(3.156/mean(cars$speed))*100
## [1] 20.49351
The coefficient of variation is 20.5% > 20% which is high as a desirable outcome ((we want the magnitude of the error to be at most 20% compared to a typical value of Y). In this problem, an RSE of 3.156 mph does NOT seem to be as low as would be desire.
The equation is statistically significant since the p-value 1.49e-12 is smaller than alpha (0.05). The equation is better than the sample mean to make predictions of speed but, the RSE of 3.156 mph is not as good as desired since its coefficient of variation of 20.5% is too high and a 10% or less would be better.
prediction5 = predict(speed_simple_R, newdata = cars[5,])
prediction5
## 5
## 10.93299
The equation finds the speed of the 5th car that stopped at a distance of 16 feet to be 10.93 mph
6a) Compute the value of the prediction error for the prediction that you just made
actual_speed = cars$speed[5]
prediction_error = sqrt(mean((prediction5 - actual_speed)^2))
prediction_error
## [1] 2.932987
prediction = predict(speed_simple_R, data.frame(dist=c(12)))
prediction
## 1
## 10.27072
Using the equation it is predicted that the speed at which the car was traveling was of 10.27 mph having a stopping distance of 12 ft.
plot(predict(speed_simple_R), residuals(speed_simple_R), xlab = "Predict", ylab="Residuals")
abline(h=0, col = "red")
The graph shows that the residuals show a nonlinear pattern. The data points seem to spread out; therefore, the linear model might NOT be the most appropriate and showing that ( assumption 1 is not satisfied). We had already observed some scattered point and outliers when we did the original scatter plot. However, this graph shows it more clearly.
Ho: The residuals follow a Normal distribution Ha: The residuals do NOT follow a Normal distribution
shapiro.test(residuals(speed_simple_R))
##
## Shapiro-Wilk normality test
##
## data: residuals(speed_simple_R)
## W = 0.98745, p-value = 0.8696
p-value = 0.8696, which is a lot larger than alpha (0.05). Thus, we fail to reject Ho in favor of Ha. The data is giving us evidence that the residuals follow a Normal distribution. Assumption 3 is satisfied.