Search code examples
rplotmachine-learningrandom-forestcategorical-data

How can I create a Partial Dependence plot for a categorical variable in R?


I am working with the r-package randomForest and have successfully made a random forest model and an importance plot. I am working with a dichotomous response and several categorical predictors.

However, I can't figure out how to make partial dependence plots for my categorical variables. I have tried using the randomForest command partialPLot. But I get the following error:

> partialPlot(rf.5, rf.train.1, religion)
Error in is.finite(x) : default method not implemented for type 'list'

.

So my question is: Can anyone explain in a simple way how you would make a random forest partial dependence plot for a categorical variable?

This is the kind of plot I want to make: https://stats.stackexchange.com/questions/235667/partial-dependence-plot-interpretation-for-categorical-variables

Would really appreciate some help on this. Thanks!


Solution

  • Here is a simple example of how to use partialPlot for a categorical explanatory variable. Check if the classes of the inputs of your partialPlot are the same of this example.
    I hope this can help you.
    The dataset df has a binary independent variable x4 and a binary response variable y:

    df <- data.frame(iris[,1:3], x4=factor(iris$Petal.Width>1.5), 
                             y=factor(iris$Species=="virginica"))
    str(df)
    
    ######################
    'data.frame':   150 obs. of  5 variables:
     $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
     $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
     $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
     $ x4          : Factor w/ 2 levels "FALSE","TRUE": 1 1 1 1 1 1 1 1 1 1 ...
     $ y           : Factor w/ 2 levels "FALSE","TRUE": 1 1 1 1 1 1 1 1 1 1 ...
    

    Here is the partial plot for x4:

    library(randomForest)
    RF <- randomForest(y~., data=df)
    
    partialPlot(x=RF, pred.data=df, x.var=x4, which.class="TRUE")
    

    enter image description here