Easy way of feeding outputs from a model as initial values for a re-run of that model?

I am using cmdstanr, and would like to feed the outputs from a model back into a secondary run for that model.

Unfortunately, it seems like all of the output formats (eg $draws(), $summary etc) are in the “square brackets” format, not the shape that Stan wants for initial values.

I get this error when I try:

Error: 'init' contains entries with parameter names that include square-brackets, which is not permitted. To supply inits for a vector, matrix or array of parameters, create a single entry with the parameter's name in the init list and specify init values for the entire parameter container.

Is there any way to reshape the outputs into the correct format for initial values?

Thanks!

In the Birthdays case study I did

init1 <- sapply(c('intercept','sigma_f1','lengthscale_f1','beta_f1','sigma'),
                function(variable) {as.numeric(subset(odraws1, variable=variable))})
fit1 <- model1$sample(data=standata1, iter_warmup=100, iter_sampling=100,
                      chains=4, parallel_chains=4,
                      init=function() { init1 })

In this case study odraws1 comes from the optimization, but I hope you are able to modify to work with draws from the sampling (unfortunately I don’t have right now time to test the code with draws from sampling)

Thanks. I should be able to get something working with this, although in my case my objects are up to three dimensional arrays.

A standardised function would be useful here.

…actually it looks like there might already be one in the rstan package:

1 Like