Tidymodels and Vetiver


Section Agenda

  • Goal of Ferry Delay Prediction Model

  • Tidymodels

    • Assignment - Create a tidymodel
  • Vetiver

    • Pins and Plumber
    • Assignment - Deploy and monitor your tidymodel

Disclaimer


THIS IS NOT A MODELING WORKSHOP!


… but modeling is still super cool 😎 !

Review

What’s the Goal?

What we have:

  • A beautiful - tidy - validated dataset

    • Ferry information and history

    • Weather data


What do we want:

  • A MODEL that can predict if a ferry will have a delayed departure

Where are we going

Review - Washington State Ferry


⛴️ WSF is the largest operating public ferry system in the US! 🤯

🐋 21 ferries across Puget Sound and the Salish Sea

Question and Answer

The Question

Can we help inform travelers about possible ferry delays?

The Answer

Use the ferry-weather (validated) data to create a model that will predict delay status!

Introduction to Tidymodels


✋ If you have used tidymodels before?

Introduction to Tidymodels


Steps to create (most) models:

  1. Data - preprocessing?
  2. Create a formula - what does the model do?
  3. Define your model type - Linear regression, decision tree, xgboost, etc?
  4. Train
  5. Test
  6. Predict

Introduction to Tidymodels

library(tidymodels)

# Create Recipe
model_recipe <- recipes::recipe(what-to-predict ~ data-features, 
                                data = your-data)

# Define model using parsnip (e.x. linear regression)
model_type <- parsnip::linear_reg()

# Combine the two to create a workflow
model_wflow <- workflows::workflow(model_recipe, model_type)

# Fit workflow to data using parsnip
model_fit <- parsnip::fit(model_wflow, your-data)

Introduction to Tidymodels


Activity Time!

Activity

  • Open the project materials/04-tidymodels-vetiver/04-tidymodels-vetiver.Rproj
  • Open the file 04-tidymodels-vetiver.qmd

Complete Tasks 0 & 1 only!

Goal

Build a model using tidymodels.

We have a model…now what 🤷 ?


We have two questions:

  1. How can we save our model so others can use it?
  2. How can we serve our model so others can interact with it?

How do you save/share models?

Vetiver












  • Open source R (and Python) package.
  • Deploy and maintain machine learning models –> MLOps
  • Provides tools to version, deploy, and monitor a trained model.

Introduction to Pins & Plumber

🧰 Pins

  • Open source R (and Python) package.
  • Publishes data, models, and other R objects, making it easy to share them across projects and with your colleagues.
    • Interoperability –> You can share an object built in R with Python users (and vice versa)!
  • For our workflow:
    • How can we save our model so that others (or other content) can use it?

Activity

Activity

  • Open the project materials/04-tidymodels-vetiver/04-tidymodels-vetiver.Rproj
  • Open the file 04-tidymodels-vetiver.qmd

Complete Task 2 only!

Goal

Use vetiver to pin model to Posit Connect.

🧰 Plumber

  • Open source R package.
  • Create a web Application Programming Interface (API) using only R code!
    • Interoperability –> You can interact with a Plumber API using Python (or any other language/HTTP client).
  • For our workflow:
    • How can we serve our model as an API?

Activity

Activity

  • Open the project materials/04-tidymodels-vetiver/04-tidymodels-vetiver.Rproj
  • Open the file 04-tidymodels-vetiver.qmd

Complete Task 3 only!

Goal

Use vetiver to serve the pinned model as an API to Posit Connect.

Version and Monitor your models


  • Data changes over time
  • Models change over time
  • How can I ensure I’m using the best model for the job?

Activity

  • Open the project materials/04-tidymodels-vetiver/04-tidymodels-vetiver.Rproj
  • Open the file 04-tidymodels-vetiver.qmd

Complete Task 4

Goal

Use vetiver to version and monitor your ferry predict model.

Summary - Pins/Plumber

Ferry Delay Prediction Model

Training this model is relatively fast

  • What if the model training is slow or the data is REALLY big?

    • Workbench Jobs allows you to run R/Python scripts in new sessions independent of your current RStudio Pro/VSCode session

  • If you create a model, you should create a model card!

    • Check out a basic model card here!

Review