(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.
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'.
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!!
--
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.
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 schemeI got an errorcalling "rstan.package.skeleton()": Could you let me know how to fix it? Thank you very much!
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.
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\u2019I 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.
Yes I do.
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> 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 unloadedDepends:
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.1export(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)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.
useDynLib(my_package, .registration = TRUE)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!
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 failedI 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!
"${R_HOME}/bin/R" --vanilla --slave -e 'roxygen2::roxygenize(clean = TRUE)'
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).
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.
dso = readRDS("Name_of_the_file_to_which_I_ve_saved_the_DSOs")
sampling(dso[[x]],...)rstan.package.skeletonI think I've managed to useto 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).rstan.package.skeleton
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?
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?
rm(list=ls())
## These are necessary when sourcing the file with the code for the packagelibrary(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 indat <- read.table("http://www.statistica.it/gianluca/survHE/data.txt",header=TRUE)# Adds some fictious covariates - for testingdat$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:5dat$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")
You can test whether things work with the following example