[R] for loops in Gibbs sampler

Yulei He yuleih at umich.edu
Fri Jul 16 22:40:43 CEST 2004

Dear all:

I am using R to do multiple imputation for longitudinal data set. The
Gibbs chain basically requires draw posterior distribution of model
parameters, including the random effects. The multiple imputation requires
several independent Gibbs chains. So my program structure is like:

for (chain in 1:5)
 # perform Gibbs sampling...

 for (row in 1:row.no)
  b.row=some function # draw random effects from each row of the data

I used two for loops. I know that for loops should be avoided in R. Since
the Gibbs chains are independent, so does the draw of the random effects
for the data matrix, I am just wondering if there exists faster command in
R to do above operation. I happen to see function sapply(), will it be
faster than my double for loops? Your help will be greatly appreciated.


Yulei He
1586 Murfin Ave. Apt 37
Ann Arbor, MI 48105-3135
yuleih at umich.edu

More information about the R-help mailing list