An Intuitive Approach to ROC Curves (with SAS & R)

May 6, 2011

(This article was first published on Econometric Sense, and kindly contributed to R-bloggers)

I developed the following schematic (with annotations) based on supporting documents (link) from the article cited below. The authors used R for their work. The ROC curve in my schematic was output from PROC LOGISTIC in SAS, the scatterplot with marginal histograms was created in R (code below) using the scored data from PROC LOGISTIC exported using my SAS MACRO %EXPORT_TO_R  (link to SAS macro code)

(click to enlarge)


Selection of Target Sites for Mobile DNA Integration in the Human Genome
Berry C, Hannenhalli S, Leipzig J, Bushman FD, 2006 Selection of Target Sites for Mobile DNA Integration in the Human Genome. PLoS Comput Biol 2(11): e157. doi:10.1371/journal.pcbi.0020157

quote “The data were analyzed using the R language and environment for statistical computing and graphics “
R code for plot was adapted from code provided via the addicted to R graph gallery :

# *------------------------------------------------------------------
# |
# | import scored logit data from SAS - code generated by SAS MACRO %EXPORT_TO_R
# |
# |
# *-----------------------------------------------------------------
# set R working directory
setwd("C:\\Documents and Settings\\wkuuser\\Desktop\\PROJECTS\\Stats Training")
# get data
dat.from.SAS <- read.csv("fromSAS_delete.CSV", header=T)
# check data dimensions
# *------------------------------------------------------------------
# |
# | scatter plot with marginal histograms
# |
# |
# *-----------------------------------------------------------------
# model predicts P(G) so we want these probabilities for each group
# get p(G) data set for the group that is actually green
green <- dat.from.SAS[ dat.from.SAS$class=="G",]
# get p(G) data set for group that is actually red
red <- dat.from.SAS[ dat.from.SAS$class=="R",]
# just look at regular histograms for each group
hist(green$P_G, main = 'histogram for green')
hist(red$P_G, main = 'histogram for red')
# in order to do scatter plots n must be the same for each
# group, randomly sample n = n(green) from red
# Total number of red observations to match green
N <- 24
# Randomly arrange the data and select out N size sample for red
# and test set.
dat <- red[sample(1:N),] <- dat[1:N,]
# does the distribution retain original properties? Yes
hist($P_G, main = 'histogram for red sample')
# *------------------------------------------------------------------
# |
# | create the marginal plots
# |
# |
# *-----------------------------------------------------------------
def.par <- par(no.readonly = TRUE) # save default, for resetting...
# define histograms
Ghist <- hist(green$P_G,plot=FALSE)
Rhist <- hist($P_G, plot=FALSE)
top <- max(c(Ghist$counts, Rhist$counts))
Grange <- c(0,1)
Rrange <- c(0,1)
nf <- layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), c(3,1), c(1,3), TRUE)
plot(green$P_G,$P_G, xlim=Grange, ylim=Rrange, xlab="green", ylab="red")
barplot(Ghist$counts, axes=FALSE, ylim=c(0, top), space=0, main = 'green')
barplot(Rhist$counts, axes=FALSE, xlim=c(0, top), space=0, horiz=TRUE, main = 'red')

Created by Pretty R at

To leave a comment for the author, please follow the link and comment on their blog: Econometric Sense. 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...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , ,

Comments are closed.


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)