我正在使用 runjags 从正态分布中采样一些数据。对于我用于模拟的参数,我没有任何先验信息。似乎 runjages 不使用参数来修复种子:list(".RNG.name"="base::Super-Duper", ".RNG.seed"=1)
. 我将参数更改为,list(muOfClustsim=rep(1, npop), ".RNG.name"="base::Super-Duper", ".RNG.seed"=1)
但它也不起作用。有没有办法在 runjags 中修复此类模型的种子?
这是一个最小的可重现示例:
library(runjags)
npop=3
nrep=10
sdpop=7
sigma=5
seed=4
set.seed(seed)
N = npop*nrep # nb of observations
## Population identity of each individual used to sample genotypes but not used for common garden test
pop <- rep(1:npop, each=nrep)
muOfClustsim <- rnorm(npop, 0, sdpop) # vector of population means
(tausim <- 1/(sigma*sigma)) # precision of random individual error
# parameters are treated as data for the simulation step
data <- list(N=N, pop=pop, muOfClustsim=muOfClustsim, tausim=tausim)
## JAG model
txtstring <- "
data{
# Likelihood:
for (i in 1:N){
ysim[i] ~ dnorm(eta[i], tausim) # tau is precision (1 / variance)
eta[i] <- muOfClustsim[pop[i]]
}
}
model{
fake <- 0
}
"
## Initial values with seed for reproducibility
initssim <- list(".RNG.name"="base::Super-Duper", ".RNG.seed"=1)
##initssim <- list(muOfClustsim=rep(1, npop), ".RNG.name"="base::Super-Duper", ".RNG.seed"=1)
## Simulate with jags
set.seed(seed)
out <- run.jags(txtstring, data = data, monitor=c("ysim"), sample=1, n.chains=1, inits=initssim, summarise=FALSE)
## reformat the outputs
(ysim1 <- coda::as.mcmc(out)[1:N])
set.seed(seed)
out <- run.jags(txtstring, data = data, monitor=c("ysim"), sample=1, n.chains=1, inits=initssim, summarise=FALSE)
## reformat the outputs
(ysim2 <- coda::as.mcmc(out)[1:N])
identical(ysim1, ysim2)