Applies one or more metrics to a data.frame containing columns with actual and predicted values as well as an optional column with case weights. The results are returned as a data.frame and can be used in a pipe.

performance(
  data,
  actual,
  predicted,
  w = NULL,
  metrics = rmse,
  key = "metric",
  value = "value",
  ...
)

Arguments

data

A data.frame with columns actual, predicted, and optionally w.

actual

The column name in data referring to actual values.

predicted

The column name in data referring to predicted values.

w

The optional column name in data referring to case weights.

metrics

Either a function or a named list of functions. Each function represents a metric and has four arguments:

  • observed,

  • predicted,

  • case weights, and

  • ....

If not a named list but a single function, the name of the function is guessed by deparse(substitute(...)), which would not provide the actual name of the function if called within lapply() etc. In such cases, you can pass a named list with one element, e.g., list(rmse = rmse).

key

Name of the resulting column containing the name of the metric. Defaults to "metric".

value

Name of the resulting column with the value of the metric. Defaults to "value".

...

Further arguments passed to the metric functions. E.g., if the metric is r_squared(), you could pass the relevant deviance function as additional argument (see examples).

Value

Data frame with one row per metric and two columns: key and value.

Examples

ir <- iris
fit_num <- lm(Sepal.Length ~ ., data = ir)
ir$fitted <- fit_num$fitted
performance(ir, "Sepal.Length", "fitted")
#>   metric    value
#> 1   rmse 0.300627
performance(ir, "Sepal.Length", "fitted", metrics = r_squared)
#>      metric     value
#> 1 r_squared 0.8673123
performance(
  ir,
  actual = "Sepal.Length",
  predicted = "fitted",
  metrics = c(`R-squared` = r_squared, rmse = rmse)
)
#>      metric     value
#> 1 R-squared 0.8673123
#> 2      rmse 0.3006270
performance(
  ir,
  actual = "Sepal.Length",
  predicted = "fitted",
  metrics = r_squared,
  deviance_function = deviance_gamma
)
#>      metric     value
#> 1 r_squared 0.8671253
performance(
  ir,
  actual = "Sepal.Length",
  predicted = "fitted",
  metrics = r_squared,
  deviance_function = deviance_tweedie,
  tweedie_p = 2
)
#>      metric     value
#> 1 r_squared 0.8671253