2. For parts (a) through (c), indicate which of i. through iv. is correct. Justify your answer.

i. More flexible and hence will give improved prediction accuracy when its increase in bias is less than its decrease in variance.

ii. More flexible and hence will give improved prediction accuracy when its increase in variance is less than its decrease in bias.

iii. Less flexible and hence will give improved prediction accuracy when its increase in bias is less than its decrease in variance.

iv. Less flexible and hence will give improved prediction accuracy when its increase in variance is less than its decrease in bias.

(a) The lasso, relative to least squares, is:

  1. Less flexible and hence will give improved prediction accuracy when its increase in bias is less than its decrease in variance.

(b) Repeat (a) for ridge regression relative to least squares.

  1. Less flexible and hence will give improved prediction accuracy when its increase in bias is less than its decrease in variance.

(c) Repeat (a) for non-linear methods relative to least squares.

  1. More flexible and hence will give improved prediction accuracy when its increase in variance is less than its decrease in bias.

9. In this exercise, we will predict the number of applications received using the other variables in the College data set.

(a) Split the data set into a training set and a test set.

train = sample(1:dim(College)[1], dim(College)[1]/2)

College.train = College[train,]
College.test = College[-train,]

(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((lm.pred - College.test[,"Apps"]) ^2)
## [1] 1511450

test error = 1511450

(c) Fit a ridge regression model on the training set, with λ chosen by cross-validation. Report the test error obtained.

train.mat = model.matrix(Apps~., data=College.train)
test.mat = model.matrix(Apps~., data=College.test)
grid = 10 ^ seq(10, -2, length=100)
ridge.mod = cv.glmnet(train.mat, College.train[, "Apps"], alpha=0, lambda=grid)
lambda.best = ridge.mod$lambda.min

ridge.pred = predict(ridge.mod, newx=test.mat, s=lambda.best)
mean((College.test[, "Apps"] - ridge.pred) ^2)
## [1] 1589018

test error = 1589018

(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)
lambda.best = mod.lasso$lambda.min
lasso.pred = predict(mod.lasso, newx=test.mat, s=lambda.best)
mean((College.test[, "Apps"] - lasso.pred)^2)
## [1] 1535419

test error = 1535419

(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.

pcr.fit = pcr(Apps~., data=College.train, scale=T, validation="CV")
pcr.pred = predict(pcr.fit, College.test, ncomp=10)
mean((College.test[, "Apps"] - pcr.pred)^2)
## [1] 3093142

test error = 3093142

(f) Fit a PLS 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.

pls.fit = plsr(Apps~., data=College.train, scale=T, validation="CV")
pls.pred = predict(pls.fit, College.test, ncomp=10)
mean((College.test[, "Apps"] - pls.pred)^2)
## [1] 1554293

test error = 1554293

(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?

PCR model under performed compared to the others.

11. We will now try to predict per capita crime rate in the Boston data set.

(a) Try out some of the regression methods explored in this chapter, such as best subset selection, the lasso, ridge regression, and PCR. Present and discuss results for the approaches that you consider.

train = sample(1:dim(Boston)[1], dim(Boston)[1]/2)

Boston.train = Boston[train,]
Boston.test = Boston[-train,]
train.mat = model.matrix(crim~., data=Boston.train)
test.mat = model.matrix(crim~., data=Boston.train)
grid = 10 ^ seq(10, -2, length=100)
ridge.mod = cv.glmnet(train.mat, Boston.train[, "crim"], alpha=0, lambda=grid)
lambda.best = ridge.mod$lambda.min

ridge.pred = predict(ridge.mod, newx=test.mat, s=lambda.best)
mean((Boston.test[, "crim"]- ridge.pred) ^2)
## [1] 130.8663
mod.lasso = cv.glmnet(train.mat, Boston.train[, "crim"], alpha=1, lambda=grid)
lambda.best = mod.lasso$lambda.min
lasso.pred = predict(mod.lasso, newx=test.mat, s=lambda.best)
mean((Boston.test[, "crim"] - lasso.pred)^2)
## [1] 129.3982
pcr.fit = pcr(crim~., data=Boston.train, scale=T, validation="CV")
pcr.pred = predict(pcr.fit, Boston.test, ncomp=10)
mean((Boston.test[, "crim"] - pcr.pred)^2)
## [1] 71.10062

(b) Propose a model (or set of models) that seem to perform well on this data set, and justify your answer. Make sure that you are evaluating model performance using validation set error, crossvalidation, or some other reasonable alternative, as opposed to using training error.

PCR has lowest test error so id choose that one.

(c) Does your chosen model involve all of the features in the data set? Why or why not?

???