我正在使用 mixOmics 包在 R 中执行 PLS-DA 分析。我有一个二元 Y 变量(是否存在湿地)和 21 个连续预测变量 (X),其值范围为 1 到 100。我已经使用数据集制作了模型,data_training
并希望使用数据集预测新结果data_validation
。这些数据集具有完全相同的结构。
我的代码如下所示:
library(mixOmics)
model.plsda<-plsda(X,Y, ncomp = 10)
myPredictions <- predict(model.plsda, newdata = data_validation[,-1], dist = "max.dist")
我想根据 10、9、8、... 到 2 个主成分来预测结果。通过使用该get.confusion_matrix
函数,我想估计每个主成分数量的错误率。
prediction <- myPredictions$class$max.dist[,10] #prediction based on 10 components
confusion.mat = get.confusion_matrix(truth = data_validatie[,1], predicted = prediction)
get.BER(confusion.mat)
我可以单独做 10 次,但我想做得快一点。因此,我正在考虑prediction
为每个数量的组件制作一个列表...
library(BBmisc)
prediction_test <- myPredictions$class$max.dist
predictions_components <- convertColsToList(prediction_test, name.list = T, name.vector = T, factors.as.char = T)
...然后将 lapply 与get.confusion_matrix
andget.BER
函数一起使用。但后来我不知道该怎么做。我在互联网上搜索过,但找不到有效的解决方案。我怎样才能做到这一点?
非常感谢您的帮助!