[R] Vectorize a bootstrapped calculation

From: Fernando Saldanha <fsaldan1_at_gmail.com>
Date: Sun 08 May 2005 - 04:18:48 EST


I would like to vectorize a calculation that is bootstrapped, in the sense that each step uses the results of the previous steps. For example:

x <- rep(NA, 5)
y <- rnorm(5)
for (i in 1:5) {

        x[i] <- max(y[i], ifelse(i > 0, min(x[1:i-1], 0))) }

(the functions max and min are used just as an example, I would like to do something like this for arbitrary functions f and g).

It would be nice to be able to write something like:

x <- cumsapply(y, max, min)

where the first function, max in this example, would be applied to the i-th element of y, and the second function, min in the example, would be applied to the vector x[1:i-1].

Of course one can program such a function, the issue is can it be done efficiently, and not just as a wrapping of the for loop above?

FS



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sun May 08 04:24:54 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:38 EST