JAGS, rjags: "Error in file (modfile," rt "): cannot open connection"

I recently started working with JAGS and calling it inside R. I finally got jags related to R with code

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

and got a way out

Linked to JAGS 3.4.0
Loaded modules: basemod,bugs

I also saved the JAGS model data in a separate file in the BUG format (for example, I was taught).

When I tried to run my data, however, I continue to receive error messages:

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"

and

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

Is there any important step that I am missing?

EDIT: Code, for example, a problem

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:

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

You may not have provided the full path to the model file age_problem.bug. Correcting this path should do the trick, but I usually catmodel for tempfile, as in the following code, which should work well for you.

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)
+3
source

All Articles