r - gbm 函数下标越界

标签 r gbm

我遇到了一个奇怪的问题。我已在笔记本电脑上成功运行此代码,但是当我尝试首先在另一台计算机上运行它时,我收到此警告 未指定分布,假设伯努利...,这是我所期望的,但随后出现此错误: object$var.levels[[i]] 中出现错误:下标超出范围

library(gbm)
gbm.tmp <- gbm(subxy$presence ~ btyme + stsmi + styma + bathy,
                data=subxy,
                var.monotone=rep(0, length= 4), n.trees=2000, interaction.depth=3,
                n.minobsinnode=10, shrinkage=0.01, bag.fraction=0.5, train.fraction=1,
                verbose=F, cv.folds=10)

有人可以帮忙吗?数据结构完全相同,代码相同,R 相同。我在这里甚至没有使用下标。

编辑:traceback()

6: predict.gbm(model, newdata = my.data, n.trees = best.iter.cv)
5: predict(model, newdata = my.data, n.trees = best.iter.cv)
4: predict(model, newdata = my.data, n.trees = best.iter.cv)
3: gbmCrossValPredictions(cv.models, cv.folds, cv.group, best.iter.cv, 
       distribution, data[i.train, ], y)
2: gbmCrossVal(cv.folds, nTrain, n.cores, class.stratify.cv, data, 
       x, y, offset, distribution, w, var.monotone, n.trees, interaction.depth, 
       n.minobsinnode, shrinkage, bag.fraction, var.names, response.name, 
       group)
1: gbm(subxy$presence ~ btyme + stsmi + styma + bathy, data = subxy,var.monotone = rep(0, length = 4), n.trees = 2000, interaction.depth = 3, n.minobsinnode = 10, shrinkage = 0.01, bag.fraction = 0.5, train.fraction = 1, verbose = F, cv.folds = 10)

是否因为我将保存的 R 工作区移动到另一台计算机而有什么关系?

编辑2:好的,所以我已经更新了代码运行的机器上的gbm包,现在我得到了同样的错误。因此,此时我认为较旧的 gbm 软件包可能没有进行此检查,或者较新的版本存在一些问题。我不太了解gbm,无法说。

最佳答案

只是一种预感,因为我看不到您的数据,但我相信当测试集中存在但训练集中不存在的变量级别时,就会发生错误。

当您的因子变量具有大量级别,或者一个级别的实例数较少时,很容易发生这种情况。

由于您使用的是 CV 折叠,因此其中一个循环上的保留集可能对训练数据具有外部级别。

我建议:

A) 使用 model.matrix() 对因子变量进行 one-hot 编码

B) 继续设置不同的种子,直到获得不发生此错误的 CV 分割。

编辑:是的,通过该回溯,您的第三个 CV 保留在其测试集中有一个在训练中不存在的因子水平。因此预测函数看到一个外部值并且不知道该怎么做。

编辑2:这是一个简单的例子来展示我所说的“不在测试集中的因子水平”的含义

#Example data with low occurrences of a factor level:

set.seed(222)
data = data.frame(cbind( y = sample(0:1, 10, replace = TRUE), x1 = rnorm(10), x2 = as.factor(sample(0:10, 10, replace = TRUE))))
data$x2 = as.factor(data$x2)
data

      y         x1 x2
 [1,] 1 -0.2468959  2
 [2,] 0 -1.2155609  6
 [3,] 0  1.5614051  1
 [4,] 0  0.4273102  5
 [5,] 1 -1.2010235  5
 [6,] 1  1.0524585  8
 [7,] 0 -1.3050636  6
 [8,] 0 -0.6926076  4
 [9,] 1  0.6026489  3
[10,] 0 -0.1977531  7

#CV fold.  This splits a model to be trained on 80% of the data, then tests against the remaining 20%.  This is a simpler version of what happens when you call gbm's CV fold.

CV_train_rows = sample(1:10, 8, replace = FALSE) ; CV_test_rows = setdiff(1:10, CV_train_rows)
CV_train = data[CV_train_rows,] ; CV_test = data[CV_test_rows,]

#build a model on the training... 

CV_model = lm(y ~ ., data = CV_train)
summary(CV_model)
#note here: as the model has been built, it was only fed factor levels (3, 4, 5, 6, 7, 8) for variable x2

CV_test$x2
#in the test set, there are only levels 1 and 2.

#attempt to predict on the test set
predict(CV_model, CV_test)

Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : 
factor x2 has new levels 1, 2

关于r - gbm 函数下标越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18640169/

相关文章:

machine-learning - 如何在 sklearn::LGBMClassifier() 中的 LightGBM 分类器的 feature_importances_ 中将 'gain' 设置为特征重要性度量

java - h2o - 无法找到或加载主类代码 7 错误

R插入符/gbm代码不会预测: dim(X) must have a positive length

r - 如何使用基本R将表写入.xlsx?

python - 相当于 R dcast 的 Pandas

r - 'x' 是一个列表,但没有组件 'x' 和 'y'

R:从 h2o.randomForest() 和 h2o.gbm() 绘制树

python - 使用 H2O 实现网格搜索时出现服务器错误 Water.exceptions.H2OIllegalArgumentException

R Shiny - ui.R 似乎无法识别 server.R 读取的数据帧

r - 如何在 macOS 上安装和定位 ODBC 头文件 sql.h 和 sqlext.h