1

I would like to extract variable importance for each resampling iteration with mlr3. So far, I have only found a "manual" way of doing it so I was wondering if there was a wrapper function or some other way to do it. Below is a toy example with random forest:

library(mlr3verse)
data("mtcars")
mtcars

task <- as_task_regr(mtcars, target = "mpg")

learner.rf <- lrn("regr.ranger", importance = "permutation", num.trees = 1000)

cv10 <- rsmp("cv", folds = 10)

resamp.rf <- resample(
  task = task, 
  learner = learner.rf, 
  resampling = cv10, 
  store_models = TRUE
)

Naively, I have tried the following and it does not work (that might no be surprising, I am still confused by the R6 objects):

resamp.rf$importance()

The following works but I have to do it for each resampling iteration:

resamp.rf$learners[[1]]$importance()

Or create my own extracting function:

aa <- resamp.rf$learners
dd  <- data.frame()
for (i in 1:length(aa)) {
  tmp <- data.frame(ITER = i, IMPORTANCE = aa[[i]]$importance())
  dd <- rbind(dd, tmp)
  rm(tmp)
}
user2165907
  • 1,401
  • 3
  • 13
  • 28
  • 1
    Not sure whether this justifies a method, you can just use `Map(function(x) x$importance(), resamp.rf$learners)`, which is what the method would return as well. – Sebastian May 12 '22 at 05:10

0 Answers0