Lesson 1 of 7

Preprocess with recipes

You have framed a problem, fit a regression, and trained a classifier. Real models need one more thing first: clean, model-ready data. Picture a lender sizing up twelve loan applicants: incomes run into the tens of thousands, ages sit near forty, one person's employment history is missing, and home is the words own, rent, or mortgage. No model can use that table as-is. A recipe is the tidymodels way to package the fix into one reusable object that learns from your training data and applies itself, identically, to everything else.

By the end of this lesson you will be able to:

  • Say why raw columns (different scales, text categories, missing values) are not model-ready
  • Explain preprocessing leakage, the quiet mistake that makes your scores look better than they are
  • Build a recipe, learn its numbers from the training set with prep(), and apply them to new data with bake()

Prerequisites: you can run R and use the |> pipe, and you have met the train/test split and why leakage matters.

The problem

Raw data is not model-ready

Meet our running example: a lender deciding who is likely to default. Twelve applicants, each with an income, an age, how many months they have been employed, and whether they own, rent, or have a mortgage.

RInteractive R
loans <- data.frame( applicant = c("Maria","James","Priya","Ahmed","Lena","Tom", "Sara","Ravi","Nina","Omar","Eva","Liu"), income = c(52000, 38000, 64000, 71000, 45000, 29000, 88000, 41000, 60000, 33000, 95000, 47000), age = c(41, 26, 35, 52, 29, 23, 48, 31, 39, 27, 55, 33), employed = c(60, 18, 40, 120, 22, 8, 96, 30, 54, NA, 140, 44), home = factor(c("own","rent","mortgage","own","rent","rent", "mortgage","rent","mortgage","rent","own","mortgage")), defaulted = factor(c("no","yes","no","no","yes","yes", "no","no","no","yes","no","no")) ) loans #> applicant income age employed home defaulted #> 1 Maria 52000 41 60 own no #> 2 James 38000 26 18 rent yes #> 3 Priya 64000 35 40 mortgage no #> 4 Ahmed 71000 52 120 own no #> 5 Lena 45000 29 22 rent yes #> 6 Tom 29000 23 8 rent yes #> 7 Sara 88000 48 96 mortgage no #> 8 Ravi 41000 31 30 rent no #> 9 Nina 60000 39 54 mortgage no #> 10 Omar 33000 27 NA rent yes #> 11 Eva 95000 55 140 own no #> 12 Liu 47000 33 44 mortgage no

  

Three things stop most models from using this as-is:

  1. Different scales. Income runs into the tens of thousands; age sits around forty. A model that measures distance (kNN, SVM) or penalizes coefficients (lasso, ridge) will be dominated by income purely because its numbers are bigger.
  2. Text categories. home is the words own, rent, mortgage. Most algorithms only do arithmetic, so a category has to become numbers.
  3. A missing value. Omar's employed is NA. Many models refuse to run with a gap in the data.

A recipe is how we fix all three, in one place, the right way.