Tidymodels and Vetiver

Agenda

  • Goal of Food inspections Model

  • Tidymodels

    • Assignment - Create a tidymodel
  • Vetiver

    • Pins and Plumber
    • Assignment - Deploy and monitor your tidymodel
  • Food inspection model (xgboost)

    • Assignment - Interacting with the model

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

    • Historical food inspection results

    • Business information


What do we want:

  • A MODEL that can predict if an establishment will fail an inspection

Where are we going

Review - Chicago Food Inspections Project

What is this project?

  • Over 15,000 food establishments across the City of Chicago
  • Only a handful of food inspectors currently employed

The Question

Can we help inspectors identify which establishments are at highest risk for failing an inspection?

The Answer

Use the historical (validated) inspections data to create a model that will predict fail likelihood!

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 file materials/activities/activity-05_TidymodelsVetiver.qmd

Complete Task 1 only!

Important

We will be using the diamonds dataset for this activity!

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?

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 file materials/activities/activity-05_TidymodelsVetiver.qmd

Complete Task 2 only!

🧰 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 file materials/activities/activity-05_TidymodelsVetiver.qmd

Complete Tasks 3 & 4 only!

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 file materials/activities/activity-05_TidymodelsVetiver.qmd

Complete Task 5

Food Inspections Model

Prior to this workshop:

  • An xgboost classification model was created using the inspections_validated data saved to our Postgres database

    • materials/project/03_model/03_model_creation.qmd

    • How to run background/workbench jobs for long running analyses!

      • materials/activities/activity-06_jobs.qmd
  • If you create a model, you should create a model card!


  • Model Goal: Return the likelihood that a business will fail an inspection

Food Inspections Model

Activity

πŸ‘‰ Try interacting with the Food Inspections Model API on Posit Connect

Example Prediction

Review