Learn R Programming

yardstick (version 1.1.0)

metric_vec_template: Developer function for calling new metrics

Description

metric_vec_template() is useful alongside metric_summarizer() for implementing new custom metrics. metric_summarizer() calls the metric function inside dplyr::summarise(). metric_vec_template() is a generalized function that calls the core implementation of a metric function, and includes a number of checks on the types, lengths, and argument inputs.

Usage

metric_vec_template(
  metric_impl,
  truth,
  estimate,
  na_rm = TRUE,
  cls = "numeric",
  estimator = NULL,
  case_weights = NULL,
  ...
)

Arguments

metric_impl

The core implementation function of your custom metric. This core implementation function is generally defined inside the vector method of your metric function.

truth

The realized vector of truth. This is either a factor or a numeric.

estimate

The realized estimate result. This is either a numeric vector, a factor vector, or a numeric matrix (in the case of multiple class probability columns) depending on your metric function.

na_rm

A logical value indicating whether NA values should be stripped before the computation proceeds. NA values are removed before getting to your core implementation function so you do not have to worry about handling them yourself. If na_rm=FALSE and any NA values exist, then NA is automatically returned.

cls

A character vector of length 1 or 2 corresponding to the class that truth and estimate should be, respectively. If truth and estimate are of the same class, just supply a vector of length 1. If they are different, supply a vector of length 2. For matrices, it is best to supply "numeric" as the class to check here.

estimator

The type of averaging to use. By this point, the averaging type should be finalized, so this should be a character vector of length 1\. By default, this character value is required to be one of: "binary", "macro", "micro", or "macro_weighted". If your metric allows more or less averaging methods, override this with averaging_override.

case_weights

Optionally, the realized case weights, as a numeric vector. This must be the same length as truth, and will be considered in the na_rm checks. If supplied, this will be passed on to metric_impl as the named argument case_weights.

...

Extra arguments to your core metric function, metric_impl, can technically be passed here, but generally the extra args are added through R's scoping rules because the core metric function is created on the fly when the vector method is called.

Details

metric_vec_template() is called from the vector implementation of your metric. Also defined inside your vector implementation is a separate function performing the core implementation of the metric function. This core function is passed along to metric_vec_template() as metric_impl.

See Also

metric_summarizer() finalize_estimator() dots_to_estimate()