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)
}