Learn R Programming

tidyposterior (version 1.1.0)

initialize_keys: Create a Template of Model Labels

Description

compare_to_leader() can relabel models for plotting through its key argument. initialize_keys() builds a template for that argument with one row per model, ready to have its label column edited.

Usage

initialize_keys(x)

Value

A tibble with one row per model and two columns: model, holding the model names recorded in x, and label, initialized to the same values.

Arguments

x

An object produced by perf_mod().

Details

Leave the model column alone. Those names are what compare_to_leader() joins on, and every model in x has to be represented, so editing or dropping them will produce an error. Edit the label column to whatever should appear on the plot.

Rows may be reordered and extra columns may be added; both are ignored. Labels do not have to be unique.

See Also

compare_to_leader()

Examples

Run this code
library(parsnip)
library(rsample)
library(workflowsets)

set.seed(1)
folds <- vfold_cv(mtcars, v = 5)

# \donttest{
mpg_models <-
  workflow_set(
    preproc = list(
      small = mpg ~ wt,
      medium = mpg ~ wt + hp,
      large = mpg ~ .
    ),
    models = list(lm = linear_reg())
  ) |>
  workflow_map("fit_resamples", resamples = folds, seed = 2)

set.seed(4321)
mpg_post <- perf_mod(mpg_models, metric = "rmse", refresh = 0, chains = 2)

mpg_keys <- initialize_keys(mpg_post)
mpg_keys

# Edit the labels, then pass the result along:
mpg_keys$label <- c("1 predictor", "2 predictors", "all predictors")

mpg_post |>
  compare_to_leader(size = 0.5, key = mpg_keys, seed = 2) |>
  autoplot()
# }

Run the code above in your browser using DataLab