2017-08-14 48 views
2

我如何計算護林模型AUC值?遊俠是R的快速實現隨機森林算法我使用下面的代碼來構建分類目的遊俠模型,從模型得到的預測:如何計算遊俠RF模型的AUC值?

#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值

任何想法?

回答

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爲負面)。

+0

謝謝你這麼多阿爾喬姆 – user2947767