Created
March 23, 2019 16:42
-
-
Save trinker/ffbe18a56d3249ce7bc72a2d38a5d3a2 to your computer and use it in GitHub Desktop.
Calculating AUC: the area under a ROC Curve
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## https://blog.revolutionanalytics.com/2016/11/calculating-auc.html | |
## Load Dependency | |
library(numform) | |
##======================================================= | |
## Make some fake data | |
##======================================================= | |
set.seed(10) | |
actual <- sample(0:1, 100, T, c(.8, .2)) | |
i <- sample(1:100, 20) | |
y <- actual | |
y[i] <- 1 - x[i] | |
y <- ifelse(y < .5, y + .25, y - .25) | |
predicted <- jitter(y, 2) | |
predicted | |
plot(predicted, actual) | |
##======================================================= | |
## Visualize | |
##======================================================= | |
(cuts <- (100:1)/100) | |
values <- list(TPR = rep(NA, 100), FPR = rep(NA, 100)) | |
plot(0:1, 0:1, type = "n", , yaxs="i", , xaxs="i", | |
ylab = 'True Positive Rate', xlab = 'False Positive Rate') # setting up coord. system | |
abline(c(0,1), lty = 'dashed') | |
for (i in 1:100){ | |
p <- cuts[i] | |
(predicted2 <- ifelse(predicted < p, 0, 1)) | |
TPR0 <- TPR | |
FPR0 <- FPR | |
(TN <- sum(predicted2 == 0 & actual == 0)) | |
(FP <- sum(predicted2 == 1 & actual == 0)) | |
(FN <- sum(predicted2 == 0 & actual == 1)) | |
(TP <- sum(predicted2 == 1 & actual == 1)) | |
(TPR <- TP/(TP + FN)) # sensitivity | |
(FPR <- FP/(FP + TN)) # 1 - specificity | |
cat(sprintf('(p:%s| TPR:%s, FPR:%s\n', f_num(p, 3), f_num(TPR, 3), f_num(FPR, 3))) | |
values$TPR[i] <- TPR | |
values$FPR[i] <- FPR | |
points(FPR, TPR, col = "blue", pch = 19) | |
if (i > 1) segments(FPR0, TPR0, FPR, TPR, col= 'blue') | |
Sys.sleep(.1) | |
} | |
Sys.sleep(2) | |
polygon(c(values$FPR, 1, 0), c(values$TPR, 0, 0), col=adjustcolor("red",alpha.f=0.5)) | |
Sys.sleep(2) | |
text(.6, .4, 'AUC', col = 'white', cex = 4) | |
##======================================================= | |
## Compute AUC | |
##======================================================= | |
dFPR <- c(diff(values$FPR), 0) | |
dTPR <- c(diff(values$TPR), 0) | |
(auc <- sum(values$TPR * dFPR) + sum(dTPR * dFPR)/2) | |
Sys.sleep(2) | |
text(.6, .2, f_num(auc,3), col = 'white', cex = 3) | |
#library(pROC) | |
#roc(actual, predicted) |
Author
trinker
commented
Mar 23, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment