首页 > 解决方案 > 使用 caret::varImp 时如何修复错误?

问题描述

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

线性判别分析

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

无预处理 重采样:留一法交叉验证 样本量总结:49、49、49、49、49、49,... 使用 SMOTE 进行额外采样

重采样结果:

ROC Sens 规格
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], ...)

我的错误

标签: rr-caret

解决方案


正如@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)

一切正常,所以问题与您的数据有关,而不是与代码有关


推荐阅读