Skip to content
Prev 261602 / 398502 Next

Function to save plots

Hi Merik,

I believe this does what you want (and maybe a little more):

## define the function, a1 and a2 correspond to your arguments
## d is the directory to save the file in (by default the working directory)
## ... are additional arguments that can be passed to png() to control
things like quality, etc.
roc <- function(a1, a2, d = getwd(), ...) {

## someone wiser may know a better way, but I put dev.off() into
on.exit() because
## otherwise, if the plot fails (say due to an error in prediction()
or performance())
## the device does not get shutdown otherwise, and the image is empty
but still used by R
  on.exit(dev.off())

## I use get() to deal with the fact that the dataset name is passed
as a character
  dat <- get(a1)

  png(filename = paste(d, "/", a1, "_", a2, ".png", sep = ''), ...)
  plot(performance(prediction(dat[, a2], dat[, "goldstandard"]),
    "tpr", "fpr"))
## I like titles (if you don't need it, just delte this line)
  title(main = paste(a2, "in dataset:", a1))
}


## Test dataset
test1 <- data.frame(method1 = rbinom(40, 1, .4),
  method2 = rbinom(40, 1, .4), goldstandard = rbinom(40, 1, .8))


## Example Usage
roc(a1 = "test1", a2 = "method1")
roc(a1 = "test1", a2 = "method1", d = "DRIVE:/AlternatePath")
roc(a1 = "test1", a2 = "method1", width = 960, height = 960)

Hope this helps,

Josh
On Wed, Jun 1, 2011 at 12:39 PM, Merik Nanish <merik.nanish at gmail.com> wrote: