如何为R中的分类变量创建偏相关图? [英] How can I create a Partial Dependence plot for a categorical variable in R?

查看:658
本文介绍了如何为R中的分类变量创建偏相关图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用r包randomForest,并已成功制作了随机森林模型和重要性图.我正在处理一个二分法的反应和几个分类的预测变量.

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.

但是,我无法弄清楚如何为我的分类变量制作部分依赖图.我尝试使用randomForest命令partialPLot.但是我收到以下错误:

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?

这是我要绘制的图: https: //stats.stackexchange.com/questions/235667/partial-dependence-plot-interpretation-for-categorical-variables

真的很感谢您的帮助.谢谢!

Would really appreciate some help on this. Thanks!

推荐答案

这里是一个简单的示例,说明了如何将partialPlot用作分类解释变量.检查partialPlot的输入的类是否与此示例相同.
希望对您有所帮助.
数据集df具有一个二进制自变量x4和一个二进制响应变量y:

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 ...

这是x4的局部图:

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

partialPlot(x=RF, pred.data=df, x.var=x4, which.class="TRUE")

这篇关于如何为R中的分类变量创建偏相关图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆