I have an RF model similar to the following.

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

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

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

  • $\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
    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
    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
    Nov 22, 2021 at 13:38

1 Answer 1


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.


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

#>     Mileage    Cylinder       Doors      Cruise 
#>  3589592.86 71388131.20    58988.84 21625831.07

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.