Skip to content

Instantly share code, notes, and snippets.

@stephenturner
Created July 26, 2022 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenturner/3653d754edef90c9b8e81fbb9723017c to your computer and use it in GitHub Desktop.
Save stephenturner/3653d754edef90c9b8e81fbb9723017c to your computer and use it in GitHub Desktop.
Train and deploy a random forest with vetiver + plumber
# R session 1: model training and deployment ------------------------------
library(tidymodels)
library(vetiver)
library(plumber)
# Not the way you'd actually split data - for demo only
cars_train <- mtcars[1:24,]
cars_test <- mtcars[25:32,]
# Random forest using C++ implementation (ranger>>>RandomForest)
rf_spec <-
rand_forest(trees=1000) %>%
set_mode("regression") %>%
set_engine("ranger")
# Simple workflow
rf_wflow <-
workflow(mpg~., rf_spec)
# Fit the model
rf_fit <- fit(rf_wflow, cars_train)
# Create a vetiver model object
v <- vetiver_model(rf_fit, "mtcars_mpg")
# Create a plumber API
pr <- pr() %>% vetiver_api(v)
# Run the API server (pick a port number or let plumber pick one for you)
pr_run(pr, port=5678)
# R session 2: predict from the model endpoint ----------------------------
library(vetiver)
# Use the same IP/port from above
endpoint <- vetiver_endpoint("http://127.0.0.1:5678/predict")
# same split from above
cars_test <- mtcars[25:32,]
# Predict from the endpoint (returns a 1-col tibble)
predict(endpoint, cars_test)
# Stick this onto the original data, plot, etc.
cbind(cars_test, predict(endpoint, cars_test))
@turalsadigov
Copy link

turalsadigov commented Apr 18, 2023

Thank you, this was very helpful, particularly, the second piece, predicting from the endpoint. Best, Tural.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment