简体   繁体   English

如何使用 R 中的 pdp 来计算 3d 部分依赖图?

[英]How to use the pdp in R to compute 3d partial dependence plots?

I have a Random forest model in R similar to this:我在 R 中有一个与此类似的随机森林 model:

library("randomForest")
library("caret")
library("pdp")
data("cars")
my_data<-cars[1:5]
my_rf <- randomForest( Price ~ ., data=my_data)
price_mil<- partial(my_rf, pred.var = c("Price", "Mileage"))
plotPartial(price_mil, levelplot = FALSE, zlab = "Price", colorkey = TRUE)

However, I would like to have some 3d partial dependence plots, including the values of parameters on the axis.但是,我想要一些 3d 部分依赖图,包括轴上的参数值。 How can I do this with pdp ?我怎么能用pdp做到这一点?

First of all, In your example you used "price" in the partial() function.首先,在您的示例中,您在partial() function 中使用了“价格”。 This does not make sense to me, as you essentially just plot a 2d partial dependence plot that way.这对我来说没有意义,因为你基本上只是 plot 二维部分依赖 plot 那样。 I changed that in my example code below.我在下面的示例代码中更改了它。

However, to get the requested partial plots you can use但是,要获得请求的部分图,您可以使用

plotPartial(price_mil, zlab = "Price", levelplot = F, scale = list(arrows = F))

If you want to have more control, I would advise to use the underlying functions of the package to construct your formula and wireframe object and then call wireframe() with scale=list(arrows = F) to add the values to the axes.如果您想拥有更多控制权,我建议您使用 package 的底层函数来构建您的公式和线框 object,然后使用scale=list(arrows = F)调用wireframe()以将值添加到轴。

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

object <- pdp::partial(my_rf, pred.var = c("Cylinder", "Mileage"))

form <- stats::as.formula(paste("yhat ~", paste(names(object)[1L:2L], 
                                                collapse = "*")))

wireframe(form, data = object, drape =T, zlab = "Price", scale = list(arrows = F))

yields产量

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM