Hi Andrew and everyone,
On the topic of posterior preditive checks without loops: I wrote up a
small set of functions for mapping posteriors functionally:
https://gist.github.com/tpapp/8650643
Examples are at the end. To take a well-known example from the BDA
appendix,
theta_rep <- array (NA, c(n_sims, J))
y_rep <- array (NA, c(n_sims, J))
for (s in 1:n_sims){
theta_rep[s,] <- rnorm (J, schools_sim$mu[s], schools_sim$tau[s])
y_rep[s,] <- rnorm (J, theta_rep[s,], sigma)
}
would be something like
theta_and_y <- map_subarrays(school_sim,
function(mu, tau, sigma) {
theta_rep <- rnorm(J, mu, tau)
list(theta_rep=theta_rep,
y_rep=rnorm(J, theta_rep, sigma))
})
The good news is that this does what I want, and the syntax works great,
simplifying loops (or nested loops) considerably.
The bad news is that it is painfully slow for nontrivial examples. This
comes from the management of list labels, if I understand
correctly. Currently, it is so slow as to be unusable.
If you know any R wizards, maybe they can speed it up a bit. I asked on
the R-devel mailing list, where the message was not approved ("please
ask on R-help") and then didn't get the kind of help I was looking for
on R-help. So I am back to loops, ugly as they are, I don't want to
waste too much time on this now.
Also, aaply from the plyr libray works for with a little tweaking
(cbinding arrays together, destructuring, applying the function, etc),
but that's not much nicer than loops.
Best,
Tamas