sebzee
sebzee

Reputation: 35

"non-conforming parameters in function :" in simple linear regression using JAGS

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 :

Upvotes: 2

Views: 846

Answers (1)

sebzee
sebzee

Reputation: 35

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.

Upvotes: 1

Related Questions