richarddmorey / BayesFactor

BayesFactor R package for Bayesian data analysis with common statistical models.
https://richarddmorey.github.io/BayesFactor/
131 stars 48 forks source link

set.seed does not work with posterior of t test #134

Closed TobiasHeycke closed 5 years ago

TobiasHeycke commented 5 years ago

When calculating an effect size (and corresponding HDI), the effect size (and HDI borders) cannot be reproduced, even when setting a seed. The following code should demonstrate it:

library("BayesFactor")
set.seed(42)
d <- c()

for(i in 1:20){
tmp <- ttestBF(cars$dist - 30, mu = 0)
post <- posterior(tmp, iterations = 100000)
d <- c(d, median(post[, "delta"]))
}

sd(d) > 0 
richarddmorey commented 5 years ago

What are you expecting the code to return? You only set the seed once. If I do this:


# run the code 10 times with the same seed
replicate(10,
          {
            set.seed(42)
            ttestBF(
              cars$dist - 30,
              mu = 0,
              posterior = TRUE,
              iterations = 1000,
              progress = FALSE
            )
          }, simplify = FALSE) -> samples

## Check to make sure they're all identical to the first
sapply(samples[-1], 
       function(el) 
         identical( el, samples[[1]] )
       )

I get exactly the same thing on each run, because I'm setting the seed before each one. So as far as I can tell, the seed is being set.

richarddmorey commented 5 years ago

I'm going to close this up; we can re-open it if you have a further issue.

TobiasHeycke commented 5 years ago

Thanks Richard, that was my own stupidity inexperience using set.seed. In case others need, just like me, some more information on the working mechanisms of set.seed, they might find this helpful: https://stackoverflow.com/questions/20624698/fixing-set-seed-for-an-entire-session