roc.plot <- function(x,y, plot.it = T, log = "xy", version=0, ...) { # x and y are two vectors of test results # x are results under null hypothesis # y are results under alternative hypothesis # want to plot p(x > z) vs p(y < z) for all # possibilities z # choose z as values of x or y, whichever is shorter # this version doesn't worry about ties # graphical parameters may be passed in via ... # version=0 False alarms/ missing alarms # version=1 Sensitivity/ specificity nx <- length(x) ny <- length(y) n <- nx + ny if(nx < ny) { z <- sort(x) Z <- y nz <- nx nZ <- ny } else { z <- sort(y) Z <- x nz <- ny nZ <- nx } zrank <- (1:n)[c(rep(T, nz), rep(F, nZ))[order(c(z, Z))]] pz <- (1:nz - .5) / nz pZ <- (zrank - 1:nz)/nZ keep <- (pZ > 0) & (pZ < 1) pz <- pz[keep] pZ <- pZ[keep] if(nx < ny) { px <- 1 - pz py <- pZ } else { px <- 1 - pZ py <- pz } if (version==1) { py<- 1-py px<- 1-px } if(plot.it) { if (version==0) plot(px, py, log=log, xlab = "False Positive", ylab = "False Negative", xlim=c(.005,1),ylim=c(.001,1),...) else plot(px, py, log=log, xlab = "Specificity", ylab = "Sensitivity", xlim=c(.005,1),ylim=c(.001,1),...) } # note: if plot.it is false, you can add a curve to another plot # with the command "lines(roc.plot(x, y, F))" invisible(list(x = px, y = py)) }