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

Rub*_*sen 5 plot r machine-learning random-forest categorical-data

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'
Run Code Online (Sandbox Code Playgroud)

.

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!

Mar*_*dri 2

partialPlot这是如何使用分类解释变量的简单示例。检查您的输入的类别是否partialPlot与本示例相同。
我希望这可以帮助你。
数据集df有一个二元自变量x4和一个二元响应变量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 ...
Run Code Online (Sandbox Code Playgroud)

这是 的部分图x4

library(randomForest)
RF <- randomForest(y~., data=df)

partialPlot(x=RF, pred.data=df, x.var=x4, which.class="TRUE")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述