Skip to contents

Tailors compose iterative adjustments to model predictions. After initializing a tailor with this function, add adjustment specifications with adjust_*() functions:

For ad-hoc adjustments, see adjust_predictions_custom().

Tailors must be trained with fit() before being applied to new data with predict(). Tailors are tightly integrated with the tidymodels framework; for greatest ease of use, situate tailors in model workflows with ?workflows::add_tailor().

Usage

tailor()

Ordering of adjustments

When composing multiple adjustments in a tailor object, the order matters and must follow specific rules depending on the type of predictions being adjusted (classification or regression).

For classification problems (binary and multiclass), adjustments that modify probability estimates (e.g., adjust_probability_calibration()) must be applied before adjustments that change hard class predictions (including adjust_equivocal_zone()). This ensures that class predictions are based on the final calibrated probabilities. For regression problems, adjust_numeric_calibration() must be applied before other numeric adjustments. This ensures that subsequent adjustments work with calibrated predictions.

Generally, adjustments cannot be duplicated (i.e. the same adjustment type cannot be used multiple times in a tailor object), though adjust_predictions_custom() can be used multiple times. Adjustments for different prediction types cannot be mixed—numeric adjustments (for regression) and probability adjustments (for classification) cannot be used in the same tailor object.

If these ordering rules are violated, tailor() will raise an error describing the issue.

Examples

library(dplyr)
library(modeldata)

# `predicted` gives hard class predictions based on probabilities
two_class_example %>% count(predicted)
#>   predicted   n
#> 1    Class1 277
#> 2    Class2 223

# change the probability threshold to allot one class vs the other
tlr <-
  tailor() %>%
  adjust_probability_threshold(threshold = .1)

tlr
#> 
#> ── tailor ──────────────────────────────────────────────────────────────────────
#> A binary postprocessor with 1 adjustment:
#> 
#>  Adjust probability threshold to 0.1.

# fit by supplying column names. situate in a modeling workflow
# with `workflows::add_tailor()` to avoid having to do so manually
tlr_fit <- fit(
  tlr,
  two_class_example,
  outcome = c(truth),
  estimate = c(predicted),
  probabilities = c(Class1, Class2)
)

tlr_fit
#> 
#> ── tailor ──────────────────────────────────────────────────────────────────────
#> A binary postprocessor with 1 adjustment:
#> 
#>  Adjust probability threshold to 0.1. [trained]

# adjust hard class predictions
predict(tlr_fit, two_class_example) %>% count(predicted)
#> # A tibble: 2 × 2
#>   predicted     n
#>   <fct>     <int>
#> 1 Class1      362
#> 2 Class2      138