从文件 rjags 中读取数据

read data from a file rjags

我的数据文件看起来像这样:

list(y=structure(.Data=c(26, 228, 31, ...)), .Dim=c(413,9))

假设此文件另存为 "data.txt"。

如果我在 'R2OpenBUGS' 工作,它允许我毫无问题地将数据作为文件传递:

mcmc <- bugs(data = "data.txt", inits=...)

但在 JAGS 中,如果我将数据作为 "data.txt" 传递,它会显示:"data must be a list or environment"。这里有什么问题?另外,如果没有办法解决它,有没有办法可以读取 R 中的列表数据?

我的模型是:

model {
for (i in 1:413) {
    for (j in 1:9) {
        logy[i,j] <- log(y[i,j])
        logy[i,j] ~ dnorm(m[i], s)
    }
}

# priors
for (i in 1:413) {
    m[i] ~ dgamma(0.001, 0.001)
}

s ~ dgamma(0.001, 0.001)

}

来自JAGS user manual

7.0.4 Data transformations

JAGS allows data transformations, but the syntax is different from BUGS. BUGS allows you to put a stochastic node twice on the left hand side of a relation, as in this example taken from the manual

for (i in 1:N) {
   z[i] <- sqrt(y[i])
   z[i] ~ dnorm(mu, tau)
}

This is forbidden in JAGS. You must put data transformations in a separate block of relations preceded by the keyword data:

data {
   for (i in 1:N) {
     z[i] <- sqrt(y[i])
   }
}
model {
   for (i in 1:N) {
      z[i] ~ dnorm(mu, tau)
   }
   ...
}