R包,Caret RFE函数,如何自定义度量标准以使用AUC?

5

我希望使用AUC作为性能指标,但RFE仅支持RMSE、RSquared、Accuracy和Kappa。那么我如何使用自定义度量标准,比如AUC呢?

1个回答

16

您需要在trainControl()对象中指定自定义的summaryFunction(),然后从该summaryFunction()中选择适当的部分度量标准。Caret还包括一个名为twoClassSummary()的AUC函数,因此您甚至不必自己编写。以下是一个示例:

> 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. 

编辑:刚意识到您想要为 rfe() 进行翻译-同样适用,但您随后必须以同样的方式编辑 rfeFuncs 对象的 "summary" 元素。例如:

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

感谢您这种友好、迅速而且详细的回答! - user2684099

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