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...)
Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. It only takes a minute to sign up.
Sign up to join this communityI 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)
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)
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])
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.