Combining posterior data from multiple chains when saving .csv output from CmdStanR inference object

Hi All,

Some colleagues of mine are looking to plot posteriors from an inference I ran in CmdStanR in Python. I figured the best way to do it would be to generate a csv output. Initially I did the following:

CON_stan_fit_CO2 <- model$sample(data = data_list, seed = 1234, refresh = 100, init = init_theta, iter_sampling = 5000, iter_warmup = 1000, chains = 4, parallel_chains = 4, adapt_delta = 0.95)

CON_stan_fit_CO2_post <- as_tibble(CON_stan_fit_CO2$draws(c("u_M", "a_SD", "a_DS", "a_M", "a_MSC", "k_S_ref", "k_D_ref", "k_M_ref", "Ea_S", "Ea_D", "Ea_M")))
write_csv(CON_stan_fit_CO2_post, "NUTS_results/CON_CO2_NUTS_inference_SCON-C_data_post.csv")

However, after I sent them the csv file, I was told (and saw for myself) that the samples for each parameter were separated into separate columns for each chain (I ran 4, as seen in the above code). Is there a means of collapsing all the posterior samples into one column per chain when extracting the samples from CmdStanR, rather than doing additional manual Tidyverse processing? Thanks for your help!

Was finally able to arrive at the right combination of phrases to google and arrived at a docs page on that answered my question: fit-method-draws: Extract posterior draws in stan-dev/cmdstanr: R Interface to 'CmdStan'

Looks like calling CON_stan_fit_CO2$draws(c("u_M", "a_SD", "a_DS", "a_M", "a_MSC", "k_S_ref", "k_D_ref", "k_M_ref", "Ea_S", "Ea_D", "Ea_M"), format = "draws_df") or CON_stan_fit_CO2$draws(c("u_M", "a_SD", "a_DS", "a_M", "a_MSC", "k_S_ref", "k_D_ref", "k_M_ref", "Ea_S", "Ea_D", "Ea_M"), format = "draws_matrix") will do the trick.

Thanks for coming back with an answer!

1 Like