Loo moment match advice with random effects model fit in cmdstanr


I’m attempting to use moment matching with loo for a model fitted in R with cmdstan that includes random effects. (Loo without moment matching fits fine, but throws up pareto-k warnings).

Unfortunately, I get the following error message:
Error: Model has 26 unconstrained parameter(s), but 36 were provided!

My sleuthing so far leads me to wonder if this might be because one of the random effects parameters is a correlation matrix, which is I think constrained:

cholesky_factor_corr[4*K] L_u;

K in this instance is 1 - so my best guess is that this parameter may be adding 16 parameters to the model, but only 6 unconstrained ones, which is leading to the error message.

Does this sound even vaguely plausible? And if so, is there any advice on how I can deal with this?

Happy to give more code if helpful - but not sure I am even in the right ballpark in my thoughts about this, and the full model is quite complicated, so thought I’d start fairly general.

Many thanks for any help you can give!

Operating System: Windows, working in R
CmdStan Version: 2.34.1

1 Like

Welcome to discourse. Can you show the line of code that results in the error?

Thanks for catching this! There was a bug in the unconstrain_draws() function (used by moment matching) which would assume the wrong sizes for the return in some cases.

Can you try installing this branch:


And letting me know if it fixes the error for you? Make sure to recompile and re-fit your model after installing the branch, otherwise the old implementation will still be used

I happened to have the same issue yesterday (and showed to @andrjohns), but my example was that complex, that we were not sure about the cause. The new branch did fix it for me.

1 Like

Great, thanks for confirming! I’ve merged the branch in, and I’ll do a 0.8.1 bugfix release on Monday (in case anything else comes up)

1 Like

Also works for me! Thanks so much for the really speedy help, glad my timing was serendipitous!

1 Like