ROC – plot

[This article was first published on Ecological Modelling... » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

There are many implantation in R already of ROC plots (e.g. in the packages PresenceAbsence, ROCR). I just wrote my own very simple script just to get a better understanding of it.

## ROC - plot
d <- data.frame(id=1:100, ob=sample(c(1,0), 100, replace=T), m1=sample(seq(0,1,by=0.01), 100, replace=T))
# interval to calculate the threshold
int <- 100
th <- seq(0,1, length=int)
roc.plot <- data.frame(sen=rep(NA,int), spe=rep(NA,int))

for (i in 1:int)
{
 # get tn, tp, fn, fp
 tn <- nrow(d[d[,3]<th[i]&d[,2]==0,])
 fn <- nrow(d[d[,3]<th[i]&d[,2]==1,])
 fp <- nrow(d[d[,3]>th[i]&d[,2]==0,])
 tp <- nrow(d[d[,3]>th[i]&d[,2]==1,])

 # sensitivity, if sensitivty == 1, everything all positives are found
 roc.plot[i,'sen'] <- tp/(tp+fn)
 # specificity, if specificity == 1, all negatives are found
 roc.plot[i,'spe'] <- tn/(tn+fp)
}
with(roc.plot, plot(1-spe, sen, type="l"))

To leave a comment for the author, please follow the link and comment on their blog: Ecological Modelling... » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)