请参阅下面的循环 - 它调用绘图k(k-1)/ 2次,但实际上并没有生成绘图..但是如果我更改代码以手动调用绘图(例如plot(my_tree,c( 1,2),..),产生的图。)
my_tree是一个GBM对象。请参阅下面的完整代码
#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)
}
}
====完整的程序
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)
}
}
答案 0 :(得分:2)
如果您查看plot.gbm
的帮助页面,它会提到使用的是格子图,所以这很可能是FAQ 7.22的情况。尝试“打印”这些情节。