Calculate AUC in R?

前端 未结 10 1321
感动是毒
感动是毒 2020-12-07 09:45

Given a vector of scores and a vector of actual class labels, how do you calculate a single-number AUC metric for a binary classifier in the R language or in simple English?

10条回答
  •  广开言路
    2020-12-07 10:07

    As mentioned by others, you can compute the AUC using the ROCR package. With the ROCR package you can also plot the ROC curve, lift curve and other model selection measures.

    You can compute the AUC directly without using any package by using the fact that the AUC is equal to the probability that a true positive is scored greater than a true negative.

    For example, if pos.scores is a vector containing a score of the positive examples, and neg.scores is a vector containing the negative examples then the AUC is approximated by:

    > mean(sample(pos.scores,1000,replace=T) > sample(neg.scores,1000,replace=T))
    [1] 0.7261
    

    will give an approximation of the AUC. You can also estimate the variance of the AUC by bootstrapping:

    > aucs = replicate(1000,mean(sample(pos.scores,1000,replace=T) > sample(neg.scores,1000,replace=T)))
    

提交回复
热议问题