Calculate AUC in R?

前端 未结 10 1270
感动是毒
感动是毒 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:23

    Along the lines of erik's response, you should also be able to calculate the ROC directly by comparing all possible pairs of values from pos.scores and neg.scores:

    score.pairs <- merge(pos.scores, neg.scores)
    names(score.pairs) <- c("pos.score", "neg.score")
    sum(score.pairs$pos.score > score.pairs$neg.score) / nrow(score.pairs)
    

    Certainly less efficient than the sample approach or the pROC::auc, but more stable than the former and requiring less installation than the latter.

    Related: when I tried this it gave similar results to pROC's value, but not exactly the same (off by 0.02 or so); the result was closer to the sample approach with very high N. If anyone has ideas why that might be I'd be interested.

提交回复
热议问题