Machine learning with tidymodels :: Cheatsheet

Intro

tidymodels is a collection of R packages for modeling and machine learning that share a common design and grammar. Unlike most cheatsheets, which cover the functions of a single package, this cheatsheet maps the packages themselves, grouping each by where it fits in the machine learning workflow.

The machine learning workflow

The tidymodels machine learning workflow A linear pipeline flows across the top: Resampling, then Pre-processing, Modeling, Post-processing, and Measuring. Beneath it, an Orchestrating band spans Pre-processing through Post-processing and feeds into Tuning on the right. Orchestrating also flows down into Deploy. Resampling Pre-processing Modeling Post-processing Measuring Orchestrating Tuning Deploy

Resampling

Split and resample data for honest evaluation.

Pre-processing

Prepare data for modeling.

Modeling

Define and fit models through one consistent interface.

Classification & regression

Specialized problems

Post-processing

Adjust predictions.

Measuring

Measure model quality.

Orchestrating

Tie the pieces together.

Tuning

Optimize hyperparameters.

Deploy

Put models into production.

Prepare & serve

Run in a database

Data

Datasets used in documentation, tests, and teaching.

Deep learning

R packages that implement or wrap tabular deep-learning models.

Other

General

Development

The tidymodels package

Hex logo for tidymodels, a set of hexagons arranged like a honeycomb.

The tidymodels package installs and loads a set of packages that are considered important during day-to-day machine learning development.

It loads the following packages from tidymodels:

  • rsample
  • recipes
  • parsnip
  • yardstick
  • tailor
  • tune
  • dials
  • workflows
  • workflowsets
  • broom
  • infer
  • modeldata

It also loads the following packages from the tidyverse:

  • dplyr
  • ggplot2
  • purrr
  • tidyr

Example

A complete workflow: split, engineer features, tune, finalize, and deploy.

library(tidymodels)

# Split data and make CV folds
set.seed(857)

splits <- ames |>
  initial_split(prop = 0.8)
train <- training(splits)
folds <- train |>
  vfold_cv(v = 5)

# Feature engineering in a recipe
rec <- recipe(
  Sale_Price ~ Gr_Liv_Area + Year_Built + Bldg_Type,
  data = train
) |>
  step_log(Gr_Liv_Area, base = 10) |>
  step_dummy(all_nominal_predictors()) |>
  step_normalize(all_numeric_predictors())

# Model with parameters to tune
mod <- decision_tree(
  cost_complexity = tune(),
  tree_depth = tune()
) |>
  set_engine("rpart") |>
  set_mode("regression")

# Bundle into a workflow
wf <- workflow() |>
  add_recipe(rec) |>
  add_model(mod)

# Tune over the folds
res <- wf |>
  tune_grid(
    resamples = folds,
    grid = 10
  )

# Finalize best, refit, test once
best <- res |>
  select_best(metric = "rmse")
final <- wf |>
  finalize_workflow(best) |>
  last_fit(splits)

collect_metrics(final)
# A tibble: 2 × 4
  .metric .estimator .estimate .config        
  <chr>   <chr>          <dbl> <chr>          
1 rmse    standard   41683.    pre0_mod0_post0
2 rsq     standard       0.731 pre0_mod0_post0
# Deploy the fitted workflow
library(vetiver)
library(pins)

v <- final |>
  extract_workflow() |>
  vetiver_model("ames_tree")

board <- board_temp()
vetiver_pin_write(board, v)