Local Variable Importance - Random Forest

Hi All,

I wanted to generate a labelled horizontal barplot using within-class local variable importance. I wondered if it would be possible for someone to demonstrate this using the following code below?

This code was supplied by the author of the 'ranger' package on the following link: variable importance by class · Issue #552 · imbs-hl/ranger · GitHub.

With this in mind, I wondered if it was possible for someone to demonstrate local variable variable importance for one of the classes below.

All help would be appreciated, thanks.

library(ranger)
rf <- ranger(Species ~ ., iris, importance = "permutation", 
             local.importance = TRUE)
rf$variable.importance.local

colMeans(rf$variable.importance.local[iris$Species == "setosa", ])
colMeans(rf$variable.importance.local[iris$Species == "versicolor", ])
colMeans(rf$variable.importance.local[iris$Species == "virginica", ]

library(ranger)
library(tidyverse)

rf <- ranger(Species ~ ., iris,
  importance = "permutation",
  local.importance = TRUE
)
rf$variable.importance.local

(setosa_imp_df <- enframe(colMeans(
  rf$variable.importance.local[iris$Species == "setosa", ]
)))

ggplot(data=setosa_imp_df) + 
  aes(x=name,
      y=value) +
  geom_col() + 
ggtitle("setosa local importance of rf")

Thank you @nirgrahamuk . The 'enframe' function worked perfectly. Thanks for your help

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.