运行 gbm() 时找不到对象 p

Object p not found when running gbm()

我知道 GBM: Object 'p' not found; however it did not contain sufficient information to allow the stack to answer. I don't believe this is a duplicate as I've followed what was indicated in this question and the linked duplicate Error in R gbm function when cv.folds > 0 这个问题没有描述相同的错误。

我确实遵循了省略模型中未使用的任何列的建议。

cv.folds大于0时出现这个错误: object 'p' not found

据我所知,将 cv.folds 设置为 0 不会产生有意义的结果 outputs.I 尝试了不同的分布、分数、树等。我确信我的参数设置不正确,但我我这辈子都看不懂它是什么。

模型和输出:

model_output <- gbm(formula = ign ~ . , 
                  distribution = "bernoulli",
                  var.monotone = rep(0,9),
                  data = model_sample,
                  train.fraction = 0.50,
                  n.cores = 1,
                  n.trees = 150,
                  cv.folds = 1,
                  keep.data = T,
                  verbose=T)
Iter   TrainDeviance   ValidDeviance   StepSize   Improve
     1           nan             nan     0.1000       nan
     2           nan             nan     0.1000       nan
     3           nan             nan     0.1000       nan
     4           nan             nan     0.1000       nan
     5           nan             nan     0.1000       nan
     6           nan             nan     0.1000       nan
     7           nan             nan     0.1000       nan
     8           nan             nan     0.1000       nan
     9           nan             nan     0.1000       nan
    10           nan             nan     0.1000       nan
    20           nan             nan     0.1000       nan
    40           nan             nan     0.1000       nan
    60           nan             nan     0.1000       nan
    80           nan             nan     0.1000       nan
   100           nan             nan     0.1000       nan
   120           nan             nan     0.1000       nan
   140           nan             nan     0.1000       nan
   150           nan             nan     0.1000       nan

用于生成错误的最小数据曾经在这里,但是一旦采用@StupidWolf 的建议,它就太小了,下面的建议将通过初始错误。后续会出现错误,发现后会在此处发布解决方案。

并不是为了处理某人设置的情况cv.folds = 1。根据定义,k折意味着将数据分成k部分,在一部分上训练,在另一部分上测试..所以我是不太确定什么是 1 倍交叉验证,如果您查看 code for gbm,第 437 行

  if(cv.folds > 1) {
    cv.results <- gbmCrossVal(cv.folds = cv.folds, nTrain = nTrain,
    ....
    p <- cv.results$predictions
}

它进行预测,并将结果收集到 gbm 中,第 471 行:

  if (cv.folds > 0) { 
    gbm.obj$cv.fitted <- p 
  }

因此,如果 cv.folds ==1,则不会计算 p,但它 > 0 因此会出现错误。

下面是一个可重现的例子:

library(MASS)
test = Pima.tr 
test$type = as.numeric(test$type)-1

model_output <- gbm(type~ . , 
                  distribution = "bernoulli",
                  var.monotone = rep(0,7),
                  data = test,
                  train.fraction = 0.5,
                  n.cores = 1,
                  n.trees = 30,
                  cv.folds = 1,
                  keep.data = TRUE,
                  verbose=TRUE)

给我错误 object 'p' not found

设置为cv.folds = 2,运行流畅....

model_output <- gbm(type~ . , 
                  distribution = "bernoulli",
                  var.monotone = rep(0,7),
                  data = test,
                  train.fraction = 0.5,
                  n.cores = 1,
                  n.trees = 30,
                  cv.folds = 2,
                  keep.data = TRUE,
                  verbose=TRUE)