2017-08-25 85 views
2

每次使用jags()函數運行我的JAGS模型時,我都會得到非常不同的擬合參數值。但是,我希望其他人重現我的結果。如何使用jags()函數設置隨機種子?

我試圖添加set.seed(123),但它沒有幫助。 This link描述瞭如何使用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) 
    } 
} 

回答

1

下面是線性迴歸的玩具例子。首先,模型:

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