Efficient way to save diagnostics

Assume that I ran the function sample and saved the results in the variable fit. Then, I want to save Stan diagnostics in a text file. To do so, I run the following code:

diag_path <- "./diag.txt"
diagnosis <- fit$cmdstan_diagnose()
 writeLines(diagnosis$stdout, diag_path)

It works, but this code is very slow when the model is large. Any suggestions to do this more efficiently?

Thanks.

I’m not sure what the most efficient way to write that to a text file is. Do you definitely want to save them in a text file or do you just want to save them? cmdstan_diagnose(), as you know, just prints to the console and returns a string, but the same information is available as R objects via other methods. You might know all this already, but just in case, you can get divergences, tree depth, and ebfmi info as a list from fit$diagnostic_summary() and you can get R-hat and effective sample size info from fit$summary(), e.g., fit$summary(variables = NULL, c("rhat", "ess_bulk", "ess_tail")). I think you can also pass .cores to the posterior package (which does the summary work internally) via ..., which can help speed it up.

1 Like

Thanks! I hadn’t realised how expensive it is to compute rhat & ess.

1 Like