Solved – Invalid parent values in JAGS

bayesianjagsr

I read through some other forums with the same error but have not yet been able to figure out my case. When I run the model:

y <- Fairfield$incidents
n <- Fairfield$population
N <- nrow(Fairfield)
jData <- list("y", "n", "N")

model <- function()
{

for(i in 1:N)
{
 y[i] ~ dbin(theta[i], n[i]) #y is incidents, n is population, theta is probability
 logit(theta[i]) <- logit.theta[i] # initialize for MCMC
 logit.theta[i] ~ dnorm(mu, inv.omega.squared) # logit to connect thetas under one hyper-parameter
}
inv.omega.squared <- 1/pow(omega,2)
omega ~ dunif(0,100)
mu ~ dunif(-100,100)
alpha[3] ~ dnorm(2*alpha[2]-alpha[1],1000000*tau.a)
alpha[4] ~ dnorm(2*alpha[3]-alpha[2],1000000*tau.a)
alpha[5] ~ dnorm(2*alpha[4]-alpha[3],1000000*tau.a)
alpha[6] ~ dnorm(2*alpha[5]-alpha[4],1000000*tau.a)
alpha[7] ~ dnorm(2*alpha[6]-alpha[5],1000000*tau.a)
alpha[8] ~ dnorm(2*alpha[7]-alpha[6],1000000*tau.a)
alpha[9] ~ dnorm(2*alpha[8]-alpha[7],1000000*tau.a)
alpha[10] ~ dnorm(2*alpha[9]-alpha[8],1000000*tau.a)
alpha[11] ~ dnorm(2*alpha[10]-alpha[9],1000000*tau.a)
alpha[1] ~ dnorm(0,1000000*tau.a)
alpha[2] ~ dnorm(0,1000000*tau.a)
beta[3] ~ dnorm(2*beta[2]-beta[1],1000000*tau.b)
beta[4] ~ dnorm(2*beta[3]-beta[2],1000000*tau.b)
beta[5] ~ dnorm(2*beta[4]-beta[3],1000000*tau.b)
beta[6] ~ dnorm(2*beta[5]-beta[4],1000000*tau.b)
beta[7] ~ dnorm(2*beta[6]-beta[5],1000000*tau.b)
beta[1] ~ dnorm(0,1000000*tau.b)
beta[2] ~ dnorm(0,1000000*tau.b)
gamma[3] ~ dnorm(2*gamma[2]-gamma[1],1000000*tau.g)
gamma[4] ~ dnorm(2*gamma[3]-gamma[2],1000000*tau.g)
gamma[5] ~ dnorm(2*gamma[4]-gamma[3],1000000*tau.g)
gamma[6] ~ dnorm(2*gamma[5]-gamma[4],1000000*tau.g)
gamma[7] ~ dnorm(2*gamma[6]-gamma[5],1000000*tau.g)
gamma[8] ~ dnorm(2*gamma[7]-gamma[6],1000000*tau.g)
gamma[9] ~ dnorm(2*gamma[8]-gamma[7],1000000*tau.g)
gamma[10] ~ dnorm(2*gamma[9]-gamma[8],1000000*tau.g)
gamma[11] ~ dnorm(2*gamma[10]-gamma[9],1000000*tau.g)
gamma[12] ~ dnorm(2*gamma[11]-gamma[10],1000000*tau.g)
gamma[13] ~ dnorm(2*gamma[12]-gamma[11],1000000*tau.g)
gamma[14] ~ dnorm(2*gamma[13]-gamma[12],1000000*tau.g)
gamma[15] ~ dnorm(2*gamma[14]-gamma[13],1000000*tau.g)
gamma[16] ~ dnorm(2*gamma[15]-gamma[14],1000000*tau.g)
gamma[17] ~ dnorm(2*gamma[16]-gamma[15],1000000*tau.g)
gamma[1] ~ dnorm(0,1000000*tau.g)
gamma[2] ~ dnorm(0,1000000*tau.g)

tau.a ~ dgamma(.001,.001)
tau.b ~ dgamma(.001,.001)
tau.g ~ dgamma(.001,.001)
}

require(R2jags); invisible(runif(1))
fit <- jags(model=model, param=c("n","theta","mu","omega","alpha",
"beta","gamma","tau.a","tau.b","tau.g"), 
data=jData, n.iter=10000, n.thin=1)

I get the error:

Error: Error in node gamma[2]
Invalid parent values

I thought the issue might be in my tau's, as other answers have involved either accidentally having 0 as a gamma parameter or landing negative values for variance in normal distributions, but I can't figure out the issue with mine. Any help is greatly appreciated, I'm not great with JAGS (or Bayesian methods in general).

***EDIT, updated the variance (or precision, technically) in the models and still get the error.
***EDIT 2, removed the loop and expanded, still get invalid parent error with beta[2].

Best Answer

This is just a guess, but one of the distributions might be receiving invalid values.

For example you have the line:

 gamma[i] ~ dnorm(2*gamma[i-1],gamma[i-2]*1000000*tau.g)

The variance (or precision as is the case in JAGS) of a normal distribution must be greater than zero. So if you are getting a negative number ever in gamma[i-2]*1000000*tau.g then you're going to get an error. Given that gamma is sampled from a normal distribution, it seems likely that you might get a negative variance.

So for example, if you have a variance parameter like gamma, you need to make sure that it respects the requirements of the distribution. For example, this is why we often use a gamma distribution or a uniform distribution with lower limit equal to zero when sampling variance parameters. It ensures that you don't get negative variances.

Related Question