ROC – plot

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"))
This entry was posted in R. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s