How to build a Rstan package? Particularly what's the best way to save compiled model files?

1,018 views
Skip to first unread message

Yaohui Zeng

unread,
Jul 15, 2016, 5:25:22 PM7/15/16
to Stan users mailing list
Dear Rstan users,
 
I wish to build a R package with RSTAN model files. Let's say I have two R files, fun1.R, fun2.R, and two Stan model files, model1.stan and model2.stan. 
Since we don't want to compile stan files each time we call the package, I am wondering what's the best way to save the compiled models in the R package?
 
I am thinking of doing following:
 

(1) save stanmodel object:

m1 <- stan_model(file = 'model1.stan')
save(m1, file = 'm1.RData')
 
Then in R files, I would do:
load(m1.RData)
fit <- sampling(m1, iter = 3, chains = 1)
 
 
 But the problem is, fit is a "stanmodel" object, not a 'stanfit' object from 'stan()' function. I am not sure which one I'd better to use, but I tend to use "stan()" so 'stanfit' object.
 
(2) do similar things as above, but save and load stanfit object:
m1 <- stan(data = data, file = 'model1.stan', chains=0, iter=0)
fit
<- stan(data = data, fit = m1, iter = 3, chains = 1)

But the problem here is, it seems I need to provide 'data' to compile the model file and get 'm1'.

Are above two ways reasonable to do or am I totally off the track? Moreover, what's the typical workflow to pack stan files up as an R package? If you could point out some examples of stan packages, that would be really appreciated. Thank you very much!!

Yaohui







 
 
 
 
 
 

Ben Goodrich

unread,
Jul 15, 2016, 7:39:19 PM7/15/16
to Stan users mailing list
On Friday, July 15, 2016 at 5:25:22 PM UTC-4, Yaohui Zeng wrote:
Moreover, what's the typical workflow to pack stan files up as an R package? If you could point out some examples of stan packages, that would be really appreciated. Thank you very much!!

Call rstan::rstan.package.skeleton(), which was implemented in order to make the rstanarm package possible. Since then, beanz and eggCounts have followed essentially the same route.

Ben

Yaohui Zeng

unread,
Jul 20, 2016, 8:30:09 AM7/20/16
to stan-...@googlegroups.com
​Thank you so much, Ben! I will take a look at it!

--
You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/JlYv7Rt6-zM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+...@googlegroups.com.
To post to this group, send email to stan-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yaohui Zeng

unread,
Aug 18, 2016, 9:50:16 AM8/18/16
to Stan users mailing list
Hi Ben,

I got an errorcalling "rstan.package.skeleton()": Could you let me know how to fix it? Thank you very much!


R 3.2.3> rstan.package.skeleton("BHERM")
Creating directories ...
Creating DESCRIPTION ...
Creating NAMESPACE ...
Creating Read-and-delete-me ...
Saving functions and data ...
Making help files ...
Done.
Further steps are described in './BHERM/Read-and-delete-me'.
Error in download.file("https://raw.githubusercontent.com/stan-dev/rstanarm/master/cleanup",  :
  unsupported URL scheme

Ben Goodrich

unread,
Aug 18, 2016, 10:34:17 AM8/18/16
to Stan users mailing list
On Thursday, August 18, 2016 at 9:50:16 AM UTC-4, Yaohui Zeng wrote:
I got an errorcalling "rstan.package.skeleton()": Could you let me know how to fix it? Thank you very much!

Use a version of R that supports downloading from https URLs. I thought 3.2.3 would do that for all platforms but apparently not in your case. 3.3.x does which you should be using (if not R develop from subversion) when developing packages.

Ben
 

Yaohui Zeng

unread,
Aug 18, 2016, 11:31:34 AM8/18/16
to Stan users mailing list
Thank you very much for your quick response, Ben! I will try it out.

Moreover, I am still not very clear what next steps are for building R package with stan files after calling "rstan.package.skeleton()". It seems it's very complicated based on the discussions I found here: https://groups.google.com/forum/#!topic/stan-users/iBjP4Qq7aP4

So do I have to essentially do similar things in those discussions? Or I just run "R CMD build/check" as usual once I set up the package structure with "rstan.package.skeleton"? It would be very helpful if you could give me a brief description.

Thanks in a ton.

Yaohui

Ben Goodrich

unread,
Aug 18, 2016, 12:04:02 PM8/18/16
to Stan users mailing list
On Thursday, August 18, 2016 at 11:31:34 AM UTC-4, Yaohui Zeng wrote:
Thank you very much for your quick response, Ben! I will try it out.

Moreover, I am still not very clear what next steps are for building R package with stan files after calling "rstan.package.skeleton()". It seems it's very complicated based on the discussions I found here: https://groups.google.com/forum/#!topic/stan-users/iBjP4Qq7aP4

So do I have to essentially do similar things in those discussions? Or I just run "R CMD build/check" as usual once I set up the package structure with "rstan.package.skeleton"? It would be very helpful if you could give me a brief description.

That link is out of date. It is not so complicated. Make cleanup and cleanup.win executable, put your main .stan files in exec/, and put any snippets of Stan code that you use in multiple Stan files in inst/chunks so that your main .stan files in exec/ can #include them. Then R CMD build and / or R CMD INSTALL --preclean will work.

Ben

Yaohui Zeng

unread,
Aug 18, 2016, 3:14:45 PM8/18/16
to Stan users mailing list
Hi Ben,

Again thank you very much for your clarification. Very helpful!

I followed your instruction. But when I tried R CMD build or R CMD INSTALL --preclean. I got following weird error:


installing to /home/zengya6/R/x86_64-pc-linux-gnu-library/3.2/BHERM/libs
** R
** exec
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
Error in .doLoadActions(where, attach) :
  error
in load action .__A__.1 for package BHERM: is(module, "character"): object 'm' not found
Error: loading failed
Execution halted
ERROR
: loading failed
* removing \u2018/home/zengya6/R/x86_64-pc-linux-gnu-library/3.2/BHERM\u2019

I am sure I didn't have any objects called 'm' in my code. Just wondering whether you have any clue about what's going wrong here.

Thanks,
Yaohui

Ben Goodrich

unread,
Aug 18, 2016, 5:52:48 PM8/18/16
to Stan users mailing list
On Thursday, August 18, 2016 at 3:14:45 PM UTC-4, Yaohui Zeng wrote:
I am sure I didn't have any objects called 'm' in my code. Just wondering whether you have any clue about what's going wrong here.

Do you have any files under exec/?

Ben
 

Yaohui Zeng

unread,
Aug 18, 2016, 6:16:22 PM8/18/16
to Stan users mailing list
Yes I do.

Ben Goodrich

unread,
Aug 18, 2016, 6:28:28 PM8/18/16
to Stan users mailing list
On Thursday, August 18, 2016 at 6:16:22 PM UTC-4, Yaohui Zeng wrote:
Yes I do.

What is the whole shell output from R CMD INSTALL --preclean? For some reason, the models are not building.

Ben

Yaohui Zeng

unread,
Aug 18, 2016, 11:56:30 PM8/18/16
to Stan users mailing list

HI Ben,

Now I can successfully run R CMD check, resulting only some warnings and notes. However when I run R CMD build, the error below occurs. Also, I noticed that if I run "R CMD INSTALL --preclean", my NAMESPACE file will be automatically removed. It seems that NAMESPACE is assumed to be automatically generated by roxygen2, which is not my case. I didn't use roxygen2 but just manually edit NAMESPACE and .Rd files.


Yaohuis-MacBook-Pro:BHERM yazeng$ R CMD build BHERM
* checking for file BHERM/DESCRIPTION ... OK
* preparing BHERM’:
* checking DESCRIPTION meta-information ... OK
* cleaning src
* running cleanup
* installing the package to process help pages
     
-----------------------------------
* installing *source* package BHERM ...
ERROR
: a 'NAMESPACE' file is required
* removing ‘/private/var/folders/5r/pt6njcn50jjg0slm3sqq11y80000gn/T/RtmpSt7ewz/Rinst149a1508bca7/BHERM
     
-----------------------------------
ERROR
: package installation failed

To resolve this error, should I just remove the "cleanup" & "cleanup.win" files?

The very previous weird error may due to that I run "rstan.package.skeleton" on Mac but then compile the package on Linux. (Because of some confidential issues, the Linux machine is not connected to Internet so I cannot run "rstan.package.skeleton".) Now I just try all things on Mac and don't see previous error any more.

Best,
Yaohui

Yaohui Zeng

unread,
Aug 19, 2016, 12:23:13 AM8/19/16
to Stan users mailing list

I tried to comment out "cleanup" and "cleanup.win", and then was able to run R CMD build/INSTALL. And everything works fine. Then if I load the built package, I got some errors:

> library(BHERM)
Loading required package: Rcpp
Error in unloadNamespace(package) :
 
namespace Rcpp is imported by httpuv’, scales’, ggrepel’, rstan’, htmltools’, plyr so cannot be unloaded
In addition: Warning message:
package Rcpp was built under R version 3.2.5
Error in library(pkg, character.only = TRUE, logical.return = TRUE, lib.loc = lib.loc,  :
 
Package Rcpp version 0.12.3 cannot be unloaded

I am not sure whether it's due to that I didn't use cleanup or due to other. I am attaching part of my DESCRIPTION and NAMESPACE:

DESCRIPTION:

Depends:
    R
(>= 3.0.2),
   
Rcpp (>= 0.11.0),
    methods
,
    rstan
(>= 2.8.1),
Imports:
    ggplot2
(>= 2.0.0),
    rstan
(>= 2.9.0),
    coda
,
    ggrepel
(>= 0.5.1),
    gridExtra
(>= 2.2.1),
    grid
LinkingTo:
   
StanHeaders (>= 2.9.0),
    rstan
(>= 2.9.0),
    BH
(>= 1.60.0),
   
Rcpp (>= 0.12.0),
   
RcppEigen
RoxygenNote: 5.0.1


NAMESPACE:

export(BHERM)
export(post_analysis)
export(plot_dose_pk_EFF)
export(plot_dose_pk_SAF)
export(plot_predict_EFF)
export(plot_predict_SAF)
export(plot_mean_survival)
export(plot_pred_median)


import(Rcpp)
import(ggplot2)
import(rstan)
import(coda)
import(ggrepel)
import(gridExtra)
import(methods)
import(grid)
useDynLib
(BHERM, .registration = TRUE)

I really appreciate if anyone can spot any errors. Thank you very much! 

Ben Goodrich

unread,
Aug 19, 2016, 11:10:48 AM8/19/16
to Stan users mailing list
On Thursday, August 18, 2016 at 11:56:30 PM UTC-4, Yaohui Zeng wrote:
Now I can successfully run R CMD check, resulting only some warnings and notes. However when I run R CMD build, the error below occurs. Also, I noticed that if I run "R CMD INSTALL --preclean", my NAMESPACE file will be automatically removed. It seems that NAMESPACE is assumed to be automatically generated by roxygen2, which is not my case. I didn't use roxygen2 but just manually edit NAMESPACE and .Rd files.

Keep the cleanup files and write a NAMESPACE file that at least has the line

useDynLib(my_package, .registration = TRUE)

Ben
 

Ben Goodrich

unread,
Aug 19, 2016, 11:23:12 AM8/19/16
to Stan users mailing list
On Friday, August 19, 2016 at 12:23:13 AM UTC-4, Yaohui Zeng wrote:
NAMESPACE:

export(BHERM)
export(post_analysis)
export(plot_dose_pk_EFF)
export(plot_dose_pk_SAF)
export(plot_predict_EFF)
export(plot_predict_SAF)
export(plot_mean_survival)
export(plot_pred_median)


import(Rcpp)
import(ggplot2)
import(rstan)
import(coda)
import(ggrepel)
import(gridExtra)
import(methods)
import(grid)
useDynLib
(BHERM, .registration = TRUE)

I really appreciate if anyone can spot any errors. Thank you very much! 

I am not sure, but you shouldn't need to do an import(foo) for all of these packages. You should just import the specific functions that you need.

Ben

Yaohui Zeng

unread,
Aug 19, 2016, 11:28:06 AM8/19/16
to Stan users mailing list
Hi Ben,

I did have that line is NAMESPACE.

My problem with 'cleanup' is that, if I include 'cleanup' and run R CMD build, I will get an error below. If I don't include 'cleanup', then it works. BTW, I am working on Mac. Would that be the reason? I appreciate if you have any suggestions. Thank you so much!

Yaohuis-MacBook-Pro:BHERM yazeng$ R CMD build BHERM
* checking for file BHERM/DESCRIPTION ... OK
* preparing BHERM’:
* checking DESCRIPTION meta-information ... OK
* cleaning src
* running cleanup
* installing the package to process help pages
     
-----------------------------------
* installing *source* package BHERM ...
ERROR
: a 'NAMESPACE' file is required
* removing ‘/private/var/folders/5r/pt6njcn50jjg0slm3sqq11y80000gn/T/Rtmpt2orWq/Rinst1bf1549b4b9/BHERM

     
-----------------------------------
ERROR
: package installation failed

Ben Goodrich

unread,
Aug 19, 2016, 12:04:50 PM8/19/16
to Stan users mailing list
On Friday, August 19, 2016 at 11:28:06 AM UTC-4, Yaohui Zeng wrote:
I did have that line is NAMESPACE.

My problem with 'cleanup' is that, if I include 'cleanup' and run R CMD build, I will get an error below. If I don't include 'cleanup', then it works. BTW, I am working on Mac. Would that be the reason? I appreciate if you have any suggestions. Thank you so much!

You should just be able to delete this one line from cleanup and cleanup.win

"${R_HOME}/bin/R" --vanilla --slave -e 'roxygen2::roxygenize(clean = TRUE)'

and then it won't modify your existing NAMESPACE.

Ben

Yaohui Zeng

unread,
Aug 19, 2016, 1:52:13 PM8/19/16
to Stan users mailing list
Hi Ben,

I finally got everything worked out!!! Thank you so much for your help along!

One last question though. Shouldn't the package compilation suppose to precompile all Stan model files? Why is the Rstan code recompiled again when calling modeling functions in my package? Below is some console stdout.

What I want is to have all stan source codes recompiled so that the user doesn't need to wait the compilation again and again, which could be annoying.

Any comments? Thank you!

Yaohui

In file included from file1cec30a70d9a.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:42:
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints.hpp:14:17: warning: unused function 'set_zero_all_adjoints' [-Wunused-function]
   
static void set_zero_all_adjoints() {
               
^
In file included from file1cec30a70d9a.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:43:
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints_nested.hpp:17:17: warning: 'static' function 'set_zero_all_adjoints_nested' declared in header file should be declared 'static inline' [-Wunneeded-internal-declaration]
   
static void set_zero_all_adjoints_nested() {
               
^
In file included from file1cec30a70d9a.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:9:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/prim/mat.hpp:55:
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/autocorrelation.hpp:19:14: warning: function 'fft_next_good_size' is not needed and will not be emitted [-Wunneeded-internal-declaration]
      size_t fft_next_good_size
(size_t N) {
             
^
3 warnings generated.
starting worker pid
=7877 on localhost:11526 at 13:41:41.731
starting worker pid
=7885 on localhost:11526 at 13:41:41.869
starting worker pid
=7893 on localhost:11526 at 13:41:42.014
starting worker pid
=7901 on localhost:11526 at 13:41:42.158


SAMPLING FOR MODEL
'SA_STAN_Model_DUAL_SAF_EFF' NOW (CHAIN 1).


Chain 1, Iteration:    1 / 1500 [  0%]  (Warmup)
Chain 1, Iteration:  150 / 1500 [ 10%]  (Warmup)
SAMPLING FOR MODEL
'SA_STAN_Model_DUAL_SAF_EFF' NOW (CHAIN 2).


Chain 2, Iteration:    1 / 1500 [  0%]  (Warmup)
Chain 1, Iteration:  300 / 1500 [ 20%]  (Warmup)
Chain 2, Iteration:  150 / 1500 [ 10%]  (Warmup)
Chain 1, Iteration:  450 / 1500 [ 30%]  (Warmup)
Chain 1, Iteration:  501 / 1500 [ 33%]  (Sampling)
SAMPLING FOR MODEL
'SA_STAN_Model_DUAL_SAF_EFF' NOW (CHAIN 3).

Ben Goodrich

unread,
Aug 19, 2016, 2:35:45 PM8/19/16
to Stan users mailing list
On Friday, August 19, 2016 at 1:52:13 PM UTC-4, Yaohui Zeng wrote:
One last question though. Shouldn't the package compilation suppose to precompile all Stan model files? Why is the Rstan code recompiled again when calling modeling functions in my package? Below is some console stdout.

You are doing it wrong. Call sampling() on one of the elements of the stanmodels list with the models that were compiled at (or before) installation time.

Ben

Gianluca Baio

unread,
Oct 3, 2016, 1:53:47 PM10/3/16
to Stan users mailing list
Hi all,
Full disclaimer: I'm pretty sure I'm missing something blatantly obvious --- so apologies if I'm asking a silly question. 

I too am trying to include pre-compiled Stan models in a package --- the idea is to provide some templates for survival analysis specifically for health economic evaluations. So I've coded up a set of parametric survival models, which I can compile. If I save the DSO files and then load the list of DSOs, all works fine and I'm able to just do
dso = readRDS("Name_of_the_file_to_which_I_ve_saved_the_DSOs")
sampling
(dso[[x]],...)
to estimate a given model, for a given set of (user-specific) data [here dso is the list with the saved DSOs].

I know this is *not* the way to do it, but at least it tells me that the models work and that the code for my package does work too (when doing this very unorthodox workaround...). 

I think I've managed to use
rstan.package.skeleton
to create the relevant structure with the compiled models. But then when I include these into my own package, while it all compiles OK, it fails to run properly (it complains about not finding the model).

What I'm not sure is right in my procedure is that I've created the rstan package skeleton by running a R scripts that actually compiles my 9 pre-specified models. Is that what I was meant to do? May I please ask whether there's anybody who has a more structured example on how you precompile stan models to include in another package?

Many thanks!
Gianluca

Ben Goodrich

unread,
Oct 3, 2016, 2:03:26 PM10/3/16
to Stan users mailing list
On Monday, October 3, 2016 at 1:53:47 PM UTC-4, Gianluca Baio wrote:
I think I've managed to use
rstan.package.skeleton
to create the relevant structure with the compiled models. But then when I include these into my own package, while it all compiles OK, it fails to run properly (it complains about not finding the model).

I would guess that you forgot to do this line from the read-and-delete-me file

"Be sure to add useDynLib(mypackage, .registration = TRUE) to NAMESPACE."

but if you have a link to the package I could investigate.
 
What I'm not sure is right in my procedure is that I've created the rstan package skeleton by running a R scripts that actually compiles my 9 pre-specified models. Is that what I was meant to do? May I please ask whether there's anybody who has a more structured example on how you precompile stan models to include in another package?

That is handled at build time by the configure script and the tools/make_cpp.R file, both of which should have been downloaded to the skeleton directory when you called rstan_package.skeleton(). This is how the rstanarm, beanz, dfpk, and EggCounts R packages do things and possibly others that are not on CRAN and I have forgotten about.

Ben

Gianluca Baio

unread,
Oct 3, 2016, 3:38:47 PM10/3/16
to Stan users mailing list
Thanks, Ben.
I had read this thread before compiling the package and so I did include the line about useDynLib. 

It'd be great if you could spare a moment to have a look --- would the tar file of the compiled package help you do that?

Many thanks!
Gianluca

Ben Goodrich

unread,
Oct 3, 2016, 5:00:17 PM10/3/16
to Stan users mailing list
On Monday, October 3, 2016 at 3:38:47 PM UTC-4, Gianluca Baio wrote:
Thanks, Ben.
I had read this thread before compiling the package and so I did include the line about useDynLib. 

It'd be great if you could spare a moment to have a look --- would the tar file of the compiled package help you do that?

OK

Jonah Gabry

unread,
Oct 3, 2016, 5:05:21 PM10/3/16
to Stan users mailing list
Hey Gianluca, this isn't related to your particular question that Ben is looking into, but also keep an eye out for a set of recommendations/guidelines about developing R packages using Stan that we'll be posting soon (sometime this week probably). 

Jonah

Gianluca Baio

unread,
Oct 4, 2016, 5:06:09 AM10/4/16
to Stan users mailing list
Thank you, Jonah --- will certainly do.

Also, I'm attaching the tar file --- please feel free to play around with it. (But notice we're still developing the package so there are bits of rather inefficient code!).

You can test whether things work with the following example

rm(list=ls())

## These are necessary when sourcing the file with the code for the package
library(flexsurv)
library(INLA)
library(data.table)
library(rstan)
source("survHE.R")

## This is what we'd like to have (and load the dependencies)
library(survHE)

# Reads data in
dat <- read.table("http://www.statistica.it/gianluca/survHE/data.txt",header=TRUE)
# Adds some fictious covariates - for testing
dat$sex <- rbinom(dim(dat)[1],1,.5)
dat$age <- rpois(dim(dat)[1],32)
dat$imd <- cut(runif(dim(dat)[1],0,100),breaks=seq(0,100,20)); levels(dat$imd)=1:5
dat$ethnic <- cut(runif(dim(dat)[1],0,100),breaks=seq(0,100,20)); levels(dat$ethnic)=1:5

## Now you can test whether the survHE functions work
# 1. running MLEs via flexsurv
x1 = fit.models(Surv(time,censored)~as.factor(arm),dat,"weibull")

# 2. running INLA (*NOT NECESSARY* --- but we implement this too, for the survival models that INLA can do)
x2 = fit.models(Surv(time,censored)~as.factor(arm),dat,"weibull","inla")

# 3. running HMC/Stan
x3 = fit.models(Surv(time,censored)~as.factor(arm),dat,"weibull","hmc")


Please let me know if you need anything else and apologies if there's any obvious silly bit! :-)

Many thanks!
Gianluca
survHE_1.0.4.tar.gz

Ben Goodrich

unread,
Oct 4, 2016, 9:40:23 AM10/4/16
to Stan users mailing list
On Tuesday, October 4, 2016 at 5:06:09 AM UTC-4, Gianluca Baio wrote:
You can test whether things work with the following example

The package lacks a R/zzz.R file, which would have been created by rstan_package.skeleton(). Or you can download rstanarm's zzz.R file from

https://raw.githubusercontent.com/stan-dev/rstanarm/master/R/zzz.R

and modify it. Also, although there is a NAMESPACE file, there was no line that says

useDynLib(survHE, .registration = TRUE)

With those two changes,

x3 = fit.models(Surv(time,censored)~as.factor(arm),dat,"weibull","hmc")

runs.

Ben


Gianluca Baio

unread,
Oct 4, 2016, 2:52:13 PM10/4/16
to Stan users mailing list

Thank you, Ben --- this is most helpful!

Gianluca

vj...@nyu.edu

unread,
Oct 15, 2016, 11:43:08 PM10/15/16
to Stan users mailing list
I want to say that I had some similar problems and thought I'd share my experience.

1) When adding a Stan model to an existing package that contains C/C++/Fortran code and uses R's native routine registration, it is necessary to either explicitly register the name of the Rcpp Module boot functions for every model that you wish to compile, or to make sure that dynamic symbol lookup is set to true. Errors on loading the package such as:

Error in .doLoadActions(where, attach) : 
  error in load action .__A__.1 for package PACKAGENAME: is(module, "character"): object 'm' not found

can be traced to this. I think that using dynamic symbol lookup is the default, but turning it off can yield a performance benefit if the package makes frequent use of native routines. To add boot functions, the package needs a C/C++ file with contents similar to:

// if C and not C++, ignore the extern "C" part and replace the casts with C-style ones
extern "C" {
  // declarations matches Modules.cpp, but that file has to be preprocessed first to actually see definition
  extern SEXP _rcpp_module_boot_stan_fit4MODELNAME1_mod();
  extern SEXP _rcpp_module_boot_stan_fit4MODELNAME2_mod();
  
  R_CallMethodDef R_callMethods[] = {
    { "_rcpp_module_stan_fit4MODELNAME1_mod_init",
      reinterpret_cast<DL_FUNC>(&_rcpp_module_stan_fit4MODELNAME1_mod_init),
      0 },
    { "_rcpp_module_stan_fit4MODELNAME2_mod_init",
      reinterpret_cast<DL_FUNC>(&_rcpp_module_stan_fit4MODELNAME2_mod_init),
      0 },
    { "ordinaryNativeRoutine", reinterpret_cast<DL_FUNC>(&ordinaryNativeRoutine), 10 },
    ...
    { NULL, NULL, 0 }
  };
  
  void R_init_PACKAGENAME(DllInfo* info)
  {
    R_registerRoutines(info, NULL, R_callMethods, NULL, NULL);
    R_useDynamicSymbols(info, static_cast<Rboolean>(FALSE));
  }
}

2) The system and user library paths in instances spawned by parallel::makeCluster in the sampling function can be reversed from the parent R session. If you happen to have an old version in the system library path with the one containing the stan model in your user library path (say, release and development versions), sampling can fail with an error such as:

Error in checkForRemoteErrors(val) : 
  NUMCHAINS nodes produced errors; first error: object 'model_MODELNAME' not found

This will only happen with more than one chain, as when there is only one sampling happens in your current R session. I filed an issue on this, but an obvious workaround is to just work with a single version of your package.

Vince
Reply all
Reply to author
Forward
0 new messages