This question involves the use of simple linear regression on the Auto data set.
library(ISLR2)
## Warning: package 'ISLR2' was built under R version 4.4.2
data(Auto) # Loads the dataset
# Fit the regression model
model <- lm(mpg ~ horsepower, data = Auto)
# Print the summary of the model
summary(model)
##
## Call:
## lm(formula = mpg ~ horsepower, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -13.5710 -3.2592 -0.3435 2.7630 16.9240
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 39.935861 0.717499 55.66 <2e-16 ***
## horsepower -0.157845 0.006446 -24.49 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.906 on 390 degrees of freedom
## Multiple R-squared: 0.6059, Adjusted R-squared: 0.6049
## F-statistic: 599.7 on 1 and 390 DF, p-value: < 2.2e-16
# Create the scatter plot
plot(Auto$horsepower, Auto$mpg,
main = "MPG vs Horsepower",
xlab = "Horsepower",
ylab = "MPG",
col = "blue",
pch = 16)
# Add the least squares regression line
abline(model, col = "red", lwd = 2)
The diagnostic plots reveal several issues with the linear regression model predicting mpg from horsepower. The Residuals vs. Fitted plot shows a clear curved pattern, suggesting that the relationship between mpg and horsepower is non-linear, meaning a simple linear model may not be the best fit. Additionally, the Q-Q plot indicates that the residuals deviate significantly from normality, especially in the tails, which suggests the presence of outliers or skewness in the data. The Scale-Location plot further reveals signs of heteroscedasticity, where the variance of residuals increases as fitted values increase, indicating that the model does not explain variability in mpg equally across all levels of horsepower. Finally, the Residuals vs. Leverage plot identifies high-leverage points, such as observations 334, 387, and 17, which may have an outsized influence on the model. These issues suggest that improvements could be made by adding non-linear terms, applying a log transformation to mpg, or using a weighted least squares regression to stabilize variance.
# Generate diagnostic plots
par(mfrow = c(2, 2)) # Arrange plots in a 2x2 grid
plot(model)
par(mfrow = c(1, 1)) # Reset layout
This question should be answered using the Carseats data set.
# Load the Carseats dataset
data(Carseats)
# Fit the multiple regression model
model <- lm(Sales ~ Price + Urban + US, data = Carseats)
# Display the regression summary
summary(model)
##
## Call:
## lm(formula = Sales ~ Price + Urban + US, data = Carseats)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.9206 -1.6220 -0.0564 1.5786 7.0581
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 13.043469 0.651012 20.036 < 2e-16 ***
## Price -0.054459 0.005242 -10.389 < 2e-16 ***
## UrbanYes -0.021916 0.271650 -0.081 0.936
## USYes 1.200573 0.259042 4.635 4.86e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.472 on 396 degrees of freedom
## Multiple R-squared: 0.2393, Adjusted R-squared: 0.2335
## F-statistic: 41.52 on 3 and 396 DF, p-value: < 2.2e-16
Provide an interpretation of each coeffcient in the model. Be careful—some of the variables in the model are qualitative!
Since the p-value is extremely small (< 0.001), we reject the null hypothesis H0:β1=0H_0: _1 = 0H0:β1=0, meaning that Price significantly affects Sales.
Interpretation: For each $1 increase in Price, Sales decrease by 0.0545 units, holding all else constant.
The negative sign confirms an inverse relationship between Price and Sales.
The p-value is very high (0.936), meaning that
Urban
does not significantly affect
Sales.
Interpretation: Whether the store is located in an urban or rural area does not have a meaningful impact on Sales.
Actionable Insight: Since this variable is not significant, we might consider removing it from the model.
The p-value is very small (< 0.001), meaning that we reject the null hypothesis H0:β3=0H_0: _3 = 0H0:β3=0, confirming that US location significantly affects Sales.
Interpretation: Stores in the US have 1.2 more units of Sales on average compared to non-US stores, holding all else constant.
This suggests that US stores generally have higher Sales than international stores.
Write out the model in equation form, being careful to handle the qualitative variables properly.
Sales=13.04−0.0545×Price−0.0219×UrbanYes+1.2006×USYes
For which of the predictors can you reject the null hypothesis \(H_0: \beta_j = 0\)?
We can reject the null hypothesis for Price
and
US
, meaning they significantly affect Sales.
On the basis of your response to the previous question, fit a smaller model that only uses the predictors for which there is evidence of association with the outcome.
# Fit the reduced multiple regression model (excluding Urban)
reduced_model <- lm(Sales ~ Price + US, data = Carseats)
# Display the summary of the new model
summary(reduced_model)
##
## Call:
## lm(formula = Sales ~ Price + US, data = Carseats)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.9269 -1.6286 -0.0574 1.5766 7.0515
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 13.03079 0.63098 20.652 < 2e-16 ***
## Price -0.05448 0.00523 -10.416 < 2e-16 ***
## USYes 1.19964 0.25846 4.641 4.71e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.469 on 397 degrees of freedom
## Multiple R-squared: 0.2393, Adjusted R-squared: 0.2354
## F-statistic: 62.43 on 2 and 397 DF, p-value: < 2.2e-16
How well do the models in (a) and (e) fit the data?
Both models fit the data equally well, but Model (e) is preferable because RSE is slightly lower, Adjusted \(R^2\) is slightly higher, The F-statistic is higher, and The Urban variable was not significant.
Using the model from (e), obtain 95 % confdence intervals for the coeffcient(s).
# Obtain 95% confidence intervals for the coefficients
confint(reduced_model, level = 0.95)
## 2.5 % 97.5 %
## (Intercept) 11.79032020 14.27126531
## Price -0.06475984 -0.04419543
## USYes 0.69151957 1.70776632
plot(reduced_model, which = 4) # Cook's Distance plot
This problem focuses on the collinearity problem.
set.seed(1)
x1 <- runif(100)
x2 <- 0.5 * x1 + rnorm(100) / 10
y <- 2 + 2 * x1 + 0.3 * x2 + rnorm(100)
The last line corresponds to creating a linear model in which y is a function of x1 and x2. Write out the form of the linear model. What are the regression coeffcients?
# Fit the linear model
model <- lm(y ~ x1 + x2)
# Display the summary to get regression coefficients
summary(model)
##
## Call:
## lm(formula = y ~ x1 + x2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.8311 -0.7273 -0.0537 0.6338 2.3359
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.1305 0.2319 9.188 7.61e-15 ***
## x1 1.4396 0.7212 1.996 0.0487 *
## x2 1.0097 1.1337 0.891 0.3754
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.056 on 97 degrees of freedom
## Multiple R-squared: 0.2088, Adjusted R-squared: 0.1925
## F-statistic: 12.8 on 2 and 97 DF, p-value: 1.164e-05
# Compute correlation
correlation <- cor(x1, x2)
print(paste("Correlation between x1 and x2:", round(correlation, 2)))
## [1] "Correlation between x1 and x2: 0.84"
# Create scatterplot
plot(x1, x2, main = paste("Scatterplot of x1 vs x2 (Correlation:", round(correlation, 2), ")"),
xlab = "x1", ylab = "x2", pch = 19, col = "blue")
grid()
x1 is statistically significant (p=0.0487p = 0.0487p=0.0487), though close to the 0.05 threshold.
x2x_2x2 is not significant (p=0.3754p = 0.3754p=0.3754), likely due to multicollinearity with x1x_1x1.
# Extract p-values
summary(model)$coefficients[, 4]
## (Intercept) x1 x2
## 7.606713e-15 4.872517e-02 3.753565e-01
library(car)
## Loading required package: carData
vif(model)
## x1 x2
## 3.304993 3.304993
# Fit least squares regression using only x1
model_x1 <- lm(y ~ x1)
# Display summary for model_x1
summary(model_x1)
##
## Call:
## lm(formula = y ~ x1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.89495 -0.66874 -0.07785 0.59221 2.45560
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.1124 0.2307 9.155 8.27e-15 ***
## x1 1.9759 0.3963 4.986 2.66e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.055 on 98 degrees of freedom
## Multiple R-squared: 0.2024, Adjusted R-squared: 0.1942
## F-statistic: 24.86 on 1 and 98 DF, p-value: 2.661e-06
# Extract p-value for H0: β1 = 0
summary(model_x1)$coefficients["x1", 4]
## [1] 2.660579e-06
# Fit least squares regression using only x2
model_x2 <- lm(y ~ x2)
# Display summary for model_x2
summary(model_x2)
##
## Call:
## lm(formula = y ~ x2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.62687 -0.75156 -0.03598 0.72383 2.44890
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.3899 0.1949 12.26 < 2e-16 ***
## x2 2.8996 0.6330 4.58 1.37e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.072 on 98 degrees of freedom
## Multiple R-squared: 0.1763, Adjusted R-squared: 0.1679
## F-statistic: 20.98 on 1 and 98 DF, p-value: 1.366e-05
# Extract p-value for H0: β1 = 0 in x2 model
summary(model_x2)$coefficients["x2", 4]
## [1] 1.36643e-05
x1 <- c(x1, 0.1)
x2 <- c(x2, 0.8)
y <- c(y, 6)
Re-ft the linear models from (c) to (e) using this new data. What efect does this new observation have on the each of the models? In each model, is this observation an outlier? A high-leverage point? Both? Explain your answers
# Add the new observation
x1 <- c(x1, 0.1)
x2 <- c(x2, 0.8)
y <- c(y, 6)
# Fit full model
model_full <- lm(y ~ x1 + x2)
# Display summary
summary(model_full)
##
## Call:
## lm(formula = y ~ x1 + x2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.73348 -0.69318 -0.05263 0.66385 2.30619
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.2267 0.2314 9.624 7.91e-16 ***
## x1 0.5394 0.5922 0.911 0.36458
## x2 2.5146 0.8977 2.801 0.00614 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.075 on 98 degrees of freedom
## Multiple R-squared: 0.2188, Adjusted R-squared: 0.2029
## F-statistic: 13.72 on 2 and 98 DF, p-value: 5.564e-06
# Fit model using only x1
model_x1 <- lm(y ~ x1)
# Display summary
summary(model_x1)
##
## Call:
## lm(formula = y ~ x1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.8897 -0.6556 -0.0909 0.5682 3.5665
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.2569 0.2390 9.445 1.78e-15 ***
## x1 1.7657 0.4124 4.282 4.29e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.111 on 99 degrees of freedom
## Multiple R-squared: 0.1562, Adjusted R-squared: 0.1477
## F-statistic: 18.33 on 1 and 99 DF, p-value: 4.295e-05
# Fit model using only x2
model_x2 <- lm(y ~ x2)
# Display summary
summary(model_x2)
##
## Call:
## lm(formula = y ~ x2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.64729 -0.71021 -0.06899 0.72699 2.38074
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.3451 0.1912 12.264 < 2e-16 ***
## x2 3.1190 0.6040 5.164 1.25e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.074 on 99 degrees of freedom
## Multiple R-squared: 0.2122, Adjusted R-squared: 0.2042
## F-statistic: 26.66 on 1 and 99 DF, p-value: 1.253e-06
# Compute leverage values for the full model
leverage_values <- hatvalues(model_full)
# Extract leverage for the new observation (last data point)
new_obs_index <- length(y)
leverage_values[new_obs_index]
## 101
## 0.4147284
# Residuals vs. Leverage plot
plot(model_full, which = 5)