0Pricing
R Academy · Lesson

Workflows: Combining Recipes and Models

Bundle preprocessing and model into a single workflow object.

What Is a Workflow?

A workflow bundles a recipe and a model spec into a single object. This solves a critical problem: preprocessing and modelling steps must be treated as one unit during cross-validation and final fitting, otherwise parameters like normalization means can leak from test folds.

library(workflows)

# Start an empty workflow
wf <- workflow()
print(wf)

# Workflows have two slots: preprocessor and model
# Both must be filled before fitting

add_recipe() and add_model()

Use add_recipe(rec) to attach a recipe preprocessor and add_model(spec) to attach a parsnip model specification. The pipe operator makes this highly readable.

library(recipes)
library(parsnip)
library(workflows)

rec <- recipe(price ~ ., data = train) |>
  step_impute_median(all_numeric_predictors()) |>
  step_dummy(all_nominal_predictors()) |>
  step_normalize(all_numeric_predictors())

spec <- linear_reg() |> set_engine('lm')

wf <- workflow() |>
  add_recipe(rec) |>
  add_model(spec)

print(wf)

All lessons in this course

  1. Feature Engineering with recipes
  2. Model Specifications with parsnip
  3. Workflows: Combining Recipes and Models
  4. Resampling and Cross-Validation with rsample
← Back to R Academy