Search code examples
rstan

rstan() should not run in #'@example?


In package development, each example requires <5s. However, the pair of stan_model() and rstan::sampling() take long times more than 5s as follows:

    Examples with CPU or elapsed time > 5s
                     user system elapsed
   fit               1.25   0.11   32.47

So I put \donttest{} for each rstan::sampling() in roxygen comments #'@examples

In examples#'@examples, we should not run sampling() or is there any treatment ?


I had tried to create my package based on the code rstan_package_skeleton(path = 'BayesianAAA') when I was taught from you (Thank you !!) but, I do not understand many things about it.

Previously, rstan_package_skeleton(path = 'BayesianAAA') launched the errors in my computer ( but now the error does not occur). So, I made my package without the rstan_package_skeleton(), say BayesianAAA, and in my original making, I put the Model_A.stan,Model_B.stan,Model_C.stan,.... in the inst/extdata and I refer my stan files as follows;

  scr <- system.file("extdata", "Model_A.stan", package="BayesianAAA")

  scr <- rstan::stan_model(scr) 

I have many questions about the code rstan_package_skeleton(path = 'BayesianAAA').

1) The first question is How to include my existing stan files and how to refer my .stan files for the rstan::stan_model() ? According to the page following page, it said that

If we had existing .stan files to include with the package we could use the optional stan_files argument to rstan_package_skeleton to include them.

So, I think I should execute, I am not sure but the following like manner is required;

`rstan_package_skeleton(path = 'BayesianAAA', stan_files = "Model_A.stan" )`.

But I do not know how to write the code for several stan files, say Model_A.stan,Model_B.stan,Model_C.stan in my existing package made without the rstan_package_skeleton(). I do not understand , but the following code is correct ? Since I do not where the files described in the variable stan_files is reflected in the new project created by the rstan_package_skeleton().

`rstan_package_skeleton(path = 'BayesianAAA', stan_files = c("Model_A.stan",`Model_B.stan`,`Model_C.stan` )`.

Here, the another question arise, that is,

2) The second question: Where I execute the code rstan_package_skeleton(path = 'BayesianAAA', stan_files = "Model_A.stan" ) ? I execute it form the R studio console in my existing package project. Is it correct ? And then, the new project arise and it is contained the old existing project. What should I do ?

https://cran.r-project.org/web/packages/rstantools/vignettes/minimal-rstan-package.html

3) I do not quite know about the packages "rstanarm" , but I try to imitate it for my package, but I can not fined any .stan file in it, I am wrong ?

I am sorry for my poor English, and Lack of study about these things. I would be grateful if you could tell me.


Solution

  • You generally should not be writing a package that calls stan_model at runtime, unless like brms or tmbstan you are generating a Stan program at runtime as opposed to writing it statically. There are dozens of packages on CRAN that provide compiled Stan programs basically by following the build process developed for rstanarm, which is facilitated by the rstantools::rstan_package.skeleton function, the step-by-step guide, and the developer guidelines which directly address your question

    CRAN policy permits long installation times but imposes restrictions on the time consumed by examples and unit tests that are much shorter than the time that it takes to compile even a simple Stan program. Thus, it is only possible to adequately test your package if it has pre-compiled Stan programs.

    Even then, it can be difficult to sample from a posterior distribution (adequately) in five seconds, so you often have to use small datasets, one chain, a small number of iterations, etc.

    It is best to pass the names of your Stan programs (which should end in a .stan extension, not use a period otherwise, and only have ASCII letters, numbers, and the underscore in their names) to rstantools::rstan_package_skeleton(). If doing so from RStudio, I would call it while not in an existing project. Then

    During installation, all Stan programs will be compiled and saved in the list stanmodels that can then be used by R function in the package. The rule is that the Stan program compiled from the model code in src/stan_files/foo.stan is stored as list element stanmodels$foo.

    There are dozens of R packages that have Stan programs in their src/stan_files directory (although the locations of the Stan programs are going to move to inst/stan for the next rstantools release) that for the most part just followed the vignettes and did not have to do any additional steps except write more R functions.