1

我是 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)
4

1 回答 1

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 中,您只需要指定在您的工作环境中的数据和初始化,然后使用:

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

希望有帮助。

于 2017-06-06T09:21:09.293 回答