Hello Everyone,

I am trying to fit a mixed effect model in which there is a correlation within each id. Here is my data structure.

I fitted stan model with loop, but it takes longer time. I would really appreciate if anyone could given suggestion to make it fast. I have given the part of stan code for your review.

Thanks

Marimuthu.

N - Total number of observations

K - Number of id

Ni[K] - Number of observations in each id

P - Number of covariates

S (N by N) - within study covariance matrix

L (P by P)- between study covariance matrix

```
int pos;
pos = 1;
// Likelihood
for (k in 1:K) {
int nk;
nk=Ni[k];
matrix[nk,P] Xk;
vector[nk] Yk;
matrix[nk, nk] Sk ;
Xk = block(X, pos, 1, nk, P);
Yk = segment(y, pos, nk);
Sk=block(S, pos, pos, nk, nk);
Yk ~ multi_normal(Xk*beta+ Xk*u_beta[k,]', Sk+Xk*L*Xk');
// Update position
pos = pos + nk;
}
```