Load the dataset

library(ISLR2) data(“Carseats”)

Fit the model

model_full <- lm(Sales ~ Price + Urban + US, data = Carseats)

View the summary

summary(model_full)

Coefficients: (Intercept) 13.043 Price -0.054 UrbanYes -0.021 USYes 1.200

View the coefficients of the full model

coef(model_full)

Intercept Price UrbanYes USYes 13.043 -0.054 -0.021 1.200

Sales= 13.043 − 0.054 ⋅ Price − 0.02 2 ⋅UrbanYe s + 1 .20 1 ⋅USYes

model_reduced <- lm(Sales ~ Price + US, data = Carseats) summary(model_reduced)

Adjusted R-squared

summary(model_full)\(adj.r.squared summary(model_reduced)\)adj.r.squared

ANOVA comparison

anova(model_reduced, model_full)

confint(model_reduced) 2.5 % 97.5 % (Intercept) 11.763698 14.323239 Price -0.064617 -0.044300 USYes 0.688997 1.712149

Diagnostic plots

par(mfrow = c(2, 2)) plot(model_reduced)

Influence measures

influence.measures(model_reduced)

High leverage points

hatvalues <- hatvalues(model_reduced) plot(hatvalues, main=“Leverage Values”) abline(h = 2 * mean(hatvalues), col = “red”)