Babak
Babak

Reputation: 487

read data from a file rjags

My data file looks something like this:

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

Let's say this file is saved as "data.txt".

If I'm working in 'R2OpenBUGS', it allows me to pass the data as a file with no problem:

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

But in JAGS, if I pass data as "data.txt", it says: "data must be a list or environment". What's the problem here? Also, if there is no way around it, is there a way I can read the data as list in R?

My model is:

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)

}

Upvotes: 0

Views: 388

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226577

From the 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)
   }
   ...
}

Upvotes: 3

Related Questions