【发布时间】:2021-01-13 13:37:27
【问题描述】:
我有几个由函数传递的模型,模型的数量是可变的。通过 (...) 我可以通过如下所有模型:
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 *
但是当模型保存在列表中时,它们不会保留名称,有没有办法让以下输出使用 (...) 存储名称?
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 *
由 reprex 包 (v0.3.0) 于 2021-01-13 创建
【问题讨论】:
标签: r tidyverse tidymodels