You seem to have a lot of for loops. You can try vectorizing the assigns, but those probably won’t do much since for loops in Stan are pretty fast because they’re compiled to C++. What might make a bigger difference is if you vectorize the sampling statements that are in for loops such as betas[i] ~ multi_normal_cholesky(mu_b,L_b);
That multi_normal PDF involves calculating the determinant of the covariance matrix, so you’re doing that for every iteration of that for loop, even though the determinant looks to be the same in every iteration. If you vectorized, Stan’s autodiff is smart enough to pick up on the fact that the covariance matrix is the same for every iteration and it’ll only compute the determinant once. Depending on how big the covariance matrix is, that can give you a big speedup.
See chapter 28 of the Stan manual for more on vectorization and optimizing Stan code.
Another reason for slow sampling is large tree depths. Large tree depths often happen when the NUTS chains have to go far with a small stepsize. The most common cause of that is highly-correlated posteriors. So I would check to see what your tree-depth values are, and if they’re high, try looking at pairs plots of your posterior samples to identify parameters that are highly-correlated in the posterior.