R package, Caret RFE function, how to customize metric to use AUC?

7.7k views Asked by At

I want to use AUC as the performance measure, but RFE only support RMSE, RSquared, Accuracy, Kappa. How can I use a customized metric such as auc?

1

There are 1 answers

1
David On BEST ANSWER

You have to specify a custom summaryFunction() within your trainControl() object and then select an appropriate section metric from that summaryFunction(). Caret also includes a function for AUC called twoClassSummary() so you don't even have the write that yourself. Here is an example:

> library(caret)
> iris <- iris[1:100,]
> iris$Species <- as.factor(as.character(iris$Species))
> 
> tc <- trainControl(method="cv",summaryFunction=twoClassSummary,classProb=T)
> train.rf <- train(Species ~ .,data=iris, method="rf", trControl=tc, metric =  "ROC")
> train.rf
100 samples
  4 predictors
  2 classes: 'setosa', 'versicolor' 

No pre-processing
Resampling: Cross-Validation (10 fold) 

Summary of sample sizes: 90, 90, 90, 90, 90, 90, ... 

Resampling results across tuning parameters:

  mtry  ROC  Sens  Spec  ROC SD  Sens SD  Spec SD
  2     1    1     1     0       0        0      
  3     1    1     1     0       0        0      
  4     1    1     1     0       0        0      

ROC was used to select the optimal model using  the largest value.
The final value used for the model was mtry = 2. 

EDIT: Just realized that you want it for rfe() - the same thing holds but you then have to edit the "summary" element of your rfeFuncs object in the same fashion. Ex:

rfFuncs$summary <- twoClassSummary
rfe(iris[,-5],iris[,5],rfeControl = rfeControl(rfFuncs), s=2:3,metric="ROC")
Recursive feature selection

Outer resampling method: Bootstrap (25 reps) 

Resampling performance over subset size:

 Variables ROC Sens Spec ROCSD SensSD SpecSD Selected
         2   1    1    1     0      0      0        *
         3   1    1    1     0      0      0         
         4   1    1    1     0      0      0         

The top 2 variables (out of 2):
   Petal.Width, Petal.Lengt