Friday, December 9, 2011

AUC ROC

# for plotting ROC curve
ROC <- function(ranks , n) {
#doesn't work for ties !
TF = rep(0,n)
TF[ranks] = 1;
fp=cumsum(as.numeric(!TF)/as.numeric(sum(!TF)))
tp=cumsum(TF/sum(TF))
return( list( fp = fp, tp = tp))
}
r <- ROC(c(1:5),10)
plot(r$fp, r$tp)



# calculating AUC score
AUC <- function(ranks, n) {
#ranks : ranks = c( 1,2,3);
#n = 5 ; total number
Npos =length(ranks)
Nneg = n - Npos
AUC = 1 - ( sum(ranks) - Npos*(Npos+1)/2 ) / (Npos * Nneg)
return(AUC)
}

No comments: