Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

r-caret

rfe

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?

like image 985
user2684099 Avatar asked Aug 14 '13 21:08

user2684099


1 Answers

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
like image 116
David Avatar answered Oct 14 '22 22:10

David