Hi there! I run Stan’s variational approximation using $variational() in RStudio. My issue is that I cannot find a way to save the number of iterations at the end.

To be more clear:

I run the variational approximation (e.g. fit_vb = model_stan$variational(...)).

A part of the output that is appeared in my Console is:

I need to save the number of iterations (in this case 539). So, I run fit_vb$output() and the same data (as above) are appeared but I cannot find any way to save these data (I used for example as.matrix(fit_vb$output())).

When I run typeof(fit_vb$output()) the result is ‘NULL’, so I guess that’s why I cannot save them.

Do you know any way to save/export the number of iterations?

The content from $output() is printed to the console, so if you want to extract and save the number of iterations you’ll need to capture the output and then parse it:

Another option is to set save_latent_dynamics=TRUE, then use the $save_latent_dynamics_files() method to save as a csv file. The first column of the last row has the number you’re looking for. @andrjohns’ solution to simply parse the printed output seems easier if you only want this single value. The latent dynamics approach seems better if you want to extract a larger amount of the (meta)data from this part of the fit.

(Note that the RNG seed, eval_elbo, inits, and possibly other arguments that aren’t immediately coming to mind may have a big impact on how many iterations the algorithm takes to converge!)