0
$\begingroup$

I have an RF model similar to the following.

The variation explained is about 50% and the variable importance is the following:

library("randomForest")
library("caret")
data("cars")
my_data <- cars[1:5]
my_rf <- randomForest( Price ~ ., data=my_data)
varImpPlot(my_rf)

What I want is to extract the percentage contribution of each of the variables to the model. How can I do this?

$\endgroup$
3
  • $\begingroup$ I don't think you can get the percentage contribution, what you can do is apply something like a shap value, for example cran.r-project.org/web/packages/iml/vignettes/intro.html $\endgroup$
    – StupidWolf
    Commented Nov 22, 2021 at 13:10
  • $\begingroup$ this will tell you for example, the relative change in for example mean squared error if the term is excluded $\endgroup$
    – StupidWolf
    Commented Nov 22, 2021 at 13:10
  • $\begingroup$ One simple approach would be to sum up the single variable importances and then divide every single var importance by that sum, which would give you exactly what you want. $\endgroup$
    – deschen
    Commented Nov 22, 2021 at 13:38

1 Answer 1

1
$\begingroup$

As suggested by @deschen, you could possibly just sum up the importances and divide, although don't think that's necessarily advisable.

imp <- unlist(varImp(my_rf))
scales::percent(imp / sum(imp))
#> Overall1 Overall2 Overall3 Overall4 
#>  "10.0%"  "62.4%"   "2.7%"  "24.9%"

Either way, use varImp() to retrieve numerical importance instead of just the plot. If you want to explore metrics for calculating importance, the ranger package allows you to define feature importance measures when you fit the model.

library(ranger)

my_rf2 <- ranger(Price ~ .,
                 data = my_data,
                 importance = "permutation")

importance(my_rf2)
#>     Mileage    Cylinder       Doors      Cruise 
#>  3589592.86 71388131.20    58988.84 21625831.07
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.