Reputation: 367
I have been testing random forests on one dataset. I would like to output the variable importance of all RFs in one data frame. Something like:
forests <- grep("rf", ls(), value=T)
importances <- do.call(cbind, lapply(forests, importance))
which throws an error:
Error in UseMethod("importance") : no applicable method for 'importance' applied to an object of class "character"
I tried converting forests
to a list, but that didn't help either.
Example:
rf10 <- randomForest(mpg ~., mtcars, ntree=10)
rf100 <- randomForest(mpg ~., mtcars, ntree=100)
cbind(importance(rf10), importance(rf100))
Upvotes: 0
Views: 266
Reputation: 1495
You should do instead
do.call(cbind, lapply(forests, function(x) importance(get(x))))
The return value from grep was a list of variable names, not the variables themselves. When you did importance(x)
, it was for example doing importance("rf10")
. You should be using the object as the parameter, and not the object's name. get(x)
returns the reference object for you.
Upvotes: 1