Explore lasso regression penalty values
In the previous exercise, you completed all the code to scale the target and predictor variables. You will use the train
data and lasso_recipe
to build a workflow to train a lasso regression model and explore the effects of different penalty values. As you adjust the penalty and retrain the model, pay attention to the number of non-zero variables that are left in the model. You will be observing how lasso regression performs feature selection.
The tidyverse
and tidymodels
packages have been loaded for you.
This exercise is part of the course
Dimensionality Reduction in R
Exercise instructions
- Train a lasso regression workflow with a penalty of 0.001 and display the model coefficients that are greater than zero.
- Re-train a lasso regression workflow with a penalty of 0.01 and display the model coefficients that are greater than zero.
- Re-train a lasso regression workflow with a penalty of 0.1 and display the model coefficients that are greater than zero.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Train workflow model with penalty = 0.001 and view model variables
lasso_model <- linear_reg(___ = ___, mixture = 1, engine = "___")
lasso_workflow <- workflow(preprocessor = lasso_recipe, spec = ___)
tidy(lasso_workflow %>% fit(train)) %>% filter(___ > ___)
# Train the workflow model with penalty = 0.01 and view model variables
lasso_model <- ___(___ = ___, ___ = ___, ___ = "___")
lasso_workflow <- workflow(preprocessor = ___, spec = ___)
tidy(lasso_workflow %>% fit(train)) %>% filter(___ > ___)
# Train the workflow model with penalty = 0.1 and view model variables
lasso_model <- ___(___ = ___, ___ = ___, ___ = "___")
lasso_workflow <- ___ %>% ___(___)
tidy(___ %>% ___(___)) %>% ___(___ > ___)