问题描述
我正在使用 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
和 get.BER
函数一起使用。但后来我不知道该怎么做。我在互联网上搜索过,但找不到有效的解决方案。我该怎么做?
非常感谢您的帮助!
解决方法
没有可重复性,就无法对此进行测试,但您需要将每次要运行的代码转换为函数。像这样:
confmat <- function(x) {
prediction <- myPredictions$class$max.dist[,x] #prediction based on 10 components
confusion.mat = get.confusion_matrix(truth = data_validatie[,1],predicted = prediction)
get.BER(confusion.mat)
}
现在重叠:
results <- lapply(10:2,confmat)
这将返回一个列表,其中包含每个 PC 数量的 get.BER
结果,因此 results[[1]] 将是 10 PC 的结果。您不会获得 prediction
或 confusionmat
的值,除非它们包含在 get.BER
返回的结果中。如果你想要所有这些,你需要用 return(list(prediction,confusionmat,get.BER(confusion.mat))
替换函数的最后一行。这将生成一个列表列表,因此 results[[1]][[1]]
将是 10 台 PC 的 prediction
的结果,results[[1]][[2]]
和 results[[1]][[3]]
将是 confusionmat
和 {{ 1}} 分别。