How to speed up the construction of the random effects in joint models?

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;



Have you profiled if this is really a bottleneck in your program? Cmdstan 2.26 has new profiling support which should tell you easily what takes long.

1 Like

You can replace this function with:

rows_dot_product(randMat, randeff[,sub_ind]');
1 Like

Also, check out the trick here.

I haven’t check recently, but a while ago I benchmarked rows_dot_product and didn’t find it faster than the equivalent loop.

1 Like

Hi Andrew,

Thank you for your suggestion. I tried this function. However, the speed was not improved significantly.

Good to know that new features. I used Rstan which is 2.21 version now. I will try to see this to detect the reasons.

Hi Mike,

You made a good point. The function rows_dot_product did not beat loop in my codes. I will check the website you mentioned.

1 Like