使用plot.gbm生成边缘图时出错

时间:2017-09-23 03:28:59

标签: r gbm

R的总新手 - 我试图从一个BRT我用gbm包完成一些边缘图并继续得到同样的错误。 以下是我的代码; boosted.tree_LRFF是我从完成gbm.fit获得的输出

> plot.gbm(boosted.tree_LRFF, 
+          i.var= 5,
+          n.trees = train.model$finalModel$tuneValue$n.trees,
+          continuous.resolution = 100,
+          return.grid = FALSE,
+          type = "link")
Error in plot.window(...) : need finite 'ylim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf

1 个答案:

答案 0 :(得分:0)

在第一部分中,我只是重新创建用于适合包装的“gbm.pdf”的gbm的数据集:

library(gbm)
N <- 1000
X1 <- runif(N)
X2 <- 2 * runif(N)
X3 <- ordered(sample(letters[1:4], N, replace = TRUE), levels = letters[4:1])
X4 <- factor(sample(letters[1:6], N, replace = TRUE))
X5 <- factor(sample(letters[1:3], N, replace = TRUE))
X6 <- 3 * runif(N)
mu <- c(-1, 0, 1, 2)[as.numeric(X3)]
SNR <- 10 # signal-to-noise ratio
Y <- X1 ** 1.5 + 2 * (X2 ** .5) + mu
sigma <- sqrt(var(Y) / SNR)
Y <- Y + rnorm(N, 0, sigma)
# introduce some missing values
X1[sample(1:N, size = 500)] <- NA
X4[sample(1:N, size = 300)] <- NA
data <- data.frame(Y = Y, X1 = X1, X2 = X2, X3 = X3, X4 = X4, X5 = X5, X6 = X6)

boosted.tree_LRFF <-
gbm(Y ~ X1 + X2 + X3 + X4 + X5 + X6,
data = data,
var.monotone = c(0, 0, 0, 0, 0, 0),
distribution = "gaussian",
n.trees = 1000,
shrinkage = 0.05,
interaction.depth = 3,
bag.fraction = 0.5,
train.fraction = 0.5,
n.minobsinnode = 10,
cv.folds = 3,
keep.data = TRUE,
verbose = FALSE,
n.cores = 1) 

现在我绘制变量x5的树函数值,类似于你的情节:

plot(boosted.tree_LRFF,
i.var = 5,
n.trees = boosted.tree_LRFF$n.trees,
continuous.resolution = 100,
return.grid = FALSE,
type = "link")

我认为你的错误是由于n.trees论证造成的。您可以将其作为常量输入,也可以从GBM拟合对象输入。在我的例子中,我使用了“boosted.tree_LRFF”,它似乎是你的例子中原始拟合对象的名称(虽然我的数据当然不同)。