如何计算游侠RF模型的AUC值?

问题描述 投票:2回答:1

如何计算游侠模型的AUC值? Ranger是R中randomForest算法的快速实现。我使用以下代码构建用于分类目的的游侠模型,并从模型中获得预测:

#Build the model using ranger() function
ranger.model <- ranger(formula, data = data_train, importance = 'impurity',   
write.forest = TRUE, num.trees = 3000, mtry = sqrt(length(currentComb)), 
classification = TRUE)
#get the prediction for the ranger model
pred.data <- predict(ranger.model, dat = data_test,)
table(pred.data$predictions)

但我不知道如何计算AUC值

任何的想法 ?

r random-forest auc
1个回答
2
投票

计算AUC的关键是有一种方法可以将您的测试样本从“最有可能为正”到“最不可能为正”进行排名。修改您的培训电话,以包括probability = TRUEpred.data$predictions现在应该是类概率的矩阵。记下与“正面”类对应的列。此列提供了计算AUC所需的排名。

为了实际计算AUC,我们将使用来自Hand and Till, 2001的等式(3)。我们可以按如下方式实现这个等式:

## An AUC estimate that doesn't require explicit construction of an ROC curve
auc <- function( scores, lbls )
{
  stopifnot( length(scores) == length(lbls) )
  jp <- which( lbls > 0 ); np <- length( jp )
  jn <- which( lbls <= 0); nn <- length( jn )
  s0 <- sum( rank(scores)[jp] )
  (s0 - np*(np+1) / 2) / (np*nn)
}   

其中scores将是对应于正类的pred.data$predictions列,而lbls是编码为二元向量的相应测试标签(1表示正数,0-1表示负数)。

© www.soinside.com 2019 - 2024. All rights reserved.