rstan and packages

304 views
Skip to first unread message

Sebastian Weber

unread,
Jul 24, 2015, 2:48:29 AM7/24/15
to Stan users mailing list
Hi!

I am planning to include stan models in R package and have a couple of questions:

- How can I make sure that the binary versions of the package come along with a pre-compiled stan binary?
- Is there a recommended way to do this?
- Should I use the command-line interface or the rstan interface?
- And which options make 100% sure that stan does not output anything (the open_progress=FALSE does not seem to be sufficient)

Many thanks in advance!

Best,
Sebastian

Ben Goodrich

unread,
Jul 24, 2015, 9:35:54 AM7/24/15
to Stan users mailing list, sdw....@gmail.com, sdw....@gmail.com
On Friday, July 24, 2015 at 2:48:29 AM UTC-4, Sebastian Weber wrote:
I am planning to include stan models in R package and have a couple of questions:

- How can I make sure that the binary versions of the package come along with a pre-compiled stan binary?

You have to compile them
 
- Is there a recommended way to do this?

Download the .zip file but put it under inst/include of a copy of an unzipped rstan_2.7.0-1.tar.gz (not under the include/ directory of your installed rstan)

setwd("/path/to/inst/include/example-models")
models
<- dir(getwd(), pattern = "stan$", full.names=TRUE, recursive = TRUE)
library
(paralell)
options(mc.cores = 8) # or however many cores you have available
output
<- mclapply(models, mc.silent = TRUE,
                   FUN
= function(m) try(rstan::stan_model(m, auto_write = TRUE)))

Then rebuild the rstan package via R CMD build

- Should I use the command-line interface or the rstan interface?

rstan
 
- And which options make 100% sure that stan does not output anything (the open_progress=FALSE does not seem to be sufficient)

The open_progress to sampling() or stan() is only relevant if you are running the model. Ostensibly the mc.silent argument suppresses stdout, but it is not as if the "COMPILING MODEL X NOW" statements hurt anything. You could also background the R process doing this.

Ben

Sebastian Weber

unread,
Jul 24, 2015, 9:52:53 AM7/24/15
to Stan users mailing list, goodri...@gmail.com
Hi Ben!

Well, your answer is great - as always; and I will likely use this. Very sweet!

However, I did have in mind to deploy my own library, which uses some stan models behind the scenes to do its work. So from what I get from you, is that I could for example setup a script which compiles my model like

stan_model <- stan("my_internal_library_model.stan", chains=0)

and then do

writeRDS(stan_model, file="inst/data/my_model.rda")

Then in my R library Ioad the rda file the usual way and have immediately a ready to run Stan model, correct? Thanks.

Best,
Sebastian

Ben Goodrich

unread,
Jul 24, 2015, 9:59:00 AM7/24/15
to Stan users mailing list, sdw....@gmail.com, sdw....@gmail.com
On Friday, July 24, 2015 at 9:52:53 AM UTC-4, Sebastian Weber wrote:
However, I did have in mind to deploy my own library, which uses some stan models behind the scenes to do its work. So from what I get from you, is that I could for example setup a script which compiles my model like

stan_model <- stan("my_internal_library_model.stan", chains=0)

The rstan 2.7.0 package looks for a serialized stanmodel object next to the .stan file, not a stanfit object. So, you want to call stan_model() on a .stan file with auto_write = TRUE, and that does a saveRDS().

Ben

Sebastian Weber

unread,
Jul 28, 2015, 7:35:08 AM7/28/15
to Stan users mailing list, goodri...@gmail.com
Hi Ben!

I followed what you wrote, but I am getting

Error in inDL(x, as.logical(local), as.logical(now), ...) : 
  unable to load shared object 

in my 32bit windows machine after a number of models have compiled. Is removing the compiled Stan object enough in order to unload the dll (which I assume each model represents)?

Thanks.

Sebastian

On Friday, July 24, 2015 at 3:35:54 PM UTC+2, Ben Goodrich wrote:

Ben Goodrich

unread,
Jul 28, 2015, 7:42:01 AM7/28/15
to Stan users mailing list, sdw....@gmail.com, sdw....@gmail.com
On Tuesday, July 28, 2015 at 7:35:08 AM UTC-4, Sebastian Weber wrote:
Is removing the compiled Stan object enough in order to unload the dll (which I assume each model represents)?

No, you have to call dyn.unload(x) where x is given by the stanmodel's @dso@.CXXDSOMISC$dso_last_path.

Ben

Sebastian Weber

unread,
Jul 29, 2015, 4:29:52 AM7/29/15
to Stan users mailing list, goodri...@gmail.com
Hi Ben!

I just did what you suggested, but somehow stan_demo still wants to recompile things even though there is a rda file in place for each model. Any idea what went wrong and what I can do?

Thanks!

BTW, now the rstan package 160M large!

Best,
Sebastian

Ben Goodrich

unread,
Jul 29, 2015, 10:49:01 AM7/29/15
to Stan users mailing list, sdw....@gmail.com, sdw....@gmail.com
On Wednesday, July 29, 2015 at 4:29:52 AM UTC-4, Sebastian Weber wrote:
I just did what you suggested, but somehow stan_demo still wants to recompile things even though there is a rda file in place for each model. Any idea what went wrong and what I can do?

It even wants to recompile in a new R session? It is working for me. You could try the GitHub version since I made some changes after 2.7.0 was released. Otherwise, you would probably have to do debug(stan_model) and look at which line of

    if(!file.exists(file.rda) ||
       
(mtime.rda <- file.info(file.rda)$mtime) <  mtime ||
       mtime
.rda < as.POSIXct(packageDescription("rstan")$Date) ||
       mtime
.rda > rstan_load_time ||
       
!is(obj <- readRDS(file.rda), "stanmodel") ||
       
!is_sm_valid(obj) ||
       
!is.null(writeLines(obj@model_code, con = tf <- tempfile())) ||
       md5
!= tools::md5sum(tf)) {
         
# do nothing
   
}
   
else return(invisible(obj))

is unexpectedly failing.

Ben
 

Sebastian Weber

unread,
Jul 29, 2015, 11:43:30 AM7/29/15
to Stan users mailing list, goodri...@gmail.com
Ok, I will see what happens with the debug. Is a 2.7.0-2 planned to go to CRAN?

... non-released stuff is a bit difficult for me to use in this context here.

Sebastian
Reply all
Reply to author
Forward
0 new messages