我使用caret
+ ranger
训练了一个随机森林。
fit <- train(
y ~ x1 + x2
,data = total_set
,method = "ranger"
,trControl = trainControl(method="cv", number = 5, allowParallel = TRUE, verbose = TRUE)
,tuneGrid = expand.grid(mtry = c(4,5,6))
,importance = 'impurity'
)
现在我想要讲述变量的重要性。然而,这些都无法正常工作:
> importance(fit)
Error in UseMethod("importance") : no applicable method for 'importance' applied to an object of class "c('train', 'train.formula')"
> fit$variable.importance
NULL
> fit$importance
NULL
> fit
Random Forest
217380 samples
32 predictors
No pre-processing
Resampling: Cross-Validated (5 fold)
Summary of sample sizes: 173904, 173904, 173904, 173904, 173904
Resampling results across tuning parameters:
mtry RMSE Rsquared
4 0.03640464 0.5378731
5 0.03645528 0.5366478
6 0.03651451 0.5352838
RMSE was used to select the optimal model using the smallest value.
The final value used for the model was mtry = 4.
有什么办法可以获取它吗?
谢谢。
caret
的文档找到了它。感谢你提供的这种寻找信息的有用方法!然而,对于大多数使用caret
的train()
训练出来的模型,似乎varImp()
是获取变量重要性的方法。未来的用户需注意:我并不确定,并且没有时间去验证,但据我所知,在train()
中,需要将参数importance = 'impurity'
(我猜也可以传入importance = 'permutation'
)以便使用varImp()
。 - François M.ranger
但没有用caret
训练模型,那么importance(fit)
将是获取变量重要性的正确方式。同上,我认为参数importance ='impurity'
(或'permutation')需要在train()
中。 - François M.ranger
的默认设置是不计算importance
的。您必须显式指定importance = 'impurity'
或importance = 'permutation'
以使任何这些方法工作,即使您正在使用train
。 - John MvarImp
也适用于使用caret
训练的Rborist
模型。 - cuttlefish