Create a recipe-model workflow
The tidymodels
package can combine recipes and models into workflows. Workflows make it easy to create a pipeline of steps to prepare data and train models. Workflows can then be applied to new data easily, without having to redefine all the preprocessing and model building steps. Conveniently, workflows have a fit()
function that fit both the recipe and the model to the data.
In this exercise, you will practice creating a recipe and a model and adding them to a workflow, so they are ready to be fit to the data. The train
and test
sets of the employee healthcare attrition data are available for your use. The target variable is Attrition
.
The tidyverse
and tidymodels
packages have been loaded for you.
Cet exercice fait partie du cours
Dimensionality Reduction in R
Instructions
- Define a recipe using the
train
data with astep_filter_missing()
,step_scale()
, andstep_nzv()
to remove NAs, scale the numeric features, and remove low-variance features, respectively. Use a threshold of 0.5 forstep_filter_missing()
. - Define a logistic regression model using the "glm" engine.
- Add
feature_selection_recipe
andlr_model
to a workflow namedattrition_wflow
.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# Create recipe
feature_selection_recipe <-
___(___ ~ ., data = ___) %>%
___(___(), threshold = 0.5) %>%
___(___()) %>%
___(___()) %>%
prep()
# Create model
lr_model <- ___() %>%
___("___")
# Add recipe and model to a workflow
attrition_wflow <- ___() %>%
___(___) %>%
___(___)