# ROC – plot

November 1, 2010
By

(This article was first published on Ecological Modelling... » R, and kindly contributed to R-bloggers)

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"))
```

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...