[R] sapply and loop
Zhen Pang
nusbj at hotmail.com
Tue Oct 19 03:59:08 CEST 2004
I tried to use Rprof(). As an example, I consider the following code (from
Venables & Ripley, 1999).
library(MASS); library(boot); library(nls)
data(stormer)
storm.fm <- nls(Time ~ b*Viscosity/(Wt - c), stormer,
start = c(b=29.401, c=2.2183))
st <- cbind(stormer, fit=fitted(storm.fm))
storm.bf <- function(rs, i) {
st$Time <- st$fit + rs[i]
tmp <- nls(Time ~ (b * Viscosity)/(Wt - c), st,
start = coef(storm.fm))
tmp$m$getAllPars()
}
rs <- scale(resid(storm.fm), scale = FALSE) # remove the mean
Rprof("boot.out")
storm.boot <- boot(rs, storm.bf, R = 4999) # pretty slow
Rprof(NULL)
summaryRprof()
Error in summaryRprof() : no events were recorded
I am using R1.8.1 in windows. Why can't I get the results?
Zhen
>From: Thomas Lumley <tlumley at u.washington.edu>
>To: Zhen Pang <nusbj at hotmail.com>
>CC: r-help at stat.math.ethz.ch
>Subject: Re: [R] sapply and loop
>Date: Mon, 18 Oct 2004 09:14:49 -0700 (PDT)
>
>On Sat, 16 Oct 2004, Zhen Pang wrote:
>
>>Dear all,
>>
>>I am doing 200 times simulation. For each time, I generate a matrix and
>>define some function on this matrix to get a 6 dimension vector as my
>>results.
>>
>>As the loop should be slow, I generate 200 matrice first, and save them
>>into a list named ma,
>>then I define zz<-sapply(ma, myfunction)
>>
>>To my surprise, It almost costs me the same time to get my results if I
>>directly use a loop from 1 to 200. Is it common? Can I improve any
>>further?
>>
>
>It is quite common for a loop to be as fast as sapply(). After all,
>sapply() still has to run `myfunction' 200 times, and this is what takes
>most of the time, so there isn't any obvious reason why sapply() should be
>much faster. sapply() and lapply() certainly can be faster than loops, but
>usually not by very much.
>
>The surprising fact is that so many people *believe* the apply() functions
>are typically much faster than loops. It's probably a useful belief, since
>it encourages people to learn to use them (and they sometimes are faster).
>
>You should try using Rprof() to find out which parts of your code are slow.
>
> -thomas
More information about the R-help
mailing list