tags:

views:

60

answers:

2

I am trying to use the outer function with predict in some classification code in R. For ease, we will assume in this post that we have two vectors named alpha and beta each containing ONLY 0 and 1. I am looking for a simple yet efficient way to pass all combinations of alpha and beta to predict.

I have constructed the code below to mimic the lda function from the MASS library, so rather than "lda", I am using "classifier". It is important to note that the prediction method within predict depends on an (alpha, beta) pair.

Of course, I could use a nested for loop to do this, but I am trying to avoid this method.

Here is what I would like to do ideally:

alpha <- seq(0, 1)
beta <- seq(0, 1)
classifier.out <- classifier(training.data, labels)
outer(X=alpha, Y=beta, FUN="predict", classifier.out, validation.data)

This is a problem because alpha and beta are not the first two parameters in predict.

So, in order to get around this, I changed the last line to

outer(X=alpha, Y=beta, FUN="predict", object=classifier.out, data=validation.data)

Note that my validation data has 40 observations, and also that there are 4 possible pairs of alpha and beta. I get an error though saying

dims [product 4] do not match the length of object [40]

I have tried a few other things, some of which work but are far from simple. Any suggestions?

A: 

I figured out one decent way to do this. Here it is:

pairs <- expand.grid(alpha, beta)
names(pairs) <- c("alpha", "beta")
mapply(predict, pairs$alpha, pairs$beta, 
    MoreArgs=list(object=classifier.out, data=validation.data))

Anyone have something simpler and more efficient? I am very eager to know because I spent a little too long on this problem. :(

John A. Ramey
It's not exactly clear to me where pairs$alpha and pairs$beta end up being mapped to for the predict function. You might try defining your own function: myPredict <- function(alpha, beta) { predict(object=classifier.out, data=validation.data, x1=alpha, x2=beta) }. mapply could then call myPredict.
Bob Albright
+1  A: 

The problem is that outer expects its function to be vectorized (i.e., it will call predict ONCE with a vector of all the arguments it wants executed). Therefore, when predict is called once, returning its result (which happens to be of length 4), outer complains because it doesn't equal the expected 40.

One way to fix this is to use Vectorize. Untested code:

outer(X=alpha, Y=beta, FUN=Vectorize(predict, vectorize.args=c("alpha", "beta")), object=classifier.out, data=validation.data)
Jonathan Chang