我还在学习使用caret
包,通过Max Kuhn的caret包,在尝试使用plsBayesFit
模型进行预测时,卡在了16.2部分最小二乘判别分析部分,如书中所示,使用predict(plsBayesFit, head(testing), type = "prob")
进行预测。
所使用的数据是mlbench
包中的data(Sonar)
,数据被分割如下:
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)centerScaletraining <- 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
我已经检查了训练集和测试集,确认没有变量缺失。我还尝试使用用于渲染该书时的pls
包的2.7.1版本进行预测,但同样出现了相同的错误。发生了什么?
回答:
我尝试使用不同的模型来复制你的问题,因为我也遇到了这个error
,但我失败了;而且caret
现在似乎与我使用时表现不同。
无论如何,我偶然发现了这个Github
问题这里,看起来klaR
包存在一个特定的问题。所以我的猜测是,这仅仅是一个bug
– 这里无法立即修复!