rasmusab / bayesian_first_aid

Inside every classical test there is a Bayesian model trying to get out.
326 stars 40 forks source link

Setting a Seed to Get Reproducible Results #18

Closed tinyheero closed 8 years ago

tinyheero commented 8 years ago

Hi,

I was wondering whether it is possible to set a seed that generates reproducible results? I noticed that when running tests, I get slightly different results each time.

My understanding is that the package calls rjags. So the seed would have to be set at that step. Based on the rjags manual, it should be possible to set a RNG (Random number generators) for the inits parameter in the jags.model function.

Thanks,

drbenvincent commented 8 years ago

It is possible. I implemented this for matjags (the matlab interface to jags). So if rjags doesn't already offer setting the random seed, then you can implement it relatively simply. Check the lines 195-209 for the gist of how to do it https://github.com/drbenvincent/matjags/commit/b1e4dd60437daa324db04d5ae992c1d76ff3bdad

rasmusab commented 8 years ago

All bayes.* functions should now respect the set.seed() function in R. (This is done by using runif to set the seed). That is, you should be able to do:

set.seed(123)
samples1 <- as.data.frame(
    bayes.binom.test(6, 11, n.iter=100))
set.seed(123)
samples2 <- as.data.frame(
  bayes.binom.test(6, 11, n.iter=100))
# Now this should be true
all(samples1$theta == samples2$theta)

Please try this out by installing the latest version from GitHub. :)

Relevant commit: d80c0fded797cff623a5ec42fb2ad8ffbec8b441

mmkamso commented 6 years ago

Hi, Please so can you set seed when using the "gemtc" package in running a network meta-analysis? As am trying to get a reproducible result but get different result each time. Thanks