Workaround: problem with setting seed for RNG with expose_stan_functions


#1

I’ve found a workaround to the problem discussed here:
https://groups.google.com/forum/?fromgroups#!category-topic/stan-users/general/8ayvLs229Ow

The problem is that sometimes you want to reset the RNG for Stan functions called from R, but the seed value passed is only used the first time. The solution I’ve found is to create the model once, but call expose_stan_functions() each time I want to reset the RNG. A unit test file using testthat then looks something like this:

sm <- (code to compute stan_model object)

stan_fcts_from_model <- function(model) {
  e <- new.env(parent=baseenv())
  expose_stan_functions(model, e)
  e
}

...

test_that("Something", {
  ...
  fcts <- stan_fcts_from_model(sm) # RNG will be reset on next line
  x1 <- fcts$some_stan_function_I_defined_rng(..., my_seed)
  ....
  fcts <- stan_fcts_from_model(sm) # RNG will be reset on next line
  x2 <- fcts$some_stan_function_I_defined_rng(..., my_seed)
  ...
}

#2

Is there any way to expose a seed as an argument? The general case compiles the Stan function to one taking a Boost RNG, but that Boost RNG can be constructed from a seed.