使用 Caret 进行交叉验证重采样时缺少值

Missing values in cross validation resampling with Caret

我正在尝试使用 Caret 包中的 train() 函数来拟合 K 最近邻模型。这给了我一个错误。我的代码是:

 "%+%" <- function(x,y) paste(x, y, sep = "")
 set.seed(28)
 ContEnt <- trainControl(method = "repeatedcv", number = 10, repeats = 3)
 EducKnn <- train(as.formula("pp04b_cod ~ " %+% paste(VarEduc[!VarEduc %in% NoRel], collapse 
                = " + ")), EducPrueba, method = "knn", trctrl = ContEnt,
               tuneLength = 10)

这给了我 return:

Warning: predictions failed for Resample01: k= 5 Error in knn3Train(train = structure(c(0.569069692629571, 0.569069692629571,  : 
  unused argument (trctrl = list("cv", 10, NA, "grid", 0.75, NULL, 1, TRUE, 0, FALSE, TRUE, "final", FALSE, FALSE, function (data, lev = NULL, model = NULL) 
{
    if (is.character(data$obs)) data$obs <- factor(data$obs, levels = lev)
    postResample(data[, "pred"], data[, "obs"])
}, "best", list(0.95, 3, 5, 19, 10, 0.9), NULL, NULL, NULL, NULL, 0, c(FALSE, FALSE), NA, list(5, 0.05, "gls", TRUE), FALSE, TRUE))

很多类似的警告信息,最后:

Warning in nominalTrainWorkflow(x = x, y = y, wts = weights, info = 
trainInfo,  :
There were missing values in resampled performance measures.
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   :10    NA's   :10   
Error: Stopping
> 

设置参数以避免交叉验证,似乎无法解决问题。

ContEnt <- trainControl(method = "none")

此外,在 train() 中设置 na.action = na.omit 会得到相同的结果。有趣的是,class 包的 knn() 函数在同一组变量上使用 .75 训练集完美运行。

Entre <- createDataPartition(EducPrueba$pp04b_cod, 1, 0.75, list = FALSE)
EducKnn <- knn(train = EducPrueba[Entre, VarEduc[!VarEduc %in% NoRel]], test = EducPrueba[-Entre, 
VarEduc[!VarEduc %in% NoRel]], cl = EducPrueba$pp04b_cod[Entre], k = 5)

最后,可以肯定的是,EducPrueba 没有 NA 或 NaN:

> any(is.na(EducPrueba))
[1] FALSE
> any(unlist(lapply(EducPrueba, is.nan)))
[1] FALSE

VarEduc 中的变量已经居中和缩放。有谁知道如何让它工作?我将 R Portable 3.5.2 与 RStudio 一起使用。包 caret 6.0-81 和 class 7.3-15。我不知道如何上传数据框,所以这可以作为一个可重现的例子。

重现错误的方法如下:

train(Species~.,data=iris,trctrl=trainControl(method="cv",numebr=5),
      metric="Accuracy",method="knn")
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: Stopping In addition: There were 50 or more warnings (use warnings() to see the first 50)

这是相同的模型,但有建议的改动:

train(Species~.,data=iris,trControl=trainControl(method="cv",number=5),
       metric="Accuracy",method="knn")
k-Nearest Neighbors 

150 samples
  4 predictor
  3 classes: 'setosa', 'versicolor', 'virginica' 

No pre-processing
Resampling: Cross-Validated (5 fold) 
Summary of sample sizes: 120, 120, 120, 120, 120 
Resampling results across tuning parameters:

解决您的问题 您需要将 trctrl 更改为 trControl

trainControl(method = "repeatedcv", number = 10, repeats = 3)
 EducKnn <- train(as.formula("pp04b_cod ~ " %+% paste(VarEduc[!VarEduc %in% NoRel], collapse 
                = " + ")), EducPrueba, method = "knn", trControl= ContEnt,
               tuneLength = 10)