Loo works fine (without moment matching), but when I run loo with moment matching, I get the following error.

Error in object@.MISC$stan_fit_instance$log_prob(upars, adjust_transform, :

Exception: lognormal_lpdf: Random variable[551] is -3.05044, but must be nonnegative! (in ‘anon_model’, line 78, column 4 to column 50)

Error: Moment matching failed. Perhaps you did not set ‘save_pars = save_pars(all = TRUE)’ when fitting your model?

Data and code for a reprex are below.

I have seen some prior errors on this forum and therefore I was trying to do moment matching in the same r session that the model was built in (Loo moment matching not working), and I was also using the dev versions of loo and brms (Loo moment_match problem).

I just wondered if there was anything obvious that stands out? I am not particularly experienced with brms, loo and stan, hence I expect it could be something basic.

Any advice would be appreciated.

Regards,

Rich

```
This example only uses approx. 20% of the full dateset and it is only modelling intercepts and variations across participants and items. But hopefully it is sufficient to detail the problem. A sample of the dataset is attached.
Formula & Priors
formula = bf(afc_rtms ~ 1 +
(1 | pID) +
(1 | item_left) +
(1 | item_right),
ndt ~ (1 | pID))
priors = c(
set_prior('normal(6.68, 0.5)', class = 'Intercept'), #
set_prior('normal(5.70, 0.5)', class = 'Intercept', dpar = 'ndt'), #
set_prior('normal(0, 0.1)', class = 'sd'), #
set_prior('normal(0, 0.1)', class = 'sd', dpar='ndt'),
set_prior('normal(0, 0.1)', class = 'sigma') #
)
Model and loo
b0 <- brm(formula = formula,
data = data_sample, family = shifted_lognormal(),
prior = priors,
iter = 4000, warmup = 1000, cores = 4, chains = 4,
control = list(adapt_delta = 0.99, max_treedepth = 15),
save_pars = save_pars(all=TRUE),
seed = 123,
init_r = 0.1,
file = "models/b0")
summary(b0)
loo0 <- loo(b0)
This works.
loo0m <- loo(b0, moment_match = TRUE)
This gives me the above error.
[data_sample.csv|attachment](upload://lLfCMVWQ9CTPYgzC84sQUq2C1F2.csv) (84.6 KB)
```

- Operating System: Mac OS 10.15.7
- brms Version: 2.15.0 / Development version