1
vote
$\begingroup$

I can use the following code to get one-dimensional partial dependence plot. what code can I plot two-variable partial dependence plot, that's the three dimensional figure. Thanks.

plot.gbm(GBMmodel,i.var=4,n.trees=100...)

$\endgroup$
4
votes
$\begingroup$

You can use the R function persp.

Here is an example using diabetes dataset along with the function reshape2::acast to convert a three columns dataframe into a matrix of desired dimension.

We represent the partial dependence plot of the variables age and sex.

library(gbm)
library(reshape2)
data(diabetes, package = 'lars')

y        <- diabetes$y
x        <- diabetes$x
class(x) <- 'matrix'
data     <- data.frame(y, as.data.frame(x))

gbm.model <- gbm::gbm(formula = y ~ . , data = data, distribution = 'gaussian', 
                 shrinkage = 1, bag.fraction = 1, n.trees = 100,
                 interaction.depth = 3, verbose = T, keep.data = F)


partial <- plot(gbm.model, i.var = c(1,2), return.grid = T)

colnames(partial)

mat <- reshape2::acast(data = partial, formula = age ~ sex, value.var = 'y')

persp(x = as.numeric(colnames(mat)), y = as.numeric(rownames(mat)), z=mat,
      zlab = 'partial dependence', xlab = 'sex', ylab = 'age', theta = 30)

We obtain the following plot : enter image description here

$\endgroup$
2
  • $\begingroup$ +1 for minimal reproducible example and the tips of using reshape. $\endgroup$ – Haitao Du Feb 21 '18 at 19:20
  • $\begingroup$ one thing confusing though, is the sex variable. It is a binary variable, but this partial plot shows how response changes with respect to it. $\endgroup$ – Haitao Du Feb 21 '18 at 19:25
1
vote
$\begingroup$

You can provide variable positions like so:

plot.gbm(GBMmodel, i.var=c(4, 10), n.trees=100)

Or variable names:

plot.gbm(GBMmodel, i.var=c("Height", "Weight"), n.trees=100)

Edit:

To make an interactive, three dimensional figure you need a library that supports such plotting:

library(plot3Drgl) # if you don't have this, install it!

Then save the output of the contour plot:

my.plot <- plot.gbm(mod, c(1,4), return.grid = TRUE)

Finally, pass in the appropriate columns as x, y, and z to the plot function:

points3Drgl(x=my.plot[,1], y=my.plot[,2], z=my.plot[,3])
$\endgroup$
5
  • $\begingroup$ when i input the code, and press enter, there is "+", could you please show me the full code, thanks $\endgroup$ – Captain Feb 29 '16 at 10:59
  • $\begingroup$ I missed a quotation. Should work now $\endgroup$ – Zelazny7 Feb 29 '16 at 12:20
  • $\begingroup$ Error in plot.gbm(GBMmodel, i.var = c("Height", "Weight"), n.trees = 100) : Plot variables not used in gbm model fit: HeightWeight $\endgroup$ – Captain Feb 29 '16 at 14:34
  • $\begingroup$ Alright, @Captain, this is where you have to meet me halfway. I have no idea what variables are in your data set. You can obviously navigate a web site and make a comment. You should be smart enough to know that "height" and "weight" are just examples. You have to put the names of the variables you want to plot in their place. $\endgroup$ – Zelazny7 Feb 29 '16 at 14:37
  • $\begingroup$ plot.gbm(GBMmodel,i.var=c(4,3),n.trees=best.iter) it shows contour, not 3D figure $\endgroup$ – Captain Mar 1 '16 at 1:06
0
votes
$\begingroup$

The plotmo R package will plot partial dependencies as persp plots for all pairs of variables for any model. For example:

library(gbm)
data(mtcars)
set.seed(2016)
mod <- gbm(mpg~., data=mtcars[,1:4], n.minobsinnode=1)
library(plotmo)
plotmo(mod, pmethod="partdep")

which gives

plot

You can specify exactly which variables and variable pairs get plotted using plotmo's degree1 and degree2 arguments. Additional examples are in the vignette for the plotmo package.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.