我正在使用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)发布于 2018-09-11 09:50:14
.RNG.name / .RNG.seed / .RNG.state初始值适用于模型(或者更具体地说,模型中的链),而不是在数据块中使用。这意味着(据我所知)在JagesJags4.3中无法在数据块中进行任何随机表示。这是可以为将来版本的JAGS添加的东西,但我担心这是一个低优先级,因为在将数据传递给JAGS之前,总是可以(而且通常更好)模拟R中的数据。
在您的例子中,答案(假设您想使用JAGS)是在模型块而不是数据块中进行模拟:
txtstring <- "
model{
# Likelihood:
for (i in 1:N){
ysim[i] ~ dnorm(eta[i], tausim) # tau is precision (1 / variance)
eta[i] <- muOfClustsim[pop[i]]
}
}
"然后,您的其余代码将按预期运行§。值得注意的是,数据模拟通常比JAGS更适合R,但我假设有一个特定的原因,您希望在本例中使用JAGS .
哑光
§虽然一般不应期望严格的双打平等,例如:
identical(0.1+0.2, 0.3)但是:
abs(0.3 - (0.1+0.2)) < sqrt(.Machine$double.eps)甚至更好:
isTRUE(all.equal(0.1+0.2, 0.3))https://stackoverflow.com/questions/52256845
复制相似问题