能否获得 R 的 TidyModel 框架中随机森林模型的袋外误差?

Can the out of bag error for a random forests model in R's TidyModel's framework be obtained?

如果直接使用ranger函数,可以从生成的ranger class对象中获取out-of-bag错误。

如果相反,通过设置配方、模型 specification/engine、调整参数等方式进行,我们如何提取相同的错误? Tidymodels 方法似乎无法保留该数据。

如果你想访问 parsnip 对象内部的 ranger 对象,它就在那里 $fit:

library(tidymodels)

data("ad_data", package = "modeldata")

rf_spec <- 
  rand_forest() %>% 
  set_engine("ranger", oob.error = TRUE) %>% 
  set_mode("classification")

rf_fit <- rf_spec %>%
  fit(Class ~ ., data = ad_data)

rf_fit
#> parsnip model object
#> 
#> Fit time:  158ms 
#> Ranger result
#> 
#> Call:
#>  ranger::ranger(x = maybe_data_frame(x), y = y, oob.error = ~TRUE,      num.threads = 1, verbose = FALSE, seed = sample.int(10^5,          1), probability = TRUE) 
#> 
#> Type:                             Probability estimation 
#> Number of trees:                  500 
#> Sample size:                      333 
#> Number of independent variables:  130 
#> Mtry:                             11 
#> Target node size:                 10 
#> Variable importance mode:         none 
#> Splitrule:                        gini 
#> OOB prediction error (Brier s.):  0.1340793

class(rf_fit)
#> [1] "_ranger"   "model_fit"
class(rf_fit$fit)
#> [1] "ranger"

rf_fit$fit$prediction.error
#> [1] 0.1340793

reprex package (v1.0.0)

于 2021 年 3 月 11 日创建