predict.NaiveBayes 中的错误:"Not all variable names used in object found in newdata"--(虽然没有遗漏任何变量)

Error in predict.NaiveBayes : "Not all variable names used in object found in newdata"-- (Although no variables are missing)

我仍在学习通过 The caret Package by Max Kuhn and got stuck in 16.2 Partial Least Squares Discriminant Analysis 部分使用 caret 包,同时尝试通过 predict(plsBayesFit, head(testing), type = "prob") 使用 plsBayesFit 模型进行预测,如书中所示.

使用的数据是 data(Sonar) 来自 mlbench 包,数据拆分为:

inTrain <- createDataPartition(Sonar$Class, p = 2/3, list = FALSE)

sonarTrain <- Sonar[ inTrain, -ncol(Sonar)]
sonarTest  <- Sonar[-inTrain, -ncol(Sonar)]

trainClass <- Sonar[ inTrain, "Class"]
testClass  <- Sonar[-inTrain, "Class"]

然后预处理如下:

centerScale <- preProcess(sonarTrain)
centerScale

training <- predict(centerScale, sonarTrain)
testing <- predict(centerScale, sonarTest)

此后使用 plsBayesFit <- plsda(training, trainClass, ncomp = 20, probMethod = "Bayes") 训练模型,然后使用 predict(plsBayesFit, head(testing), type = "prob").

进行预测

当我尝试这样做时,出现以下错误: Error in predict.NaiveBayes(object$probModel[[ncomp[i]]], as.data.frame(tmpPred[, : Not all variable names used in object found in newdata

我检查了训练集和测试集以检查是否有任何缺失的变量,但没有。我还尝试使用 2.7.1 版本的 pls 包进行预测,该包当时用于渲染这本书,但这也给了我同样的错误。发生什么事了?

我曾尝试使用不同的模型来复制您的问题,因为我也遇到过这个 error,但我失败了; caret 现在的行为似乎与我使用它时不同。

无论如何偶然发现了这个 Github-issues hereklaR-package 似乎有一个特定的问题。所以我的猜测是,这只是一个 bug - 没有什么可以在这里轻松修复的!