The purpose of this assignment is to create a linear regression model and evaluate whether it meets the assumptions of linear regression. The model will examine whether a car’s speed is related to its stopping distance using the built-in cars dataset in R.

data(cars)

model <- lm(dist ~ speed, data = cars)

summary(model)
## 
## 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
# Check linearity

plot(model, which = 1)

lmtest::raintest(model)
## 
##  Rainbow test
## 
## data:  model
## Rain = 0.71288, df1 = 25, df2 = 23, p-value = 0.7954
# Check independence of errors

lmtest::dwtest(model)
## 
##  Durbin-Watson test
## 
## data:  model
## DW = 1.6762, p-value = 0.09522
## alternative hypothesis: true autocorrelation is greater than 0
# Check homoscedasticity

plot(model, which = 3)

lmtest::bptest(model)
## 
##  studentized Breusch-Pagan test
## 
## data:  model
## BP = 3.2149, df = 1, p-value = 0.07297
# Check normality of residuals

qqnorm(residuals(model))
qqline(residuals(model), col = "red")

shapiro.test(residuals(model))
## 
##  Shapiro-Wilk normality test
## 
## data:  residuals(model)
## W = 0.94509, p-value = 0.02152
# Check multicollinearity

cor(cars$speed, cars$dist)
## [1] 0.8068949
# Mitigate non-normal residuals with a square-root transformation

model_sqrt <- lm(sqrt(dist) ~ speed, data = cars)

qqnorm(residuals(model_sqrt))
qqline(residuals(model_sqrt), col = "red")

shapiro.test(residuals(model_sqrt))
## 
##  Shapiro-Wilk normality test
## 
## data:  residuals(model_sqrt)
## W = 0.97332, p-value = 0.3143

Check for Multicollinearity

cor(cars$speed, cars$dist)
## [1] 0.8068949

The correlation between speed and stopping distance is approximately 0.81, indicating a strong positive relationship. Since the model has only one independent variable, multicollinearity is not a concern.

Check for Homoscedasticity

plot(model, which = 1)

The Residuals vs. Fitted plot shows that the residuals are scattered around zero, there is some curvature and a few outliers. There is no obvious funnel shape, so the assumption of constant variance appears reasonably met, but the pattern suggests some caution is needed.

Check for Independence of Errors

plot(residuals(model), type = "b",
     main = "Residuals in Observation Order",
     ylab = "Residuals",
     xlab = "Observation Order")
abline(h = 0, col = "red")

The residuals fluctuate above and below zero without a clear overall trend. This suggests that the independence assumption may be reasonable, although the plot alone cannot confirm independence.

The linear regression model was used to examine the relationship between speed and stopping distance in the cars dataset. The assumption checks showed some concerns with the normality of residuals and possible curvature in the residual plots. The square-root transformation improved the normality of the residuals. Overall, the results suggest that speed is associated with stopping distance, although some caution is needed when interpreting the model.