The lasso is less flexible than least squares because it shrinks some coefficients, and some can become zero. It can improve prediction accuracy when the decrease in variance is greater than the increase in bias.
2(b) Ridge regression compared with least squares
Correct answer: iii.
Ridge regression is less flexible than least squares because it shrinks the coefficients toward zero. It can improve prediction accuracy when the decrease in variance is greater than the increase in bias.
2(c) Non-linear methods compared with least squares
Correct answer: ii.
Non-linear methods are more flexible than least squares because they can fit more complex patterns. They can improve prediction accuracy if the increase in variance is smaller than the decrease in bias.Question 9: College Applications
The goal is to predict Apps, the number of applications received by each college.
data(College)college <- College# Look at the data.dim(college)
[1] 777 18
head(college)
Private Apps Accept Enroll Top10perc Top25perc
Abilene Christian University Yes 1660 1232 721 23 52
Adelphi University Yes 2186 1924 512 16 29
Adrian College Yes 1428 1097 336 22 50
Agnes Scott College Yes 417 349 137 60 89
Alaska Pacific University Yes 193 146 55 16 44
Albertson College Yes 587 479 158 38 62
F.Undergrad P.Undergrad Outstate Room.Board Books
Abilene Christian University 2885 537 7440 3300 450
Adelphi University 2683 1227 12280 6450 750
Adrian College 1036 99 11250 3750 400
Agnes Scott College 510 63 12960 5450 450
Alaska Pacific University 249 869 7560 4120 800
Albertson College 678 41 13500 3335 500
Personal PhD Terminal S.F.Ratio perc.alumni Expend
Abilene Christian University 2200 70 78 18.1 12 7041
Adelphi University 1500 29 30 12.2 16 10527
Adrian College 1165 53 66 12.9 30 8735
Agnes Scott College 875 92 97 7.7 37 19016
Alaska Pacific University 1500 76 72 11.9 2 10922
Albertson College 675 67 73 9.4 11 9727
Grad.Rate
Abilene Christian University 60
Adelphi University 56
Adrian College 54
Agnes Scott College 59
Alaska Pacific University 15
Albertson College 55
9(a) Split the data into training and test sets
We will randomly place approximately 70% of the observations in the training set and 30% in the test set.
The cross-validated ridge lambda is 314.0251.
The ridge test MSE is 2.9814658^{6}.
The ridge test RMSE is 1726.69 applications.
9(d) Lasso regression
For the lasso, alpha = 1.
set.seed(123)lasso_cv <-cv.glmnet( x_train, y_train,alpha =1)best_lasso_lambda <- lasso_cv$lambda.minlasso_predictions <-predict( lasso_cv,s ="lambda.min",newx = x_test)lasso_test_mse <-mean( (y_test - lasso_predictions)^2)lasso_test_rmse <-sqrt(lasso_test_mse)lasso_coefficients <-coef( lasso_cv,s ="lambda.min")# Subtract 1 so the intercept is not counted.lasso_nonzero <-sum(lasso_coefficients !=0) -1best_lasso_lambda
[1] 8.149028
lasso_test_mse
[1] 1730230
lasso_test_rmse
[1] 1315.382
lasso_nonzero
[1] 16
plot(lasso_cv)
The cross-validated lasso lambda is 8.149.
The lasso test MSE is 1.73023^{6}.
The lasso test RMSE is 1315.38 applications.
The lasso selected 16 non-zero predictor coefficients.
9(e) Principal components regression
PCR selects the number of components using cross-validation.
# RMSEP() includes a 0-component model first.pcr_cv_rmse <-RMSEP(college_pcr, estimate ="CV")$val[1, 1, ]best_pcr_m <-which.min(pcr_cv_rmse[-1])best_pcr_m
# RMSEP() includes a 0-component model first.pls_cv_rmse <-RMSEP(college_pls, estimate ="CV")$val[1, 1, ]best_pls_m <-which.min(pls_cv_rmse[-1])best_pls_m
The model with the lowest test MSE is Lasso. Its test RMSE is about 1315.38 applications.
I compared the models using their test errors because they show how well each model predicts new data. If the test errors are very similar, I would choose the simpler model.
Question 11: Boston Crime Rate
The response variable is crim, the per capita crime rate by town.
First, I fit the best subset selection model using the training data. Then I used 10-fold cross-validation to choose the best model size. After that, I tested the final model using the test data.
Based on this training and test split, Ridge had the lowest test MSE, so I would choose this model.
I used the test error to compare the models because it shows how well they predict new data. If two models have very similar test errors, I would choose the simpler one because it is easier to understand and gives similar results.
11(c) Does the chosen model use all features?
# Variables selected by the lassolasso_selected_names <-rownames(boston_lasso_coef)[as.vector(boston_lasso_coef !=0)]# Remove the intercept from the displayed list.lasso_selected_names <-setdiff( lasso_selected_names,"(Intercept)")lasso_selected_names
The final model does not have to use every predictor. In this analysis, the lasso selected 10 predictors, and the best subset model selected 9 predictors. Using fewer predictors can make the model simpler while still giving good prediction results.