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 :
9
  • 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. Commented Nov 8, 2019 at 2: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, ...) Commented Nov 8, 2019 at 2: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, ... ``` ? Commented Nov 12, 2019 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)) Commented Nov 13, 2019 at 9:04
  • 1
    Excellent idea. I will update the question Commented Nov 18, 2019 at 20:13

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

sebzee; regarding your comment on the different output - you can set the seed for each chain so that results are reproducible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.