Reputation: 227
See the loop below--it calls plot k(k-1)/2 times, but no plots are ever actually produced.. Yet if I change the code to call the plots manually (e.g. plot(my_tree,c(1,2),..), plots ARE produced.)
my_tree is a GBM object. See the full code below
#there is nothing wrong with this code, but yet it does not work
for (i in my_tree$var.names) {
for (j in my_tree$var.names) {
if (i < j) plot(my_tree, c(i, j), n.trees=best.iter)
}
}
==== full program below
pdf("demo.pdf")
N <- 100000
alldata <- data.frame();
train <- factor( ifelse(runif(N)<0.5, 'T', 'V'))
X1 <- rnorm(N);
X2 <- rnorm(N);
X3 <- runif(N);
X4 <- rpois(N, lambda=4);
linear.pred <- -3 + 0.25*X1 + 0.125*X2 - X3 + X4**abs(X1)
temp <- binomial()
y <- rbinom(N, 1, p=temp$linkinv(linear.pred))
alldata <- data.frame(train,X1,X2,X3,X4,y)
rm(train,X1,X2,X3,X4,y,linear.pred)
train <- alldata[alldata$train=='T',]
library(gbm)
my_tree<-gbm(y ~ X1 + X2 + X3 + X4,
distribution="bernoulli",
data=train,
train.fraction=0.5,
interaction.depth=8,
n.trees=300,
shrinkage=0.1,
verbose=TRUE)
best.iter <- gbm.perf(my_tree, method="test")
print(best.iter)
summary(my_tree, ntrees=best.iter)
# make one and two ways
for (i in my_tree$var.names) {
plot(my_tree, i, best.iter)
}
#there is nothing wrong with this code, but yet it does not work
for (i in my_tree$var.names) {
for (j in my_tree$var.names) {
if (i < j) plot(my_tree, c(i, j), n.trees=best.iter)
}
}
Upvotes: 1
Views: 1838
Reputation: 49640
If you look at the help page for plot.gbm
it mentions that lattice plots are used, so this could very well be a case of FAQ 7.22. Try "printing" the plots.
Upvotes: 2