3

每次我使用该jags()函数运行我的 JAGS 模型时,我都会得到非常不同的拟合参数值。但是,我希望其他人重现我的结果。

我尝试添加set.seed(123),但没有帮助。此链接描述了如何使用该run.jags()功能实现我的目标。我想知道如何使用jags(). 谢谢!

以下是我在 R 中的模型:

##------------- read data -------------##
m <- 6 
l <- 3 
node <- read.csv("answer.csv", header = F)
n <- nrow(node)

# values of nodes
## IG
IG <- c(c(0.0, 1.0, 0.0), c(0.0, 0.0, 1.0), c(1.0, 0.0, 0.0), c(1.0, 0.0, 0.0), c(0.0, 1.0, 0.0), c(0.0, 0.0, 1.0))
IG <- matrix(IG, nrow=6, ncol=3, byrow=T)
V_IG <- array(0, dim=c(n, m, l)) 
for (i in 1:n){
  for (j in 1:m){
    for (k in 1:l)
    {
      V_IG[i,j,k] <- IG[j,k] # alternatively, V[i,j,k] <- PTS[j,k]
    }
  }
} 

## PTS
PTS <- c(c(1.0, 0.5, 0.0), c(1.0, 0.0, 0.5), c(1.0, 1.0, 0.0), c(1.0, 0.0, 1.0), c(0.0, 0.5, 1.0), c(0.0, 1.0, 0.5))
PTS <- matrix(PTS, nrow=m, ncol=3, byrow=T)
V_PTS <- array(0, dim=c(n, m, l))
for (i in 1:n){
  for (j in 1:m){
    for (k in 1:l)
    {
      V_PTS[i,j,k] <- PTS[j,k] 
    }
  }
} 

##------------- fit model -------------##
set.seed(123)
data <- list("n", "m", "V_IG", "V_PTS", "node") 
myinits <- list(list(tau = rep(1,n), theta = rep(0.5,n))) 
parameters <- c("tau", "theta")

samples <- jags(data, inits=myinits, parameters,
                model.file ="model.txt", n.chains=1, n.iter=10000, 
                n.burnin=1, n.thin=1, DIC=T)

还有我的模型文件model.txt:

model{
    # data: which node (1, 2, 3) was chosen by each child in each puzzle
    for(i in 1:n) # for each child
    {
        for (j in 1:m) # for each problem
        {
            # node chosen
            node[i,j] ~ dcat(mu[i,j,1:3])
            mu[i,j,1:3] <- exp_v[i,j,1:3] / sum(exp_v[i,j,1:3])
              for (k in 1:3) {
                exp_v[i,j,k] <- exp((V_IG[i,j,k]*theta[i] + V_PTS[i,j,k]*(1-theta[i]))/tau[i])
             }
    }
}
    # priors on tau and theta
    for (i in 1:n)
    {
        tau[i] ~ dgamma(0.001,0.001)
        theta[i] ~ dbeta(1,1)
    }
}
4

2 回答 2

2

这是线性回归的玩具示例。首先是模型:

model{

  a0 ~ dnorm(0, 0.0001)
  a1 ~ dnorm(0, 0.0001)
  tau ~ dgamma(0.001,0.001)

  for (i in 1:100) {

    y[i] ~ dnorm(mu[i], tau)
    mu[i] <- a0 + a1 * x[i]
  }
}

现在我们生成一些数据,并且您使用set.seed函数从多次调用函数中生成相同的结果jags

# make the data and prepare what we need to fit the model
x <- rnorm(100)
y <- 1 + 1.2 * x + rnorm(100)

data <- list("x", "y")
parameters <- c("a0", "a1", "tau")
inits = list(list(a0 = 1, a1=0.5, tau = 1))

# First fit
set.seed(121)
samples <- jags(data, inits, 
  parameters,model.file = "./sov/lin_reg.R",
  n.chains = 1, n.iter = 5000, n.burnin = 1, n.thin = 1)

# second fit
set.seed(121) # with set.seed at same value
samples2 <- jags(data, inits, 
  parameters,model.file = "./sov/lin_reg.R",
  n.chains = 1, n.iter = 5000, n.burnin = 1, n.thin = 1)

如果我们从中提取其中一个参数的平局,samples我们samples2可以看到它们产生了相同的值。

a0_1 <- samples$BUGSoutput$sims.list$a0

a0_2 <- samples2$BUGSoutput$sims.list$a0

head(cbind(a0_1, a0_2))
          [,1]      [,2]
[1,] 1.0392019 1.0392019
[2,] 0.9155636 0.9155636
[3,] 0.9497509 0.9497509
[4,] 1.0706620 1.0706620
[5,] 0.9901852 0.9901852
[6,] 0.9307072 0.9307072
于 2017-08-25T13:57:33.227 回答
2

我知道这是一个较老的问题,但对于使用 jagsUI 包的任何人来说,jags() 函数都有一个用于设置种子的参数“seed = ####”。例如,一个 JAGS 调用可能是:

    np.sim1 <- jags(data = data1, parameters.to.save =  params1, model.file = "mod1_all.txt", 
    n.chains = nc, n.iter = ni, n.burnin = nb, n.thin = nt, seed = 4879)

    summary(np.sim1)
于 2018-05-14T18:46:12.683 回答