请参阅下面的循环 - 它调用 plot 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)
}
}