1). 1. Import the Boston housing data set (boston.csv).
library(readr) boston <- read_csv(“data/boston-1.csv”)
head(boston)
- Split the data into a training set and test set using a 70-30%
split. Be sure to include the set.seed(123) so that your train and test
sets are the same size as mine and statify by cmedv.
set.seed(123) split <- intial_split(boston, prop = 0.7, strata =
cmedv) train <- training(split) test <- testing(split)
- Using the training data, assess the correlation between cmedv and
all the predictor variables. Hint: you can generate a correlation matrix
for all variables at one time with cor(). Which predictor variable has
the strongest positive correlation? Which predictor variable has the
strongest negative correlation?
correlation_train <- cor(train)
cmedv_cor <- correlation_train[ , c(“cmedv”)]
- Based on the results in #3, plot the relationship between cmedv and
the predictor variable with the strongest positive correlation. Add a
“best fit” regression line that illustrates the linear relationship. Do
you feel like this line accurately captures the relationship?
ggplot(train, aes(rm, cmedv)) + geom_point(alpha = 0.2) +
geom_smooth(method = “lm”, se = FALSE) + scale_y_continuous(labels =
scales::comma) + scale_x_continuous(labels= scales::comma)
- Train a simple linear regression model with the predictor variables
identified above as having the strongest positive correlation. Explain
the coefficient estimate for this predictor variable. Is the coefficient
statistically different than zero?
model1 <- linear_reg() %>% fit(cmedv ~ rm, data = train)
- Compute the generalization RMSE for the above model. Interpret the
results.
residuals <- model1 %>% predict(train) %>% bind_cols(train)
%>% select(rm, cmedv, .pred) %>% mutate(residual = cmedv -
.pred)
Part 2: Multiple Linear Regression
- Fit a linear regression model using all available features to
predict cmedv. Which predictor variables are statistically different
than zero at the p.value < 0.05 level? Interpret the lstat
coefficient.
model3 <- linear_reg() %>% fit(cmedv ~ ., data = train)
tidy(model3) %>% arrange(desc(p.value))
- Compute the generalization RMSE for the model with all predictor
variables. What is the test set RMSE? Is this better than the model that
just used the rm predictor variable?
model3 %>% predict(test) %>% bind_cols(test) %>% rmse(truth
= cmedv, estimate = .pred)
- Using the full model with all predictor variables, identify the top
5 most influential predictor variables in our model.
model3 %>% vip::vip(num_features = 20)