Trouble understanding difference between stan_lmer and a hand coded equivalent

Hi Stan folks,

I’ve been trying to create a model that takes advantage of covariance between observations that arise from recursively partitioning a set of observations. The details aren’t so important, but ultimately I’d like to compare against some simpler models including some that I can fit with stan_lmer.

To this end I’ve simulated some data and fit my tree model and stan_lmer. The simulated data essentially matches the stan_lmer, in that there is no tree related covariance in the model effects. Fitting both models yields essentially identical predictions, however the coefficients differ from each other by some additive constants. I figured this probably points to a difference between how I’ve coded my model and how it’s implemented in stan_lmer but I’ve been unable to spot the difference.

simple_hierarchical.stan (3.4 KB)

Attached is the stan code for my handwritten model, with the tree related portion removed. To the best of my understanding this should be approximately the same as stan_lmer, however the the constant differences are still present.

the formula for the stan_lmer equivalent is:

y ~ x + (x | node) + (1 | ID)

Any help understanding what’s happening would be greatly appreciated.


I didn’t immediately spot the difference either. You might see if brms::make_stancode gives you Stan code that works on simulated data and that you can build on.

Thanks for looking at this Ben, I’ll definitely give generating the code with brms a try.

Solved it, embarrassingly there was no difference (after including the fixed effect). I didn’t realize that coef report the sum of the fixed effects and random effects.