Skip to contents

Models are reordered according to argument order. If order is shorter than the number of models, models specified in order will appear before those not mentioned and the other will be appended.

The methods for classes “cv” and “performance” can be sorted in order of model performance using the argument by.

Usage

sort_models(x, ...)

# S3 method for model
sort_models(x, ...)

# S3 method for multimodel
sort_models(x, order, ...)

# S3 method for cv
sort_models(x, order, by = NULL, decreasing = FALSE, ...)

# S3 method for performance
sort_models(x, order, by = NULL, decreasing = FALSE, ...)

# S3 method for evaluation_log
sort_models(x, order, ...)

Arguments

x

multimodel or cv

...

Currently not used.

order

A vector of model labels or integer indices. If not complete, the remaining elements will be appended. Example: If there are 4 models, and order=c(4, 2), output will have units in order c(4,2,1,3).

by

Sort by the value of a column of the performance table (only methods for classes “cv” and “performance”: Character string specifying a column in the table. Partial matching is accepted, such that "test" is sufficient to sort by test error for any metric.

decreasing

Logical: Sort in decreasing order of the by variable?

Value

sort_models() returns its input x with rearranged models.

See also

Examples

mm <- models(
  intercept = lm(Sepal.Length ~ 1 , iris),
  linear = lm(Sepal.Length ~ . , iris), 
  full = lm(Sepal.Length ~ .^2 , iris))
print(mm, what = "formula")
#> --- A “multimodel” object containing 3 models ---
#> 
#> ‘intercept’:
#>   formula:  Sepal.Length ~ 1
#> 
#> ‘linear’:
#>   formula:  Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width + 
#>                 Species
#> 
#> ‘full’:
#>   formula:  Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width + 
#>                 Species + Sepal.Width:Petal.Length + Sepal.Width:Petal.Width + 
#>                 Sepal.Width:Species + Petal.Length:Petal.Width + 
#>                 Petal.Length:Species + Petal.Width:Species

sort_models(mm, 3:1)
#> --- A “multimodel” object containing 3 models ---
#> 
#> ‘full’:
#>   model class:  lm
#>   formula:      Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width + 
#>                     Species + Sepal.Width:Petal.Length + Sepal.Width:Petal.Width + 
#>                     Sepal.Width:Species + Petal.Length:Petal.Width + 
#>                     Petal.Length:Species + Petal.Width:Species
#>   data:         data.frame [150 x 5], 
#>                 input as: ‘data = iris’
#>   call:         lm(formula = Sepal.Length ~ .^2, data = data)
#> 
#> ‘linear’:
#>   model class:  lm
#>   formula:      Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width + 
#>                     Species
#>   data:         data.frame [150 x 5], 
#>                 input as: ‘data = iris’
#>   call:         lm(formula = Sepal.Length ~ ., data = data)
#> 
#> ‘intercept’:
#>   model class:  lm
#>   formula:      Sepal.Length ~ 1
#>   data:         data.frame [150 x 5], 
#>                 input as: ‘data = iris’
#>   call:         lm(formula = Sepal.Length ~ 1, data = data)
sort_models(mm, c(3:2))
#> --- A “multimodel” object containing 3 models ---
#> 
#> ‘full’:
#>   model class:  lm
#>   formula:      Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width + 
#>                     Species + Sepal.Width:Petal.Length + Sepal.Width:Petal.Width + 
#>                     Sepal.Width:Species + Petal.Length:Petal.Width + 
#>                     Petal.Length:Species + Petal.Width:Species
#>   data:         data.frame [150 x 5], 
#>                 input as: ‘data = iris’
#>   call:         lm(formula = Sepal.Length ~ .^2, data = data)
#> 
#> ‘linear’:
#>   model class:  lm
#>   formula:      Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width + 
#>                     Species
#>   data:         data.frame [150 x 5], 
#>                 input as: ‘data = iris’
#>   call:         lm(formula = Sepal.Length ~ ., data = data)
#> 
#> ‘intercept’:
#>   model class:  lm
#>   formula:      Sepal.Length ~ 1
#>   data:         data.frame [150 x 5], 
#>                 input as: ‘data = iris’
#>   call:         lm(formula = Sepal.Length ~ 1, data = data)
sort_models(mm, c("full", "linear"))
#> --- A “multimodel” object containing 3 models ---
#> 
#> ‘full’:
#>   model class:  lm
#>   formula:      Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width + 
#>                     Species + Sepal.Width:Petal.Length + Sepal.Width:Petal.Width + 
#>                     Sepal.Width:Species + Petal.Length:Petal.Width + 
#>                     Petal.Length:Species + Petal.Width:Species
#>   data:         data.frame [150 x 5], 
#>                 input as: ‘data = iris’
#>   call:         lm(formula = Sepal.Length ~ .^2, data = data)
#> 
#> ‘linear’:
#>   model class:  lm
#>   formula:      Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width + 
#>                     Species
#>   data:         data.frame [150 x 5], 
#>                 input as: ‘data = iris’
#>   call:         lm(formula = Sepal.Length ~ ., data = data)
#> 
#> ‘intercept’:
#>   model class:  lm
#>   formula:      Sepal.Length ~ 1
#>   data:         data.frame [150 x 5], 
#>                 input as: ‘data = iris’
#>   call:         lm(formula = Sepal.Length ~ 1, data = data)

# Sort by test performance
cvperf <- cv_performance(mm)
sort_models(cvperf, by = "test")
#> --- Performance table ---
#> Metric: rmse
#>           train_rmse test_rmse time_cv
#> linear       0.29981   0.31153   0.017
#> full         0.28120   0.32495   0.017
#> intercept    0.82487   0.82513   0.007