Question: For each exercise, provide the R code, the R output and comment both the codes and the results. For this exercise, the only extra packages

For each exercise, provide the R code, the R output and comment both the codes

and the results.

For this exercise, the only extra packages allowed are ISLR, leaps and glmnet.

a) Write the R function that takes two input arguments: a vector y and a matrix X. These

contain the data used for a supervised learning exercise where we are interested in

predicting y given X. The function must implement the four algorithms: Best subset

selection, forward stepwise selection, backward stepwise selection and the lasso. The

function must print out on the screen (in the R console) the variable names (along with

their coefficients) of the best model selected by each one of those 4 procedures. The best

model should be selected by BIC for the best subset selection, forward stepwise selection

and backward stepwise selection algorithms and by 10-fold cross-validation for the lasso.

b) Use the function on the following three datasets available in the package ISLR: Hitters,

Credit and Smarket. The output variable in each of these datasets are Salary, Balance

and Volume respectively. The input variables are selected from all the remaining variables

in each of the dataset.

c) Comment whether the forward stepwise selection, backward stepwise selection and lasso

algorithms were able to find the best model possible for each of those three datasets.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Mathematics Questions!