2

I am super new to JAGS and Bayesian statistics, and have simply been trying to follow the Chapter 22 on Bayesian statistics in Crawley's 2nd Edition R Book. I copy the code down exactly as it appears in the book for the simple linear model: growth = a + b *tannin, where there are 9 rows of two continuous variables: growth and tannins. The data and packages are this:

install.packages("R2jags")
library(R2jags)

growth <- c(12,10,8,11,6,7,2,3,3)
tannin <- c(0,1,2,3,4,5,6,7,8)
N <- c(1,2,3,4,5,6,7,8,9)
bay.df <- data.frame(growth,tannin,N)

The ASCII file looks like this:

model{
  for(i in 1:N) {
    growth[i] ~ dnorm(mu[i],tau)
    mu[i] <- a+b*tannin[i]
  }
  a ~ dnorm(0.0, 1.0E-4)
  b ~ dnorm(0.0, 1.0E-4)
  sigma <- 1.0/sqrt(tau)
  tau ~ dgamma(1.0E-3, 1.0E-3)
}

But then, when I use this code:

> practicemodel <- jags(data=data.jags,parameters.to.save = c("a","b","tau"),
+                   n.iter=100000, model.file="regression.bugs.txt", n.chains=3)

I get an error message that says:

module glm loaded
Compiling model graph
 Resolving undeclared variables
Deleting model

Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains,  : 
  RUNTIME ERROR:
Non-conforming parameters in function :
user20650
  • 24,654
  • 5
  • 56
  • 91
sebzee
  • 35
  • 5
  • I can't see anything wrong in the jags model code. Perhaps it is how you have included the data? Can you edit your question please to add data and any packages used. – user20650 Nov 08 '19 at 02:06
  • okay, from the error message it looks like the `:` is the problem. It is fine in the model code, so perhaps you have passed the value for `N` incorrectly in the `data.jags` list of data. (From your description I'd of thought it should just be `list(N=9, ...)` – user20650 Nov 08 '19 at 02:16
  • Thanks for all the tips - I tried importing the data from a csv and using read.csv and attach() R to bring up the data, but neither worked. I have edited the post to include the data, hopefully that clarifies something? Could you elaborate on the ":" theory? What would you put after ``` list(N=9, ... ``` ? – sebzee Nov 12 '19 at 23:51
  • you are passing `N` incorrectly. When `N` is used in the `for `loop like this , it is to iterate through the observations and so should be the length of your data i.e. it is a single number not a vector. To be explicit `data.jags = list(growth=bay.df$growth, tannin=bay.df$tannin, N=nrow(bay.df))` – user20650 Nov 13 '19 at 09:04
  • ... or if using your existing data then change your loop to `for(i in N) {` (i.e. no `1:N`) – user20650 Nov 13 '19 at 09:08
  • Yes! That worked! Fantastic, thanks for your help and explanation. – sebzee Nov 14 '19 at 20:30
  • Just as a follow-up to this, I realized when going over the JAGS manual that another solution to this would be changing ```N <- c(1,2,3...)``` to ```N <- 9```. It was unclear in the R Book that N should be specified in this manner. Thanks again for the help! – sebzee Nov 15 '19 at 16:19
  • sebzee ; please consider writing up an answer, so that the code in your question can now compile -- future users may find this useful. – user20650 Nov 15 '19 at 16:34
  • 1
    Excellent idea. I will update the question – sebzee Nov 18 '19 at 20:13

1 Answers1

1

The problem has been solved!

Basically the change is from N <- (1,2...) to N <- 9, but there is one other solution as well, where no N is specified in the beginning. You can specify N inside the data.jags function as the number of rows in the data frame; data.jags = list(growth=bay.df$growth, tannin=bay.df$tannin, N=nrow(bay.df)).

Here is the new code:

# Make the data frame
growth <- c(12,10,8,11,6,7,2,3,3)
tannin <- c(0,1,2,3,4,5,6,7,8)
# CHANGED : This is for the JAGS code to know there are 9 rows of data
N <- 9 code
bay.df <- data.frame(growth,tannin)

library(R2jags)

# Now, write the Bugs model and save it in a text file
sink("regression.bugs.txt") #tell R to put the following into this file
cat("
model{
  for(i in 1:N) {
    growth[i] ~ dnorm(mu[i],tau)
    mu[i] <- a+b*tannin[i]
  }
  a ~ dnorm(0.0, 1.0E-4)
  b ~ dnorm(0.0, 1.0E-4)
  sigma <- 1.0/sqrt(tau)
  tau ~ dgamma(1.0E-3, 1.0E-3)
}
", fill=TRUE)
sink() #tells R to stop putting things into this file.

#tell jags the names of the variables containing the data
data.jags <- list("growth","tannin","N")

# run the JAGS function to produce the function:
practicemodel <- jags(data=data.jags,parameters.to.save = c("a","b","tau"),
                  n.iter=100000, model.file="regression.bugs.txt", n.chains=3)

# inspect the model output. Important to note that the output will
# be different every time because there's a stochastic element to the model
practicemodel 

# plots the information nicely, can visualize the error 
# margin for each parameter and deviance
plot(practicemodel) 


Thanks for the help! I hope this helps others.

user20650
  • 24,654
  • 5
  • 56
  • 91
sebzee
  • 35
  • 5
  • 1
    sebzee; regarding your comment on the different output - you can set the seed for each chain so that results are reproducible. – user20650 Nov 28 '19 at 15:03