Add_criterion applied to a model produced by brm()

Please share your Stan program and accompanying data if possible.


I am trying to use add_criterion() to add “loo” to a model created with brm(), so that I can use loo_compare() to compared models. add_criterion() appears to run (it takes awhile as the model is complex, and the time that the .rds file was modified shows that it was modified), but when I apply loo_compare(), it throws an error stating that the model doesn’t contain a precomputed “loo” criterion. I have used save_pars = save_pars(all = TRUE) when I first constructed the model. The code for producing the model and adding the criterion are both pasted below.

Here’s the code for producing the model:

b5TestStepxVwlxSpkrGndrVwlxLsnrGndrCO_27mar26 ← brm(data = b5Test,
family = bernoulli,
prior = priors,
formula = shRsp \~ recodeStep +
vowel \* recodeSpeaker \* recodeGender \* zConscitientiousness +
vowel \* recodeListenerGender \* zConscitientiousness +
(1 + recodeStep +
vowel:recodeSpeaker +
vowel:recodeGender +
recodeSpeaker:recodeGender +
vowel:recodeSpeaker:recodeGender| uniqueParticipant),
warmup = 1000, iter = 5000, chains = 4, cores = 4, threads = threading(4),
save_pars = save_pars(all = TRUE),
control = list(adapt_delta = 0.99,
max_treedepth = 15),
seed = 101457,
file = "b5TestStepxVwlxSpkrGndrVwlxLsnrGndrCO_27mar26 ",
file_refit = “on_change”)

And here’s the code for adding the criterion.

options(mc.cores = 1)

brms::add_criterion(b5TestStepxVwlxSpkrGndrVwlxLsnrGndrCO_27mar26, criterion = c(“loo”), pointwise = TRUE, overwrite = TRUE)

As shown in the example of add_criterion() documentation you need to do

b5TestStepxVwlxSpkrGndrVwlxLsnrGndrCO_27mar26 <- brms::add_criterion(b5TestStepxVwlxSpkrGndrVwlxLsnrGndrCO_27mar26, criterion = c(“loo”), pointwise = TRUE, overwrite = TRUE)

In R function arguments are almost always pass-by-value, see e.g. Chapter 11 Pass By Value-Reference | Best Coding Practices for R and thus in your code b5TestStepxVwlxSpkrGndrVwlxLsnrGndrCO_27mar26 was not modified

1 Like

Thank you very much! I’m sorry to have troubled you with a question that has such an obvious answer.
Best,
John

No problem, these happen

1 Like