Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Linear Model Selection and Regularization This programming assignment will use the Tidy Models platform. It will take a look at regularization models and hyperparameter tuning.

Linear Model Selection and Regularization
This programming assignment will use the Tidy Models platform. It will take a look at regularization models and hyperparameter tuning. These models contain a regularization term. This assignment will use parsnip for model fitting and recipes and workflows to perform the transformations, and tune and dials to tune the hyperparameters of the model.
You will be using the Hitters data set from the ISLR package. You wish to predict the baseball players Salary based on several different characteristics which are included in the data set.
Since you wish to predict Salary, then you need to remove any missing data from that column. Otherwise, you won't be able to run the models.
Set output as Hitters
library(tidymodels)
library(ISLR2)
# Your code here
# Hitters <-
# your code here
# Hidden Tests
You will use the glmnet package to perform ridge regression. parsnip does not have a dedicated function to create a ridge regression model specification. You need to use linear_reg() and set mixture =0 to specify a ridge model. The mixture argument specifies the amount of different types of regularization, mixture =0 specifies only ridge regularization and mixture =1 specifies only lasso regularization.
Setting mixture to a value between 0 and 1 lets us use both. When using the glmnet engine you also need to set a penalty to be able to fit the model. You will set this value to 0 for now, it is not the best value, but you will look at how to select the best value in a little bit.
ridge_spec <- linear_reg(mixture =0, penalty =0)%>%
set_mode("regression")%>%
set_engine("glmnet")
Once the specification is created you can fit it to you data. You will use all the predictors. Use the fit function here.
ridge_fit <- fit(ridge_spec, Salary ~ ., data = Hitters)
The glmnet package will fit the model for all values of penalty at once, so you can now see see what the parameter estimate for the model is now that you have penalty =0. You can use the tidy function to accomplish this specific task.
tidy(ridge_fit)
Let us instead see what the estimates would be if the penalty was 11498. Store your output to tidy2. What do you notice?
# Your code here
# tidy2<-
# your code here
# Hidden Tests
Look below at the parameter estimates for penalty =705. Store your output to tidy3. Once again, use the tidy function to accomplish this task.
# Your code here
# tidy3<-
# your code here
# Hidden Tests
You can visualize how the magnitude of the coefficients are being regularized towards zero as the penalty goes up. Use the autoplot() function to accomplish this task. Output variable here is ridge_fit. Your image should look like this:
ridge_fit <-%>% autoplot()
Prediction is done like normal, if you use predict() by itself, then penalty =0 as you set in the model specification is used.
predict(ridge_fit, new_data = Hitters)
But you can also get predictions for other values of penalty by specifying it in predict(). Test with a value of 500. Store your output to predict_500
# Your code here
# predict_500<-
# your code here
# Hidden Tests
You saw how we can fit a ridge model and make predictions for different values of penalty. But it would be great if you could find the "best" value of the penalty. This is something you can use hyperparameter tuning for. Hyperparameter tuning is in its simplest form a way of fitting many models with different sets of hyperparameters trying to find one that performs "best".
The complexity in hyperparameter tuning can come from how you try different models. You will keep it simple for this lab and only look at grid search, only looking at evenly spaced parameter values. This is a fine enough approach if you have one or two tunable parameters but can become computationally infeasible.
See the chapter on iterative search from Tidy Modeling with R for more information.
You begin like normal by setting up a validation split (testing and training set). A K-fold cross-validation data set is created on the training data set with 10 folds.
Hitters_split <- initial_split(Hitters, strata = "Salary")
Hitters_train <- training(Hitters_split)
Hitters_test <- testing(Hitters_split)
Hitters_fold <- vfold_cv(Hitters_train, v =10)
You can use the tune_grid() function to perform hyperparameter tuning using a grid search. tune_grid() needs 3 different things;
a workflow object containing the model and preprocessor,
a rset object containing the resamples the workflow should be fitted within, and
a tibble containing the parameter values to be evaluated.
Optionally a metric set of performance metrics can be supplied for evaluation. If you don't set one then a default set of performance metrics is used.
You already have a resample object created in Hitters_fold. Now you should create the workflow specification next.
You just used the data set as is when you fit the model earlier. However, ridge regression is scale sensitive so you need to

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Microsoft SQL Server 2012 Programming

Authors: Paul Atkinson, Robert Vieira

1st Edition

1118102282, 9781118102282

More Books

Students also viewed these Databases questions

Question

Fixed dollar match: 75 cents per each $1 employee contribution.

Answered: 1 week ago