Cmdstan_summary() as a matrix

Hi!

I am using cmdstan in R to draw sample from a posterior distribution. Once the sampling is completed, I would like to use some of the output of cmdstan_summary() for further process (i.e. to compute the MCSE to StdDev ratio). However, I cannot isolate the “table” of the cmdstan_summary(). Is there any was I could save only that as a matrix or any?

See Compute a summary table of estimates and diagnostics — fit-method-summary • cmdstanr

Thanks, avatar! Unfortunately, this is not what I am looking for. The summary()/print() extensions only return some summary statistics, while the cmdstan_summary() offers more information - specifically I wish to isolate the MCSE.

Ideally, I am interested to obtain the output of cmdstan_summary() in a matrix form, where rows are the variables, and columns are the statistics. This would be useful to use these for further analysis, or pass them into tables for presentation, etc.

Are you aware of any such ability of stan?

I guess you missed that documentation of summary() states that it uses posterior::summarise_draws() which supports all possible diagnostics including MCSE. See Summaries of draws objects — draws_summary • posterior and List of available convergence diagnostics — diagnostics • posterior. See, for example, case study Bayesian workflow book - Digits which illustrates use of summarise_draws to display tables of MCSEs.

1 Like

Thanks, @avehtari!

I dived into the files and find a way to get what I was looking for. I will post the solution here, just for other if needed:

data.frame(summarise_draws(sample_draws,mean, mcse = mcse_mean, sd, median,
quantile2,rhat,ess_basic,ess_bulk,ess_tail))

Looking on the links of summary statistics and/or convergence diagnostics, one could edit what stats to include:

2 Likes

Great that you find what you need!

Adding, that those who are happy with the default tibble output (which is a data.frame with improved behaviour) can drop the data.frame part (but I understand if you prefer base data.frame)