



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Introduction to Statistical Learning (James/Witten/Hastie/Tibshirani)
Typology: Exercises
1 / 5
This page cannot be seen from the preview
Don't miss anything!




library (ISLR) set.seed (1)
(a). Split the data set into a training set and a test set.
train.size = dim (College)[1] / 2 train = sample (1: dim (College)[1], train.size) test = -train College.train = College[train, ] College.test = College[test, ]
(b). Fit a linear model using least squares on the training set, and report the test error obtained
lm.fit = lm (Apps~., data=College.train) lm.pred = predict (lm.fit, College.test) mean ((College.test[, "Apps"] - lm.pred)^2)
The test MSE is 1108531.
(c). Fit a ridge regression model on the training set, with λ chosen by cross validation. Report the test error obtained.
library (glmnet)
_## Loading required package: Matrix
train.mat = model.matrix (Apps~., data=College.train) test.mat = model.matrix (Apps~., data=College.test) grid = 10 ^ seq (4, -2, length=100) mod.ridge = cv.glmnet (train.mat, College.train[, "Apps"], alpha=0, lambda=grid, thresh=1e-12) lambda.best = mod.ridge$lambda.min
The best λ for ridge regression is 0.
ridge.pred = predict (mod.ridge, newx=test.mat, s=lambda.best) mean ((College.test[, "Apps"] - ridge.pred)^2)
The test MSE for ridge regression is 1108511.6962057, which is slightly smaller than linear regression
(d). Fit a lasso model on the training set, with λ chosen by crossvalidation. Report the test error obtained, along with the number of non-zero coefficient estimates.
mod.lasso = cv.glmnet (train.mat, College.train[, "Apps"], alpha=1, lambda=grid, thresh=1e-12) lambda.best = mod.lasso$lambda.min
The best λ for the lasso is 28.
lasso.pred = predict (mod.lasso, newx=test.mat, s=lambda.best) mean ((College.test[, "Apps"] - lasso.pred)^2)
The test MSE of 1028717.7713552 is lower than ridge regression and linear regression.
mod.lasso = glmnet ( model.matrix (Apps~., data=College), College[, "Apps"], alpha=1) predict (mod.lasso, s=lambda.best, type="coefficients")
The coefficients for several variables are pushed to zero.
(e). Fit a PCR model on the training set, with M chosen by crossvalidation. Report the test error obtained, along with the value of M selected by cross-validation.
library (pls)
_## Attaching package: ’pls’
pcr.fit = pcr (Apps~., data=College.train, scale=T, validation="CV") validationplot (pcr.fit, val.type="MSEP")
Apps
Here, 6 components seems best.
pls.pred = predict (pls.fit, College.test, ncomp=6) mean ((College.test[, "Apps"] - data.frame (pls.pred))^2)
or logical: returning NA
The test MSE for PLS is NA
(g). Comment on the results obtained. How accurately can we predict the number of college applications received? Is there much difference among the test errors resulting from these five approaches?
test.avg = mean (College.test[, "Apps"]) lm.test.r2=1- mean ((College.test[,"Apps"]-lm.pred)^2)/ mean ((College.test[,"Apps"]-test.avg)^2) ridge.test.r2=1- mean ((College.test[,"Apps"]-ridge.pred)^2)/ mean ((College.test[,"Apps"]-test.avg)^2)
lasso.test.r2=1- mean ((College.test[,"Apps"]-lasso.pred)^2)/ mean ((College.test[,"Apps"]-test.avg)^2) pcr.test.r2=1- mean ((College.test[,"Apps"]- data.frame (pcr.pred))^2)/ mean ((College.test[,"Apps"]-test.avg)^2)
or logical: returning NA
pls.test.r2=1- mean ((College.test[,"Apps"]- data.frame (pls.pred))^2)/ mean ((College.test[,"Apps"]-test.avg)^2)
or logical: returning NA
rbind ( c ("OLS", "Ridge", "Lasso", "PCR", "PLS"), c (lm.test.r2, ridge.test.r2, lasso.test.r2, pcr.test.r2, pls.test.r2))
Each methodology has a reasonably large R^2 and they are all fairly close together. There may not be much of a difference between them.