I would like to retrieve the stan code of an object "CmdStanMCMC" "CmdStanFit" "R6" on R, using cmdstanr (version 2.28.1), is this possible?
I know that objects of type stanfit using rstan have a slot called stanmodel. However, I did not find anything similar with CmdStanMCMC objects.
I ask this question because I did some tests with a single file that I modified few times, and one of these tests almost ran (but I cannot remember what I did!) and I unfortunately did not keep track of the changes on github… I only saved the results (with the csv files).
If there is no way to get the text version of the model at the origin of my results, then I will test again and learn from this! However, it might be useful to add a mechanism to retrieve the stan code from results!
@yizhang is right, but there still might be a way for you to get the information you seek. Upon compilation, cmdstanr will save the stan code (alongside the executable and an hpp file) in your temporary directory (see tempdir()). It may be that by looking at the stan files in your tempdir, and their creation times, that you can piece together which one was used to run your model. The output CSVs are also by default written to the same location, so for example the stan file that you used should sit immediately previous (in time created) to the CSV files that contain the fit (provided that you didn’t edit and recompile the model in a separate R session between compiling the model in your first R session and fitting the model in your first R session).
@jsocolar@yizhang Many thanks for your answers! I unfortunately restarted my computer in between, so tempdir as been cleaned-up! I should be fine, the modifications where quite obvious and easy so I should found back what I have done. However, maybe it could be a good idea to add the written code in the metadata slot of CmdStanMCMCM objects. Should I open an ‘enhancement’ request one github?
I’ve had the same inquiry before as I was normally only saving the fit objects, and was attempting to compare the code between two older fits and realized I couldn’t. Found an old issue on Github from another person asking the same (Include Stan code in resulting fit object · Issue #175 · stan-dev/cmdstanr · GitHub). I managed to solve my problem for 2.28.1 with the monkey patch I posted in that thread. It seems however a fix has been merged in master, so I assume it will be possible to get the code used for the model fit in a future version.
Many thanks for the trick! If this could be added later to cmdstan, it would be a nice feature (although I actually almost never need to check the code from a fit object, and I guess it is the same for most users). I should have not been so lazy with github! Many thanks to everyone for the help provided