Is using saveRDS and readRDS with a stanfit object a supported usage?


#1

I’ve used saveRDS and readRDS on stanfit objects and found that for most of my use cases, it happens to work for the most part. I haven’t even had to use the trick mentioned in an earlier post, where the attribute stanmodel@dso of a stanfit object is set to new("cxxdso") before saving it with saveRDS. However, there are some methods for which a saved stanfit object doesn’t work, such as unconstrain_pars, and this is apparently considered normal operation and not a bug. This makes me wonder if what I’ve been doing has only worked because of luck.

Should I be using saveRDS with stanfit objects at all, or should I assume that a stanfit object is only guaranteed to work in the same session that it was generated in?

Would a “best practice” be to just extract the information from the stanfit object that I think I’ll need and then save that to files that I know will be portable?


#2

The latter. The stuff that does not work requires that the compiled Stan program be loaded into R’s memory as a dynamic shared object and those do not persist across R sessions. It is the same as if you loaded the draws from CSV files.