JAGS模型中的未知变量错误

时间:2017-01-14 05:33:24

标签: r model bayesian jags

我目前正在尝试在JAGS中开发模型,但遗憾的是我不断收到以下错误:

Error in jags.model(file = "Model.txt", n.adapt = MCMCAdapt) : 
RUNTIME ERROR:
Compilation error on line 15.
Unknown variable precedentCount
Either supply values for this variable with the data
or define it  on the left hand side of a relation.

以下是标题为“Model.txt”的文件:

model {
for (i in 1:citationCount) {

    Z[i] ~ dbern(Z.hat[i])
    Z.hat[i] <- phi(kappa - lambda*pow(x.precedent[newPrecedentID[i]] - x.brief[newBriefID[i]], 2))

}

for (j in 1:briefCount) {

    x.brief[newBriefID[j]] ~ dnorm(0,1)

}

for (k in 1:precedentCount) {

    x.precedent[newPrecedentID[k]] ~ dnorm(0,1)

}

kappa ~ dunif(-10,10)
lambda ~ dunif(-10, 0)

}

我对发生错误的原因感到有些困惑,因为precedentCount已经在R的环境中定义了:

> precedentCount
[1] 650

briefCountcitationCount也是如此,但这些错误并没有产生同样的错误(至少):

> briefCount
[1] 126
> citationCount
[1] 2948

如果有人可以建议我如何解决这个问题,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

您需要将JAGS的所有数据作为参数jags.model(data = list(...))precedentCount出现了,因为这只是第一个错误。

data_list <- list(precedentCount = 650, 
                  briefCount = 126, 
                  citationCount = 2948,
                  newPrecedentID = ...))

jags.model(file = "Model.txt", n.adapt = MCMCAdapt, data = data_list)