Get startedGet started for free

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.

This exercise is part of the course

Dimensionality Reduction in R

View Course

Exercise instructions

  • Define a recipe using the train data with a step_filter_missing(), step_scale(), and step_nzv() to remove NAs, scale the numeric features, and remove low-variance features, respectively. Use a threshold of 0.5 for step_filter_missing().
  • Define a logistic regression model using the "glm" engine.
  • Add feature_selection_recipe and lr_model to a workflow named attrition_wflow.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create recipe
feature_selection_recipe <- 
  ___(___ ~ ., data = ___) %>% 
  ___(___(), threshold = 0.5) %>% 
  ___(___()) %>% 
  ___(___()) %>% 
  prep()
  
# Create model
lr_model <- ___() %>% 
  ___("___")

# Add recipe and model to a workflow
attrition_wflow <- ___() %>% 
  ___(___) %>% 
  ___(___)
Edit and Run Code