# how to plot 3D partial dependence in GBM

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

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 :

• +1 for minimal reproducible example and the tips of using reshape. Feb 21, 2018 at 19:20
• 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. Feb 21, 2018 at 19:25

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])

• when i input the code, and press enter, there is "+", could you please show me the full code, thanks Feb 29, 2016 at 10:59
• I missed a quotation. Should work now Feb 29, 2016 at 12:20
• Error in plot.gbm(GBMmodel, i.var = c("Height", "Weight"), n.trees = 100) : Plot variables not used in gbm model fit: HeightWeight Feb 29, 2016 at 14:34
• 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. Feb 29, 2016 at 14:37
• plot.gbm(GBMmodel,i.var=c(4,3),n.trees=best.iter) it shows contour, not 3D figure Mar 1, 2016 at 1:06

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

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.