3

我最近开始使用 JAGS 并在 R 中调用它。我终于用代码将 jags 链接到 R

install.packages("rjags")
library(rjags)

并得到了输出

Linked to JAGS 3.4.0
Loaded modules: basemod,bugs

我还将 JAGS 模型数据以 BUG 格式保存在单独的文件中(就像我被教导的那样)。

当我尝试运行我的数据时,我不断收到错误消息:

Error in file(modfile, "rt") : cannot open the connection
In addition: Warning message:
In file(modfile, "rt") :
  cannot open file 'age_problem.bug': No such file or directory
Error in jags.model("age_problem.bug", data = list(X = X, N = length(X)),  : 
  Cannot open model file "age_problem.bug"

Error in update(jags, 1000) : object 'jags' not found

我错过了一些关键的步骤吗?

编辑:例如问题的代码

N <- 1000
x <- rnorm(N, 0, 5)

write.table(x,
            file = 'example1.data',
            row.names = FALSE,
            col.names = FALSE)

library('rjags')

jags <- jags.model('example1.bug',
                   data = list('x' = x,
                               'N' = N),
                   n.chains = 4,
                   n.adapt = 100)

update(jags, 1000)

jags.samples(jags,
             c('mu', 'tau'),
             1000)

JAGS 型号:

model {for (i in 1:N) {
        x[i] ~ dnorm(mu, tau)}
    mu ~ dnorm(0, .0001)
    tau <- pow(sigma, -2)
    sigma ~ dunif(0, 100)}
4

1 回答 1

4

您可能没有给出模型文件的完整路径age_problem.bug。更正这条路径应该可以解决问题,但我通常cat建模为 a tempfile,就像在下面的代码中一样,这对你来说应该可以正常工作。

library(rjags)
N <- 1000
x <- rnorm(N, 0, 5)

cat('model {for (i in 1:N) {
  x[i] ~ dnorm(mu, tau)}
  mu ~ dnorm(0, .0001)
  tau <- pow(sigma, -2)
  sigma ~ dunif(0, 100)}', file={f <- tempfile()})

jags <- jags.model(f, data = list(x = x, N = N), n.chains = 4, n.adapt = 100)
update(jags, 1000)
于 2014-02-21T02:37:19.600 回答