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