0

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)

}
Babak
  • 497
  • 1
  • 7
  • 15
  • 1
    you could try reading it in and assigning it . `dat <- dget("data.txt")` . then pass this to data= statement – user20650 Apr 02 '17 at 13:34
  • Thanks! I didn't know about 'dget'. I'm not sure if its working right though. Because now I get another error: "Attempt to redefine node logy[1,1]". An I think my model is fine, because its was working in R2OpenBUGS – Babak Apr 02 '17 at 13:46
  • Ah actually, why do you have two nodes called logy ? – user20650 Apr 02 '17 at 13:51
  • I'm basically assuming in my model that the log of my data is normally distributed. So in the first line I get the log, and in the 2nd line I say its normally distributed. – Babak Apr 02 '17 at 13:53

1 Answers1

3

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)
   }
   ...
}
Community
  • 1
  • 1
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Thanks! another quick question. How do you initialise multiple chains in JAGS. I couldn't find an example of this in the user manual. When I do "inits=init1", it works fine. But when I do "inits=c(init1, init2)", it says: "Duplicated initial values for variable(s): m, s". Any ideas? (promise this is my last question! ) – Babak Apr 02 '17 at 14:22
  • use `list()` instead of `c` ? – Ben Bolker Apr 02 '17 at 18:11