R: using ranger with caret, tuneGrid argument

ε祈祈猫儿з 提交于 2019-12-03 07:50:10

Here is the syntax for ranger in caret:

library(caret)

add . prior to tuning parameters:

tgrid <- expand.grid(
  .mtry = 2:4,
  .splitrule = "gini",
  .min.node.size = c(10, 20)
)

Only these three are supported by caret and not the number of trees. In train you can specify num.trees and importance:

model_caret <- train(Species  ~ ., data = iris,
                     method = "ranger",
                     trControl = trainControl(method="cv", number = 5, verboseIter = T, classProbs = T),
                     tuneGrid = tgrid,
                     num.trees = 100,
                     importance = "permutation")

to get variable importance:

varImp(model_caret)

#output
             Overall
Petal.Length 100.0000
Petal.Width   84.4298
Sepal.Length   0.9855
Sepal.Width    0.0000

To check if this works set number of trees to 1000+ - the fit will be much slower. After changing importance = "impurity":

#output:

             Overall
Petal.Length  100.00
Petal.Width    81.67
Sepal.Length   16.19
Sepal.Width     0.00

If it does not work I recommend installing latest ranger from CRAN and caret from git hub:

devtools::install_github('topepo/caret/pkg/caret')

To train the number of trees you can use lapply with fixed folds created by createMultiFolds or createFolds.

EDIT: while the above example works with caret package version 6.0-84, using the names of hyper parameters without dots works as well.

tgrid <- expand.grid(
  mtry = 2:4,
  splitrule = "gini",
  min.node.size = c(10, 20)
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!