Friday, May 6, 2011

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

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


  1. Matt,
    Did you know that there is also a graph gallery for the SG procedures in SAS?
    The graph you want is in the PROC SGRENDER gallery (Sample 35172) and includes a link that takes you to

    If you cut and paste the two calls to PROC TEMPLATE, then the following statements give a scatter plot with marginal histograms for some fake data:

    data a (drop=i); /* fake data */
    do i = 1 to 20;
    x=rannor(1); y = rannor(1); output;
    ods graphics;
    proc sgrender data=a template=scatterhist;
    dynamic YVAR="X" XVAR="Y";

    Actually, I don't like that the SAS Sample uses transparency for the scatter plot.
    Set datatransparency=0 on the SCATTERPLOT statement in order to get the usual scatter plot.

  2. See also "How to create a scatter plot with marginal histograms in SAS"