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:> Hello,
>
> I'm using ROCR to plot ROC Curves and I want to automate the saving of
plots
> into PNG files using a custom function.
>
> My data frames are named like test1, test2, test3. Each data frame has
three
> variables: method1, method2, goldstandard.
>
> Right now, for each plot I have to run:
>
> png('test1_method1.png')
> plot(performance(prediction(test1$method1, test1$goldstandard),
"tpr",
> "fpr"))
> dev.off()
>
> Here is the function I tried to create but I failed:
>
> roc <- function(arg1, arg2){
> ?png(paste(arg1, arg2, "png", sep="_"))
> ?plot(performance(prediction(arg1$arg2, arg1$goldstandard),
"tpr", "fpr"))
> ?dev.off()
> }
>
> I wanted to pass the test name as arg1, and the method name as arg2. Here
> are my problems:
>
> 1) arg1$arg2 causes an error message. How can I use a function argument to
> address a variable in a dataframe?
>
> 2) paste(arg1, arg2, "png", sep=".") doesn't output
something like
> 'test1.method1.png'. How should I deal with that?
>
> Thanks in advance.
>
> ? ? ? ?[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/