Skip to contents

This generic function returns the default metric of a model. The default metric is the metric that is used when evaluating a model with cv_performance(), performance() or evaluation_log() and no metric argument is specified.

The default metric of a model is usually rmse if the response is continuous and logLoss if it is binary. The only exception to this rule are models based on fm_xgb(), where the eval_metric used in xgb.train() is chosen as default.

If x is a multimodel or “cv” object with several models, then the first model's default metric is returned.

Usage

default_metric(x)

Arguments

x

A model, multimodel, fitted model or an object of class “cv”.

Value

A named list of length one containing the metric function, if available.

See also

Examples

n <- 100
d <- data.frame(x = rnorm(n), y = rnorm(n))
lm(y~x, d) |> default_metric()
#> Error in eval(call$data, environment): object 'd' not found
glm(I(y>0)~x, d, family = binomial) |> default_metric()
#> Error in eval(call$data, environment): object 'd' not found
fm_xgb(y~x, d, nrounds = 10) |> default_metric()
#> $rmse
#> function (actual, predicted, w = NULL, ...) 
#> {
#>     sqrt(mse(actual = actual, predicted = predicted, w = w, ...))
#> }
#> <bytecode: 0x561886cced50>
#> <environment: namespace:MetricsWeighted>
#> 
fm_xgb(y~x, d, nrounds = 10, objective = "reg:absoluteerror") |> default_metric()
#> $mae
#> function (actual, predicted, w = NULL, ...) 
#> {
#>     stopifnot(length(actual) == length(predicted))
#>     weighted_mean(abs(actual - predicted), w = w, ...)
#> }
#> <bytecode: 0x56188d403de8>
#> <environment: namespace:MetricsWeighted>
#> 
fm_xgb(y~x, d, nrounds = 10, objective = "reg:pseudohubererror") |> 
default_metric()  # -> there is no function with this name
#> $mphe
#> NULL
#>