R 插入符包 rfe 错误 - "argument is not interpretable as logical"

R caret package rfe error - "argument is not interpretable as logical"

我正在尝试将 rfeControl 和 rfe 用于使用 svm 的简单特征选择任务。输入文件很小,有 20 个特征和 414 个样本。可以在此处找到输入 [https://www.dropbox.com/sh/hj91gd06dbbyi1o/AABTHPuP4kI85onSqBiGH_ISa?dl=0]

忽略警告,我不明白下面的错误是,据我所知,当 metric==RMSE 时最大化取值,而我在执行分类时具有 metric==Accuracy(参考:https://github.com/topepo/caret/blob/master/pkg/caret/R/rfe.R):

Error in if (maximize) which.max(x[, metric]) else which.min(x[, metric]) : 
argument is not interpretable as logical
In addition: Warning message:
In if (maximize) which.max(x[, metric]) else which.min(x[, metric]) :
the condition has length > 1 and only the first element will be used

代码如下:

library("caret")
library("mlbench")
sensor6data_2class <- read.csv("/home/sensei/clustering/svm_2labels.csv")
sensor6data_2class <- within(sensor6data_2class, Class <- as.factor(Class))
sensor6data_2class$Class2 <- relevel(sensor6data_2class$Class,ref="1")

set.seed("1298356")
inTrain <- createDataPartition(y = sensor6data_2class$Class, p = .75, list = FALSE)
training <- sensor6data_2class[inTrain,]
testing <- sensor6data_2class[-inTrain,]
trainX <- training[,1:20]
y <- training[,21]

ctrl <- rfeControl(functions = rfFuncs , method = "repeatedcv", number = 5, repeats = 2, allowParallel = TRUE)
model_train <- rfe(x = trainX, y = y, sizes = c(10,11), metric = "Accuracy" , Class2 ~ ZCR + Energy + SpectralC + SpectralS + SpectralE + SpectralF + SpectralR + MFCC1 + MFCC2 + MFCC3 + MFCC4 + MFCC5 + MFCC6 + MFCC7 + MFCC8 + MFCC9 + MFCC10 + MFCC11 + MFCC12 + MFCC13, rfeControl = ctrl, method="svmRadial")

提前致谢。

您的代码中存在多个错误。

  1. 您正在创建一个新的 class2,但没有选择它作为 Y,您正在选择 Class
  2. 您正在使用 rfe 中的公式表示法以及 x 和 y 表示法。这会导致您收到错误。使用 x 和 y 或使用公式表示法。检查下面的示例代码。

下面的代码有效:

library("caret")
sensor6data_2class <- read.csv("svm_2labels.csv")
sensor6data_2class$Class <- as.factor(sensor6data_2class$Class)
# sensor6data_2class$Class2 <- relevel(sensor6data_2class$Class,ref="1")

set.seed("1298356")
inTrain <- createDataPartition(y = sensor6data_2class$Class, p = .75, list = FALSE)
training <- sensor6data_2class[inTrain,]
testing <- sensor6data_2class[-inTrain,]
trainX <- training[,1:20]
y <- training[,21]

ctrl <- rfeControl(functions = rfFuncs , 
                   method = "repeatedcv",
                   number = 5, 
                   repeats = 2, 
                   allowParallel = TRUE)
set.seed("1298356")
model_train <- rfe(x = trainX, 
                   y = y, 
                   sizes = c(10,11), 
                   metric = "Accuracy" , 
                   rfeControl = ctrl)
set.seed("1298356")
model_train_form <- rfe(Class ~ ZCR + Energy + SpectralC + SpectralS + SpectralE + SpectralF + SpectralR + MFCC1 + MFCC2 + MFCC3 + MFCC4 + MFCC5 + MFCC6 + MFCC7 + MFCC8 + MFCC9 + MFCC10 + MFCC11 + MFCC12 + MFCC13, 
                        data = training,
                        sizes = c(10,11), 
                        metric = "Accuracy",
                        rfeControl = ctrl)