【发布时间】:2021-10-06 15:46:04
【问题描述】:
我仍在通过The caret Package by Max Kuhn 学习使用caret 包,并在尝试使用plsBayesFit 模型通过predict(plsBayesFit, head(testing), type = "prob") 进行预测时陷入了16.2 Partial Least Squares Discriminant Analysis 部分,如书中所示。
使用的数据是来自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)
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 包进行预测,该包当时用于渲染这本书,但这也给了我同样的错误。发生了什么事?
【问题讨论】:
标签: machine-learning data-science r-caret naivebayes least-squares