[R] functions of vectors : loop or vectorization

Julien Salanie julien.salanie at gmail.com
Sat Jul 14 00:41:05 CEST 2012


I have a read a lot about the benefits of vectorization in R. I have a
program that takes "almost forever" to run. A good way to see if I have
learned something ... My problem can be summarized like this : I have a
nonlinear function of several variables that I want to optimize over one
letting the other describe a family of curves. In short, I wan't to optimize
f(x,a,b) for several values of a and b.

It is easily done with a loop. Here's an example :

a = 1:5;
b = 1:5;
myfunction = function(x){y*x-(x+z)^2};
myresults = array(dim=c(length(a),length(b)));
for(y in a){ for(z in b) { myresults[y,z] =
optimize(myfunction,c(-10,10),maximum=TRUE)$maximum }};
myresults;

[,1] [,2] [,3] [,4] [,5]
[1,] -0.5 -1.5 -2.5 -3.5 -4.5
[2,]  0.0 -1.0 -2.0 -3.0 -4.0
[3,]  0.5 -0.5 -1.5 -2.5 -3.5
[4,]  1.0  0.0 -1.0 -2.0 -3.0
[5,]  1.5  0.5 -0.5 -1.5 -2.5

Of course, my real life problem is a bit more complicated and runs in days
...

I didn't find a straightforward way to do this using the apply family. I did
a small script that works. Here it is :

c = 1:5;
d = 1:5;
myfunction2 =
function(c,d){optimize(function(x){c*x-(x+d)^2},c(-10,10),maximum=TRUE)$maximum};
v.myfunction2 = Vectorize(myfunction2, c("c","d"));
outer(c, d, v.myfunction2);

all.equal(myresults,outer(c, d, v.myfunction2));
[1] TRUE

I was quite happy with my trick of separating and wrapping the functions
until I increased the size of the two input vectors and checked for the
processing time. I made no gain. In that case :

> time.elapsed; time.elapsed2;
Time difference of 0.08000016 secs
Time difference of 0.07999992 secs

When I changed the size of the vectors and added a logarithm here and there
to complicate a bit, it doesn't change the problem. The two methods perform
identically. Am I missing something ? Is there a better way to vectorize the
problem to gain time ? How is it that my loop performs as well as "outer" ?
Thanks in advance for your help. All the best, Julien

--
View this message in context: http://r.789695.n4.nabble.com/functions-of-vectors-loop-or-vectorization-tp4636494.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list