我使用 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.importanceNULL> fit$importanceNULL> fitRandom Forest 217380 samples 32 predictorsNo pre-processingResampling: 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.5352838RMSE was used to select the optimal model using the smallest value.The final value used for the model was mtry = 4.
有谁知道是否以及如何能获取这些信息?
谢谢。
回答:
varImp(fit)
可以帮你获取这些信息。
为了找到这个方法,我查看了 names(fit)
,这引导我查看了 names(fit$modelInfo)
– 然后你会看到 varImp
是其中一个选项。