使用 caret::varImp 时如何修复错误?

时间:2021-02-27 21:03:55

标签: r r-caret

我正在尝试为以下模型找到 varImp,

<块引用>

线性判别分析

50 个样本 25 个预测变量 2 个类别:'L'、'Lpo'

无预处理重采样:留一法交叉验证总结 样本大小:49, 49, 49, 49, 49, 49, ... 使用 烟雾

重采样结果:

ROC 传感器规格
0.9433345 0.9439082 0.8942777

caret::varImp(result$models[[2]], scale = TRUE)

是否有简单的方法可以解决此错误:

Error in y - mean(y, rm.na = TRUE): non-numeric argument to binary operator
Traceback:

1. caret::varImp(result$models[[2]], scale = TRUE)
2. varImp.train(result$models[[2]], scale = TRUE)
3. filterVarImp(x_dat, y_dat, nonpara = nonpara, ...)
4. apply(x, 2, testFunc, y = y)
5. FUN(newX[, i], ...)

My error

1 个答案:

答案 0 :(得分:0)

正如@Zyad Shehadeh 和@missuse 在评论中所说,您必须dput 一些数据或使用内置数据集重现您的错误。当我试图重现您的情况(您没有很好地解释)时,使用 iris 数据集,一切正常:我假设您正在运行分类(由于 method="lda")不平衡数据集(由于 additional sampling SMOTE)。我用 iris 模拟这种数据:

library(caret)

data("iris")
#eliminate a level in the Species column and reduce the observation for Species="versicolor" from 50 to 10 to imbalance the outcome 
iris <- iris[-c(1:90),]
iris$Species <- factor(iris$Species,levels = unique(iris$Species))
cn <- trainControl(method = "LOOCV",sampling="smote",classProbs = T,summaryFunction = 
twoClassSummary)
fit <- train(Species~.,data = iris,trControl=cn,method="lda",metric="ROC")

caret::varImp(fit, scale = TRUE)

一切正常,所以问题在某种程度上与您的数据有关,而与代码无关