AE 14: Version your housing model

R

Application exercise
R
Modified

October 21, 2025

Load the data

library(tidyverse)

housing <- read_csv(file = "data/tompkins-home-sales.csv")
glimpse(housing)

Build a model

  • Log transform the price variable
  • Split into training/test set
library(tidymodels)

set.seed(123)
housing_split <- housing |>
  mutate(price = log10(price)) |>
  initial_split(prop = 0.8)

housing_train <- training(housing_split)
housing_test <- testing(housing_split)

Train a linear regression model:

housing_fit <- workflow(
  price ~ beds + baths + area + year_built,
  linear_reg()
) |>
  fit(data = housing_train)

Create a deployable model object

library(vetiver)
v <- vetiver_model(
  model = ______,
  model_name = ______
)
v
# create a vetiver model with a custom description

Pin your model

library(pins)

board <- ______
board |> ______(v)
# retrieve your model metadata
board |> pin_meta(______)

Store a new version

Train your model with a new algorithm:

housing_fit <- workflow(
  price ~ beds + baths + area + year_built + town,
  linear_reg()
) |>
  fit(data = housing_train)

Store this new model as a new version of the same pin:

v <- vetiver_model(model = ______, model_name = ______, versioned = TRUE)
board |> ______(v)

What versions do you have?

board |> pin_versions(______)

Create a new {vetiver} model

Fit a random forest model

rf_rec <- recipe(
  price ~ beds + baths + area + year_built + town,
  data = housing_train
) |>
  step_impute_mean(all_numeric_predictors()) |>
  step_impute_mode(all_nominal_predictors())

housing_fit <- workflow() |>
  add_recipe(rf_rec) |>
  add_model(rand_forest(trees = 200, mode = "regression")) |>
  fit(data = housing_train)

Store your model:

v <- vetiver_model(model = ______, model_name = ______, versioned = TRUE)
board |> vetiver_pin_write(v)

Create a vetiver REST API

library(plumber)

pr() |>
  ______(v) |>
  pr_run()

Call your new API endpoints

Run your API in the background

We will write a standalone script to run the API in the background.

vetiver_write_plumber(
  board = board,
  name = "tompkins-housing",
  file = "plumber.R"
)

To run the R script, switch to the Terminal tab and run the Shell command:

Rscript ae-14-run-plumber.R

This executes the runner script which starts the API in the background. Note the URL and port printed in the terminal. You will need this to execute queries against the API.

Return predictions from your model API:

url <- ______
endpoint <- ______(url)
predict(endpoint, slice_sample(housing_test, n = 10))

Optional: try /metadata or /ping here:

library(httr2)

url <- ______

request(url) |>
  req_perform() |>
  resp_body_json()

Acknowledgments