caret rfe+sum中的特征选择与ROC

7

我一直在尝试使用caret包进行递归特征选择。我需要的是ref使用AUC作为性能度量。在Google上搜索了一个月后,我仍然不能使该过程正常工作。下面是我使用的代码:

library(caret)
library(doMC)
registerDoMC(cores = 4)

data(mdrr)

subsets <- c(1:10)

ctrl <- rfeControl(functions=caretFuncs, 
                   method = "cv",
                   repeats =5, number = 10,
                   returnResamp="final", verbose = TRUE)

trainctrl <- trainControl(classProbs= TRUE)

caretFuncs$summary <- twoClassSummary

set.seed(326)

rf.profileROC.Radial <- rfe(mdrrDescr, mdrrClass, sizes=subsets,
                            rfeControl=ctrl,
                            method="svmRadial",
                            metric="ROC",
                            trControl=trainctrl)

当执行此脚本时,我会得到以下结果:
Recursive feature selection

Outer resampling method: Cross-Validation (10 fold) 

Resampling performance over subset size:

Variables Accuracy  Kappa AccuracySD KappaSD Selected
     1   0.7501 0.4796    0.04324 0.09491         
     2   0.7671 0.5168    0.05274 0.11037         
     3   0.7671 0.5167    0.04294 0.09043         
     4   0.7728 0.5289    0.04439 0.09290         
     5   0.8012 0.5856    0.04144 0.08798         
     6   0.8049 0.5926    0.02871 0.06133         
     7   0.8049 0.5925    0.03458 0.07450         
     8   0.8124 0.6090    0.03444 0.07361         
     9   0.8181 0.6204    0.03135 0.06758        *
    10   0.8069 0.5971    0.04234 0.09166         
   342   0.8106 0.6042    0.04701 0.10326         

The top 5 variables (out of 9):
nC, X3v, Sp, X2v, X1v

该过程始终使用准确度作为性能指标。另一个出现的问题是,当我尝试从使用以下方法获得的模型获取预测时:
predictions <- predict(rf.profileROC.Radial$fit,mdrrDescr)

我收到了以下信息。
In predictionFunction(method, modelFit, tempX, custom = models[[i]]$control$custom$prediction) :
  kernlab class prediction calculations failed; returning NAs

模型无法提供任何预测结果。

以下是通过sessionInfo()获得的信息:

R version 3.0.2 (2013-09-25)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=es_ES.UTF-8       LC_NUMERIC=C               LC_TIME=es_ES.UTF-8       
 [4] LC_COLLATE=es_ES.UTF-8     LC_MONETARY=es_ES.UTF-8    LC_MESSAGES=es_ES.UTF-8   
 [7] LC_PAPER=es_ES.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
 [10] LC_TELEPHONE=C             LC_MEASUREMENT=es_ES.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
 [1] grid      parallel  splines   stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] e1071_1.6-2     class_7.3-9     pROC_1.6.0.1    doMC_1.3.2      iterators_1.0.6 foreach_1.4.1  
 [7] caret_6.0-21    ggplot2_0.9.3.1 lattice_0.20-24 kernlab_0.9-19 

loaded via a namespace (and not attached):
 [1] car_2.0-19         codetools_0.2-8    colorspace_1.2-4   compiler_3.0.2     dichromat_2.0-0   
 [6] digest_0.6.4       gtable_0.1.2       labeling_0.2       MASS_7.3-29        munsell_0.4.2     
 [11] nnet_7.3-7         plyr_1.8           proto_0.3-10       RColorBrewer_1.0-5 Rcpp_0.10.6       
 [16] reshape2_1.2.2     scales_0.2.3       stringr_0.6.2      tools_3.0.2       
1个回答

7

有一个小问题是一个小错误('trControl='而不是'trainControl=')。此外,在将caretFuncs附加到rfe的控制函数之后,您更改了它。最后,您需要告诉trainControl计算ROC曲线。

以下代码可以正常工作:

 caretFuncs$summary <- twoClassSummary

 ctrl <- rfeControl(functions=caretFuncs, 
                    method = "cv",
                    repeats =5, number = 10,
                    returnResamp="final", verbose = TRUE)

 trainctrl <- trainControl(classProbs= TRUE,
                           summaryFunction = twoClassSummary)
 rf.profileROC.Radial <- rfe(mdrrDescr, mdrrClass, 
                             sizes=subsets,
                             rfeControl=ctrl,
                             method="svmRadial",
                             ## I also added this line to
                             ## avoid a warning:
                             metric = "ROC",
                             trControl = trainctrl)


 > rf.profileROC.Radial

 Recursive feature selection

 Outer resampling method: Cross-Validated (10 fold) 

 Resampling performance over subset size:

  Variables    ROC   Sens   Spec   ROCSD  SensSD  SpecSD Selected
          1 0.7805 0.8356 0.6304 0.08139 0.10347 0.10093         
          2 0.8340 0.8491 0.6609 0.06955 0.10564 0.09787         
          3 0.8412 0.8491 0.6565 0.07222 0.10564 0.09039         
          4 0.8465 0.8491 0.6609 0.06581 0.09584 0.10207         
          5 0.8502 0.8624 0.6652 0.05844 0.08536 0.09404         
          6 0.8684 0.8923 0.7043 0.06222 0.06893 0.09999         
          7 0.8642 0.8691 0.6913 0.05655 0.10837 0.06626         
          8 0.8697 0.8823 0.7043 0.05411 0.08276 0.07333         
          9 0.8792 0.8753 0.7348 0.05414 0.08933 0.07232        *
         10 0.8622 0.8826 0.6696 0.07457 0.08810 0.16550         
        342 0.8650 0.8926 0.6870 0.07392 0.08140 0.17367         

 The top 5 variables (out of 9):
    nC, X3v, Sp, X2v, X1v

在预测问题中,你应该使用 rf.profileROC.Radial 而不是 fit 组件:

 > predict(rf.profileROC.Radial, head(mdrrDescr))
       pred    Active  Inactive
 1 Inactive 0.4392768 0.5607232
 2   Active 0.6553482 0.3446518
 3   Active 0.6387261 0.3612739
 4 Inactive 0.3060582 0.6939418
 5   Active 0.6661557 0.3338443
 6   Active 0.7513180 0.2486820

马克斯


谢谢您的评论,但在我的情况下修复拼写错误并不能解决问题。精度性能指标再次被使用,预测问题也没有得到解决。 - José Palma
我不小心漏掉了两件事。我已经更新了上面的代码。 - topepo
因此,在每个大小迭代中,trainControl对象直接从rfe()传递到train()。因此,可以正确地假设train()的所有其他参数都可以以相同的方式进行修改吗? - José Palma
@topepo,在SVM中预处理数据是不必要的吗?如果是,它会在rfe()中传递吗?对于rfeControl的另一个问题,是否有必要传递method="repeatedcv"而不是仅传递method="cv",因为重复交叉验证是打算使用的? - doctorate
你好,如果你有时间,能否看一下这个问题?这也与RFE有关系,链接如下:https://stackoverflow.com/questions/50691553/r-using-my-own-model-in-rferecursive-feature-elimination-to-pick-important-fea - cloudscomputes
似乎trainControl(classProbs = TRUE,summaryFunction = twoClassSummary)告诉R要考虑ROC。 - cloudscomputes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接