许多森林的聚合 (RF) 变量重要性

Aggregate (RF) Variable Importance for Many Forests

我一直在一个数据集上测试随机森林。我想在一个数据框中输出所有 RF 的可变重要性。类似于:

forests <- grep("rf", ls(), value=T)
importances <- do.call(cbind, lapply(forests, importance))

这会引发错误:

Error in UseMethod("importance") : no applicable method for 'importance' applied to an object of class "character"

我尝试将 forests 转换为列表,但这也无济于事。

示例:

rf10 <- randomForest(mpg ~., mtcars, ntree=10)
rf100 <- randomForest(mpg ~., mtcars, ntree=100)
cbind(importance(rf10), importance(rf100))

你应该这样做

do.call(cbind, lapply(forests, function(x) importance(get(x))))

grep 的 return 值是变量名列表,而不是变量本身。当您执行 importance(x) 时,例如执行 importance("rf10")。您应该使用对象作为参数,而不是对象的名称。 get(x)return给你参考对象