How to merge CmdStanModel objects?

What is the best way to merge multiple CmdStanModel objects? The only way I came up with is to extract draws into data frames and rbind those. In that case, I need to manually change chain ID to ensure that each chain has a unique ID number, right? Is there also another way? I think, in rstan, this is implemented as sflist2stanfit(sflist), is that correct? Is there an implementation for objects produced by cmdstanr?

1 Like

If I’m understanding your problem correctly, use the draws array format from the posterior package and abind those.

1 Like

Thanks a lot! But is there a way to combine the fit objects, so I can use loo(fit) on the merged object?

And if I abind the arrays of posterior draws, would this change ID numbers of individual chains? Let say, in each object resulting from mod$sample(), chains have ID numbers 1:20. If I combine two arrays of posterior draws, would it rename the chains so there would be ID numbers 1:40. Or would the chain ID numbers remain the same, so that all chains with ID 1 from the original arrays would seem as a product of one chain in the resulting array? Not sure if that’s a problem, though.

If you have the csvs saved, you can do cmdstanr::read_cmdstan_csv and pass the paths to the various csvs.

2 Likes

I have RDS objects saved using fit$save_object(). Is there a way to merge these?

Thank you! Both abind (or also posterior::bind_draws) arrays of draws along chains and cmdstanr::read_cmdstan_csv work well. In the former case, LOO can be performed using loo(fit$draws("log_lik"), r_eff=relative_eff(fit$draws("log_lik"))) as suggested here:

2 Likes