0

I'm using mlr3 for a regression learning. While using randomForest for regression, the model runs fine but when I access 'importance()' I get the following error:

    Error in switch(pars[["importance"]], mse = imp[["%IncMSE"]], nodepurity = imp[["IncNodePurity"]],  : 
      EXPR must be a length 1 vector

When I stop the code in browser mode, I do see that 'imp' has all the feature importance as the below screenshot shows:

enter image description here

Upon seeing the help page I also noticed that I need to set parameter 'importance' to either "mse" or "nodepurity" but it throws an error: unused argument (importance = "mse")

Can someone please help me?

  • 1
    Please try to avoid posting screenshots and add a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), then we can help you. – pat-s Apr 29 '21 at 13:02
  • Hi pat-s, thank you. The data is non-public and I'm not sure if I can upload it somewhere for you to reproduce the exact state of the system (easier to share the code). I was hoping you'd be able to comment on the fact that pars[["importance"]] is being accessed while par is clearly empty. Since I'm not on my own controlling these variables there must be a system-specific reason why it is so. Also could you please comment on the need to pass on a parameter named 'importance' to the method by the same name? – intelinsight Apr 29 '21 at 13:18
  • Any hint or things I can try would be helpful @pat-s. Please let me know if there is a way for me to share more info that will help you help me. I can certainly share the snippet of the code. – intelinsight Apr 29 '21 at 13:22
  • 2
    A reproducible example does not need your data - try to replicate the issue with one of the example tasks (and then edit the question). The error you are seeing does not seem to relate to your data. Also you might find the `reprex` package helpful. – pat-s Apr 29 '21 at 13:24
  • If the package choice is not mandatory you could try to switch to `caret` which is really well documented and provide all the important function to easily handle with data splitting, preprocessing, cross-validation via resampling for parameters tuning and model training. See [here](https://topepo.github.io/caret/) – Elia Apr 29 '21 at 14:20
  • @Elia do you mean tidymodels? – intelinsight Apr 29 '21 at 16:00
  • No I mean the caret package – Elia Apr 29 '21 at 16:17
  • 1
    If something it not documented (well) in mlr3, we are happy to take suggestions to our 270+ pages manual ;) Not sure if switching the framework is a helpful suggestion to the problem if no reprex was given yet proving an actual issue. – pat-s Apr 30 '21 at 05:31

1 Answers1

0

As pat-s said, we cannot point out the error in your code without a reprex. However, this works and might help you.

library(mlr3)
library(mlr3extralearners)

task = tsk("boston_housing")
task$select(c("age", "b", "cmedv")) 

learner = lrn("regr.randomForest", importance = "mse")
learner$train(task)
learner$importance()

#>     cmedv         b       age 
#> 123.64922   8.72982   6.64579 

See section 3.5.3 'Variable Importance Filters' in https://mlr3book.mlr-org.com/fs.html.

be-marc
  • 1,276
  • 5
  • 5