run BUGS models from rstan

17 views
Skip to first unread message

Ben Goodrich

unread,
Oct 5, 2012, 11:21:47 AM10/5/12
to stan...@googlegroups.com
Mav,

I wrote a R function that tries to run something under src/models. You can improve / include it in rstan if you want or put it on the wiki or something. -- Ben

libary(rstan)

stan_demo
<-
 
function(model = NULL, ...) {
    MODELS_HOME
<- file.path(system.file('include', package = 'rstan'),
                             
"stansrc", "models")
    MODELS
<- dir(MODELS_HOME, pattern = paste0(model, ".stan", "$"),
                  recursive
= TRUE, full.names = TRUE)
   
if(length(MODELS) == 0) {
      stop
("'model' not found; leave 'model' unspecified to see all choices")
   
}
   
else if(length(MODELS) > 1) {
      MODELS
<- select.list(MODELS)
      model
<- sub(".stan$", "", basename(MODELS))
   
}
    MODEL_HOME
<- dirname(MODELS)
    STAN_ENV
<- new.env()
   
if(file.exists(file.path(MODEL_HOME, paste0(model, "_generate_data.R")))) {
      current_wd
<- getwd()
      on
.exit(setwd(current_wd))
      setwd
(MODEL_HOME) # this might not work if MODEL_HOME is not writable
      source
(file.path(MODEL_HOME, paste0(model, "_generate_data.R")),
             
local = STAN_ENV, verbose = FALSE)
   
}
   
if(file.exists(file.path(MODEL_HOME, paste0(model, ".Rdata")))) {
      source
(file.path(MODEL_HOME, paste0(model, ".Rdata")),
             
local = STAN_ENV, verbose = FALSE)
   
}
   
return(stan(MODELS, model_name = model, data = STAN_ENV, ...))
 
}

check
<- stan_demo("rats")



stan_demo.R

Jiqiang Guo

unread,
Oct 5, 2012, 1:40:17 PM10/5/12
to stan...@googlegroups.com
Thanks to Ben.

This would be helpful.  I will include it and point it out in the wiki for the next release.

Jiqiang



--
 
 

Reply all
Reply to author
Forward
0 new messages