I have a couple of models that are passed by a function, the number of models is variable. Through (...) I can pass all the models as follows:
library(tidymodels)
library(dplyr)
model_1 = decision_tree(mode = "regression") %>% set_engine("rpart") %>% fit(Sepal.Length ~ Petal.Length, data = iris)
model_2 = decision_tree(mode = "regression") %>% set_engine("rpart") %>% fit(Sepal.Length ~ Petal.Length + Petal.Width, data = iris)
F.function = function(...){
a = list(...)
a
}
F.function(model_1,model_2)
#> [[1]]
#> parsnip model object
#>
#> Fit time: 15ms
#> n= 150
#>
#> node), split, n, deviance, yval
#> * denotes terminal node
#>
#> 1) root 150 102.1683000 5.843333
#> 2) Petal.Length< 4.25 73 13.1391800 5.179452
#> 4) Petal.Length< 3.4 53 6.1083020 5.005660 *
#> 5) Petal.Length>=3.4 20 1.1880000 5.640000 *
#> 3) Petal.Length>=4.25 77 26.3527300 6.472727
#> 6) Petal.Length< 6.05 68 13.4923500 6.326471
#> 12) Petal.Length< 5.15 43 8.2576740 6.165116 *
#> 13) Petal.Length>=5.15 25 2.1896000 6.604000 *
#> 7) Petal.Length>=6.05 9 0.4155556 7.577778 *
#>
#> [[2]]
#> parsnip model object
#>
#> Fit time: 5ms
#> n= 150
#>
#> node), split, n, deviance, yval
#> * denotes terminal node
#>
#> 1) root 150 102.1683000 5.843333
#> 2) Petal.Length< 4.25 73 13.1391800 5.179452
#> 4) Petal.Length< 3.4 53 6.1083020 5.005660 *
#> 5) Petal.Length>=3.4 20 1.1880000 5.640000 *
#> 3) Petal.Length>=4.25 77 26.3527300 6.472727
#> 6) Petal.Length< 6.05 68 13.4923500 6.326471
#> 12) Petal.Length< 5.15 43 8.2576740 6.165116 *
#> 13) Petal.Length>=5.15 25 2.1896000 6.604000 *
#> 7) Petal.Length>=6.05 9 0.4155556 7.577778 *
But when the models are saved in the list they do not keep the name, is there any way to have the following output storing the names using (...)?
list(model_1 = model_1,
model_2 = model_2)
#> $model_1
#> parsnip model object
#>
#> Fit time: 15ms
#> n= 150
#>
#> node), split, n, deviance, yval
#> * denotes terminal node
#>
#> 1) root 150 102.1683000 5.843333
#> 2) Petal.Length< 4.25 73 13.1391800 5.179452
#> 4) Petal.Length< 3.4 53 6.1083020 5.005660 *
#> 5) Petal.Length>=3.4 20 1.1880000 5.640000 *
#> 3) Petal.Length>=4.25 77 26.3527300 6.472727
#> 6) Petal.Length< 6.05 68 13.4923500 6.326471
#> 12) Petal.Length< 5.15 43 8.2576740 6.165116 *
#> 13) Petal.Length>=5.15 25 2.1896000 6.604000 *
#> 7) Petal.Length>=6.05 9 0.4155556 7.577778 *
#>
#> $model_2
#> parsnip model object
#>
#> Fit time: 5ms
#> n= 150
#>
#> node), split, n, deviance, yval
#> * denotes terminal node
#>
#> 1) root 150 102.1683000 5.843333
#> 2) Petal.Length< 4.25 73 13.1391800 5.179452
#> 4) Petal.Length< 3.4 53 6.1083020 5.005660 *
#> 5) Petal.Length>=3.4 20 1.1880000 5.640000 *
#> 3) Petal.Length>=4.25 77 26.3527300 6.472727
#> 6) Petal.Length< 6.05 68 13.4923500 6.326471
#> 12) Petal.Length< 5.15 43 8.2576740 6.165116 *
#> 13) Petal.Length>=5.15 25 2.1896000 6.604000 *
#> 7) Petal.Length>=6.05 9 0.4155556 7.577778 *
Created on 2021-01-13 by the reprex package (v0.3.0)