jags.model出错

时间:2017-06-06 01:03:15

标签: r jags runjags rjags

我是R和JAGS的新手,甚至没有很好的编程经验。

我正在尝试为某些数据设置分层模型,但是我收到了这个错误:

####error in line above:
Error in jags.model(file = "modelControl.txt", data = dataList, inits = 
initsList,  : 
  RUNTIME ERROR:
Cannot insert node into beta1[1:158]. Dimension mismatch

在运行下面的代码结束时。我究竟做错了什么?我该如何避免错误?

#declaration
y=as.numeric(PANSS[treat == 0]) #treat == 0 indicates control group
x=as.numeric(time[treat == 0])
meanYcontrol = mean(PANSS[treat == 0])
sdYcontrol = sd(PANSS[treat == 0])
s = pp[treat==0]

#data list
dataList = list(
  y = y,
  x = x,
  Ntotal = length(y),
  Nsubj = length(y)/6 , #each subject had 6 test moments
  s = s
)
#model
modelString = "
model {
for ( i in 1:Ntotal ) {
y[i] ~ dnorm( beta0[s[i]] + beta1[s[i]] * x[i,1], 1/sigma^2 ) 
}
for ( j in 1:Nsubj ) {
beta0[j] ~ dnorm( beta0mu , 1/(beta0sigma)^2 ) 
beta1[j] ~ dnorm( beta1mu , 1/(beta1sigma)^2 )
}
#vague priors
beta0 ~ dnorm( 0, 1/(10)^5 )  
beta1 ~ dt( 0, 1, 1 ) #Cauchy distribution
beta0sigma ~ dunif( 1.0E-5, 1.0E+5 )
beta1sigma ~ dunif( 1.0E-5, 1.0E+5 )
sigma ~ dunif( 1.0E-5, 1.0E+5 )
nu = nuMinusOne+1
nuMinusOne ~ dexp(1/29)
}
"

 #write model to text file
writeLines(modelString, con="modelControl.txt")

#initialization chains
beta0Init = meanYcontrol
beta1Init = 0
sigmaInit = sdYcontrol
initsList = list(beta0=beta0Init, beta1=beta1Init, sigma=sigmaInit)

#run chains
parameters = c("beta0", "beta1", "sigma") #parameters to be monitored
numSavedSteps = 7500 #number of steps in chain to save
adaptSteps = 1000  #number of steps to tune the samplers
burnInSteps = 500 #number of steps to burn-in the samplers
thinSteps = 1 #number of steps to keep (1=keep every step)
nChains = 3 #number of chains to run
nIter = ceiling(numSavedSteps / nChains) #number of steps per chain

jagsModel = jags.model(file="modelControl.txt", data=dataList, inits = 
initsList, n.chains=nChains, n.adapt=adaptSteps)

1 个答案:

答案 0 :(得分:0)

您的评论意味着您已更改了模型中的一些内容,但问题中的模型定义未更改(或者可能已部分编辑)。以下是我认为你现在正在使用的内容:

model {
for ( i in 1:Ntotal ) {
y[i] ~ dnorm( beta0[s[i]] + beta1[s[i]] * x[i,1], 1/sigma^2 ) 
}
for ( j in 1:Nsubj ) {
beta0[j] ~ dnorm( beta0mu , 1/(beta0sigma)^2 ) 
beta1[j] ~ dnorm( beta1mu , 1/(beta1sigma)^2 )
}
#vague priors
beta0mu ~ dnorm( 0, 1/(10)^5 )  
beta1mu ~ dt( 0, 1, 1 ) #Cauchy distribution
beta0sigma ~ dunif( 1.0E-5, 1.0E+5 )
beta1sigma ~ dunif( 1.0E-5, 1.0E+5 )
sigma ~ dunif( 1.0E-5, 1.0E+5 )
nu = nuMinusOne+1
nuMinusOne ~ dexp(1/29)
}

# In R:
meanYcontrol = mean(PANSS[treat == 0])
sdYcontrol = sd(PANSS[treat == 0])
beta0Init = meanYcontrol
beta1Init = 0
sigmaInit = sdYcontrol
initsList = list(beta0=beta0Init, beta1=beta1Init, sigma=sigmaInit)

所以你给了beta0和beta1的初始值,但我认为它们的目的是beta0mu和beta1mu。可能还有其他错误 - 很难检查,因为目前我们无法运行模型,因为我们没有您的数据。在将来,提供一个最小的可重复示例(包括所需的任何数据等)是一个好主意,因为它将有助于为您生成更快,更完整的答案。

避免这样的错误的一种方法是将#inits#和#data#标签与runjags包一起使用,这有助于使数据和初始值的使用更加明显,例如:

model {
    for ( i in 1:Ntotal ) {   #data# Ntotal
        y[i] ~ dnorm( beta0[s[i]] + beta1[s[i]] * x[i,1], 1/sigma^2 ) 
            #data# y, x, s
    }
    for ( j in 1:Nsubj ) {  #data# Nsubj
        beta0[j] ~ dnorm( beta0mu , 1/(beta0sigma)^2 ) 
        beta1[j] ~ dnorm( beta1mu , 1/(beta1sigma)^2 )
            #inits# beta0mu, beta1mu
    }
    #vague priors
    beta0mu ~ dnorm( 0, 1/(10)^5 )  
    beta1mu ~ dt( 0, 1, 1 ) #Cauchy distribution
    beta0sigma ~ dunif( 1.0E-5, 1.0E+5 )
    beta1sigma ~ dunif( 1.0E-5, 1.0E+5 )
    sigma ~ dunif( 1.0E-5, 1.0E+5 )
        #inits# sigma
    nu = nuMinusOne+1
    nuMinusOne ~ dexp(1/29)
}

[注意我也使用缩进来使模型更容易阅读 - 这通常有助于识别错误,甚至不需要发布模型]

然后在R中你只需要指定的数据和inits在你的工作环境中,然后使用:

results <- runjags::run.jags("modelControl.txt", monitor=...)

希望有所帮助。