Skip to content

Timing benefits of mapply() vs. for loop was: Wrap a loop inside a function

3 messages · Doran, Harold, Gabor Grothendieck, Frank E Harrell Jr

#
Note that if you use mapply in the way I suggested, which is not
the same as in your post, then its just as fast.  (Also the version
of mapply in your post gives different numerical results than
the for loop whereas mine gives the same.)   like.mat is the for
loop version, like.mat2 is your mapply version and like.mat3
is my mapply version.
+   like.mat <- matrix(numeric(length(items) * length(theta)), ncol =
+     length(theta))
+   for(i in 1:length(items)) like.mat[i, ] <- pcm(theta, items[[i]],
score[[i]])
+   like.mat
+ }
[1] 1.30 0.00 1.34   NA   NA
+   matrix(mapply(pcm, rep(theta,length(items)), items, score),
+     ncol = length(theta), byrow = TRUE)
[1] 5.70 0.00 5.91   NA   NA
[1] "Mean relative  difference: 1.268095"
+ t(mapply(pcm, items, score, MoreArgs = list(theta = theta)))
[1] 1.32 0.01 1.39   NA   NA
[1] TRUE
On 7/20/06, Doran, Harold <HDoran at air.org> wrote:
#
Gabor Grothendieck wrote:
You might also test mApply in Hmisc.

Cheers
Frank