Hello, I have a bootstrap implementation loop that I would like to replace by a faster "batch operator". The loop is as follows: for (b in 1:B) { indices <- sample(1:N, size=N, replace=TRUE) # sample n elements with replacement theta_star[b,] = statistic(data,indices) # execute statistic callback function } I would like to replace this by something like (does not work): indices <- replicate(B,sample(1:N, size=N, replace=TRUE)) theta_star <- apply(indices,2,statistic,data) This, however, does not work because the statistic function receives indices as second parameter and not as first parameter. While I could change the signature of my statistic function I would like to know how to achieve this without having to do so. TIA, Best regards, Giovanni [[alternative HTML version deleted]]
On Apr 30, 2010, at 4:52 PM, Giovanni Azua wrote:> Hello, > > I have a bootstrap implementation loop that I would like to replace > by a faster "batch operator". > > The loop is as follows: > > for (b in 1:B) { > indices <- sample(1:N, size=N, replace=TRUE) # sample n elements > with replacement > theta_star[b,] = statistic(data,indices) # execute statistic > callback function > } > > I would like to replace this by something like (does not work):Note: Loops may be just as fast or faster than apply calls.> > indices <- replicate(B,sample(1:N, size=N, replace=TRUE)) > theta_star <- apply(indices,2,statistic,data)Why not: theta_star <- apply(indices,2, function(x) statistic(data, x) ) May end up as a data class that needs further work, depending on what "statistic" return.> > This, however, does not work because the statistic function receives > indices as second parameter and not as first parameter. While I > could change the signature of my statistic function I would like to > know how to achieve this without having to do so. > > TIA, > Best regards, > Giovanni > [[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.David Winsemius, MD West Hartford, CT
Hello David, On Apr 30, 2010, at 11:00 PM, David Winsemius wrote:> Note: Loops may be just as fast or faster than apply calls. >How come!? is this true also for other similar functions: lapply, tapply and sapply? Then the only advantage of these above is only syntactic sugar?>> >> indices <- replicate(B,sample(1:N, size=N, replace=TRUE)) >> theta_star <- apply(indices,2,statistic,data) > > Why not: > > theta_star <- apply(indices,2, function(x) statistic(data, x) ) > > May end up as a data class that needs further work, depending on what "statistic" return. >Nice! thank you! Best regards, Giovanni
Hi r-help-bounces at r-project.org napsal dne 30.04.2010 23:11:54:> Hello David, > > On Apr 30, 2010, at 11:00 PM, David Winsemius wrote: > > Note: Loops may be just as fast or faster than apply calls. > > > How come!? is this true also for other similar functions: lapply, tapplyand sapply?> > Then the only advantage of these above is only syntactic sugar?Yes and no. If your loop(s) is poorly written it can be much slower then *apply operator but usually not from loop point of view but from the code itself. Regards Petr> > >> > >> indices <- replicate(B,sample(1:N, size=N, replace=TRUE)) > >> theta_star <- apply(indices,2,statistic,data) > > > > Why not: > > > > theta_star <- apply(indices,2, function(x) statistic(data, x) ) > > > > May end up as a data class that needs further work, depending on what > "statistic" return. > > > Nice! thank you! > > Best regards, > Giovanni > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.