Hi all,

Does anyone have better ideas to speed up the matrix construction of the random effects in the mixed effect model or joint models? As shown below, I currently wrote my Stan function by calling the random effect per observation per subject. This is really time-consuming in the computation upon the total number of subject and observation. The R package lme4 actually introduced to utilize the block diagonal matrix and Khatri-Rao2 (Khatri and Rao 1968) and Kronecker products for calculation. See the attachment lmer.pdf (518.0 KB) . Thus, no loop is needed. I guess it should run faster compared to loop. However, I am not sure whether Stan currently have similar functions for this kind of product operators. If anyone knows this, feel free to share. Thank you for your help in advance.

```
functions {
vector evaluate_eta(matrix randMat, matrix randeff, int[ ] sub_ind)
{
int N = rows(randMat);
vector[N] eta;
for (n in 1:N)
{eta[n] = randMat[n,] * randeff[,sub_ind[n]] ;}
return eta;
}
}
```

Yan