save a stan model with rstan

1,693 views
Skip to first unread message

Stéphane Laurent

unread,
Feb 24, 2015, 5:12:33 PM2/24/15
to stan-...@googlegroups.com
Hello, 

From the help of the stan_model function of the rstan package : 

save_dso 

Logical with the default of TRUE: indication of whether the dynamic shared object (DSO) compiled from the C++ code for the model will be saved or not. If TRUE, we can draw samples from the same model in another R session using the saved DSO (i.e., without compiling the C++ code again).


But I don't understand : 

- How to save the DSO in a specified folder ? I don't see any argument to specifiy the folder, and it seems that the DSO file is saved in a temporary file (I see a .so file in the node @dso).
- Once I have saved the DSO file, how can I use it in another session ? 


I have just tried to save the output of stan_model() in a RDS file with saveRDS(), then I have checked I am able to use it in another session by reading it with readRDS(). But maybe it only works until the .so file in the tmp folder will be deleted ?



Ben Goodrich

unread,
Feb 24, 2015, 6:01:31 PM2/24/15
to stan-...@googlegroups.com
That is essentially correct. Basically, use save_dso = FALSE if you want to share the results with someone else on a different computer and use save_DSO = TRUE if you want to then save the resulting R object to your disk for you to use in a subsequent R session (in which case you do need to put the result of saveRDS() into a permanent directory on the disk). In general, DSOs are not compatible across different operating systems, etc.

By the way, there is a replacement for the stan() function in rstan at

http://mc-stan.org/rstan/stan.R

that you can source() and it will automatically save a serialized version of the DSO to your disk in the same directory as your .stan program (or in the temporary directory if you pass a string to model_code). It will also run the chains in parallel in a way that most users would operate on a single laptop. Otherwise, you might need to customize it a bit for your setup, but the code should be straightforward enough to follow and modify.

Ben

Stéphane Laurent

unread,
Feb 25, 2015, 3:59:27 AM2/25/15
to stan-...@googlegroups.com
Thank you. Ok, I was afraid the dso file was in the tmp folder. But today my tmp folder is empty and everything works well by loading my saved model with readRDS.
Reply all
Reply to author
Forward
0 new messages