"One or more factor levels in the outcome has no data" 或 "Failed Stopping"

"One or more factor levels in the outcome has no data" or "Failed Stopping"

在 R 中,我正在尝试打包如下数据框:

library(datasets)
require(e1071)

fit <- function(x,y,method="rpart", control = rpart.control(maxdepth=1,maxsurrogate = 0)){
  train(x,y,method,control=control)
}

getmode <- function(v) {
  uniqv <- unique(v)
  uniqv[which.max(tabulate(match(v, uniqv)))]
}

bagged <- bag(x = iris[, names(iris) != "Species"],
              y = iris$Species,
              B=4,
              bagControl = bagControl(
                fit = fit,
                predict = predict,
                aggregate = getmode)
              )

我收到错误消息:

Something is wrong; all the Accuracy metric values are missing:
    Accuracy       Kappa    
 Min.   : NA   Min.   : NA  
 1st Qu.: NA   1st Qu.: NA  
 Median : NA   Median : NA  
 Mean   :NaN   Mean   :NaN  
 3rd Qu.: NA   3rd Qu.: NA  
 Max.   : NA   Max.   : NA  
 NA's   :3     NA's   :3    
Error in fitter(btSamples[[iter]], x = x, y = y, ctrl = bagControl, v = vars,  : 
  task 1 failed - "Stopping"

改用 ldaBag 中的函数,有效

library(datasets)
require(e1071)

bagged <- bag(x = iris[, names(iris) != "Species"],
              y = iris$Species,
              B=4,
              bagControl = bagControl(
                fit = ldaBag$fit,
                predict = ldaBag$pred,
                aggregate = ldaBag$aggregate)
)

那么我的自定义拟合和聚合函数有什么问题?

原来我需要包 rpart

require(rpart)

这是我通过在不同的环境中测试代码发现的,该环境启用了警告 - 虽然我从未有意禁用它们,但它们在以前的环境中被禁用了。

因此,我看不到完整的消息:

Error in fitter(btSamples[[iter]], x = x, y = y, ctrl = bagControl, v = vars,  : 
  task 1 failed - "Stopping"
In addition: There were 50 or more warnings (use warnings() to see the first 50)
> warnings()
Warning messages:
1: In (function (kind = NULL, normal.kind = NULL, sample.kind = NULL)  ... :
  non-uniform 'Rounding' sampler used
2: model fit failed for Resample01: cp=0 Error in rpart.control(maxdepth = 1, maxsurrogate = 0) : 
  could not find function "rpart.control"

我使用 options(warn=0)

反应了警告