3
$\begingroup$

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].

$\endgroup$

2 Answers 2

4
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ whoops. I had edited the model so that, for instance, gamma[i] ~ dnorm(2*gamma[i-1],gamma[i-2]*1000000*tau.g) became gamma[i] ~ dnorm(2*gamma[i-1]-gamma[i-2],1000000*tau.g), so that the variance (or precision) was always greater than 0 and still have the error. The update is an edit in the main post $\endgroup$
    – src471
    Dec 18, 2015 at 3:21
2
$\begingroup$

This part of your code is incorrect:

for (i in 3:17){
gamma[i] ~ dnorm(2*gamma[i-1]-gamma[i-2],1000000*tau.g)
}
gamma[1] ~ dnorm(0,1000000*tau.g)
gamma[2] ~ dnorm(0,1000000*tau.g)

you have only gamma[1] and gamma[2] declared, while you refer to values from 1 to 17. "For-loops" in BUGS and JAGS are not for-loops as known from programming languages, they do not allow you to do operations sequentially. JAGS/BUGS are declarative languages, you use them only to declare structure of your model, rather then the procedure that is to be followed for estimating it. So what for-loop in BUGS/JAGS means is only the declaration that something is applied to cases i in 1:n and not that it is applied sequentially.

Quoting Plummer (2003) himself

3.2 For loops
The existence of “for” loops in the BUGS language is somewhat incongruous, since it is a declarative language, not a procedural one. For loops can be thought of as a kind of macro that succinctly describes repetitive structures. For example, the loop:

for (i in 1:2) {
mu[i] <- alpha + beta*(x[i] - x.bar);
Y[i]   ~ dnorm(mu[i], tau);
}

can be thought of as expanding to

mu[1] <- alpha + beta*(x[1] - x.bar);
Y[1]   ~ dnorm(mu[1], tau);
mu[2] <- alpha + beta*(x[2] - x.bar);
Y[2]   ~ dnorm(mu[2], tau);

Plummer, M. (2003, March). JAGS: A program for analysis of Bayesian graphical models using Gibbs sampling. In Proceedings of the 3rd international workshop on distributed statistical computing (Vol. 124, p. 125). Technische Universit at Wien.

$\endgroup$
3
  • $\begingroup$ I expanded all of the alphas, betas, and gammas and still have the same issue (though now it decides that parent values for beta[2] are invalid. I've edited the question to include the update $\endgroup$
    – src471
    Dec 18, 2015 at 16:40
  • 1
    $\begingroup$ Sorry but code debugging questions are off-topic on this site, please also do not edit your question so to change it -- additional question should be posted as new question. Your question probably better suites to codereview.stackexchange.com where it would be more on-topic. $\endgroup$
    – Tim
    Dec 18, 2015 at 18:03
  • 1
    $\begingroup$ @src471 Did you figure out the error? I am having the same problem. $\endgroup$ Dec 10, 2018 at 20:34

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.