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 withbake()
Prerequisites: you can run R and use the |> pipe, and you have met the train/test split and why leakage matters.
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.
Three things stop most models from using this as-is:
- 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.
- Text categories.
homeis the words own, rent, mortgage. Most algorithms only do arithmetic, so a category has to become numbers. - A missing value. Omar's
employedisNA. 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.