Answered step by step
Verified Expert Solution
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
librarytidymodels
libraryISLR
# Your code here
# Hitters
Hitters ISLR::Hitters
Hitters Hitters dropnaSalary
# your code here
Attaching packages tidymodels
broom recipes
dials rsample
dplyr tibble
ggplot tidyr
infer tune
modeldata workflows
parsnip workflowsets
purrr yardstick
Conflicts tidymodelsconflicts
purrr::discard masks scales::discard
dplyr::filter masks stats::filter
dplyr::lag masks stats::lag
recipes::step masks stats::step
Use suppressPackageStartupMessages to eliminate package startup messages
# 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 linearreg and set mixture to specify a ridge model. The mixture argument specifies the amount of different types of regularization, mixture specifies only ridge regularization and mixture specifies only lasso regularization.
Setting mixture to a value between and 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 for now, it is not the best value, but you will look at how to select the best value in a little bit.
ridgespec linearregmixture penalty
setmoderegression
setengineglmnet
Once the specification is created you can fit it to you data. You will use all the predictors. Use the fit function here.
ridgefit fitridgespec, 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 You can use the tidy function to accomplish this specific task.
tidyridgefit
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started