R插入符包rfe错误 - "参数不能解释为逻辑"

时间:2016-11-10 08:15:24

标签: r r-caret feature-selection rfe

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

忽略警告,我对下面的错误不了解的是,据我所知,当指标== RMSE和I时,最大值取值,但是,当我执行分类时,指标==准确度(参考:{{3} }):

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 个答案:

答案 0 :(得分:1)

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

  1. 您正在创建一个新的class2,但不是选择它,而是选择Class
  2. 您在rfe和x和y表示法中使用公式表示法。这会导致你得到的错误。使用x和y或使用公式表示法。请查看下面的示例代码。
  3. 以下代码有效:

    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)