Rstan: can I save a compiled model?

2,029 views
Skip to first unread message

Adam Kiezun

unread,
Feb 13, 2013, 3:59:48 PM2/13/13
to stan-...@googlegroups.com
Hi,
In RStan 1.1.1, after a call to stan_model, can I store the compiled model for faster retrieval next time?

./adam

Andrew Gelman

unread,
Feb 13, 2013, 4:33:05 PM2/13/13
to stan-...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Adam Kiezun

unread,
Feb 13, 2013, 11:34:57 PM2/13/13
to stan-...@googlegroups.com, gel...@stat.columbia.edu
Thank you for your response. 
However, I'm looking for an example how to store a compiled, unfitted model (ie before using data, just the model itself). I don't see such an example there.

./adam

Ben Goodrich

unread,
Feb 14, 2013, 1:58:30 AM2/14/13
to stan-...@googlegroups.com
You can call stan() with zero chains or zero iterations. It will return a stanfit object and just say that no sampling was done.

Ben

Jiqiang Guo

unread,
Feb 14, 2013, 10:30:56 AM2/14/13
to stan-...@googlegroups.com
What is the problem you have with saving it?  It should work. 

This is an example I have tried.  

$ more a.stan
parameters {
  real y;

model {
  y ~ normal(0, 1);
}  

 
$ R -q -f a.R 
> library(rstan)
Loading required package: Rcpp
Loading required package: inline
Loading required package: stats4
rstan (Version 1.1.1, packaged: 2013-02-10 02:59:50 UTC, GitRev: 95e6d90e7aa7)
> sm <- stan_model(file = 'a.stan')

TRANSLATING MODEL 'a' FROM Stan CODE TO C++ CODE NOW.
COMPILING THE C++ CODE FOR MODEL 'a' NOW.
> save('sm', file = 'sm.RData')

$ ls -lh sm.RData 
-rw-r--r--  1 jq  staff   744K Feb 14 10:28 sm.RData

$ R -q -f b.R 
> library(rstan)
Loading required package: Rcpp
Loading required package: inline
Loading required package: stats4
rstan (Version 1.1.1, packaged: 2013-02-10 02:59:50 UTC, GitRev: 95e6d90e7aa7)
> load("sm.RData")
> fit <- sampling(sm, iter = 3, chains = 1)
SAMPLING FOR MODEL 'a' NOW (CHAIN 1).
Iteration: 1 / 3 [ 33%]  (Adapting)
Iteration: 2 / 3 [ 66%]  (Sampling)
Iteration: 3 / 3 [100%]  (Sampling)

Jiqiang 




On Feb 13, 2013, at 11:49 PM, Adam Kiezun wrote:

I see. I was actually using something simpler:

sm <- stan_model(file = 'model.stan')
fit1 <- sampling(sm, data = my.data1, ....)
fit2 <- sampling(sm, data = my.data2, ....)

etc.
the question is then, can I save the 'sm' variable (ie the compiled model) somehow and then load it already compiled next time? (i tried save followed by load, but without luck


Adam Kiezun

unread,
Feb 14, 2013, 1:08:59 PM2/14/13
to stan-...@googlegroups.com
thanks. weird, now it does work for me. I'm not sure what I mistyped before.

./adam
Reply all
Reply to author
Forward
0 new messages