I am trying to model a simple 2×2 contingency table with a multinomial-Dirichlet model.
A snippet of my data z[i,1:4] look like this:
NFF NFS NSF NSS
[1,] 12 52 52 61
[2,] 5 2 2 28
[3,] 4 8 8 159
[4,] 27 34 34 60
[5,] 16 27 27 61
…
[207,]
Here is my model file:
model
{
for (j in 1:K)
{
alpha[j] ~ dbeta(a[j],b[j])
a[j]~ dgamma(0.0001, 0.0001)
b[j]~ dgamma(0.0001, 0.0001)
}
pi ~ ddirch(alpha[1:K])
total ~ dpois(rate)
rate ~ dgamma(0.0001, 0.0001)
for (i in 1:N)
{ z[i, 1:K] ~ dmulti(pi, total)}
}
My RJAGS calls look like this:
z<-as.matrix(infile)
K<-ncol(z)
N<-nrow(z)
data <- list("z" = z, "N" = N,"K"= K)
jags.m <- jags.model(file="multinomialmodeloriginal4.jag", data = data,n.chains = 3, n.adapt = 1000)
Unfortunately, I run into the following error messages:
Compiling model graph
Resolving undeclared variables
Allocating nodes
Graph Size: 226
Initializing model
Deleting model
Error in jags.model(file = "multinomialmodeloriginal5.jag", data = data, :
Error in node z[1,1:4]
Observed node inconsistent with unobserved parents at initialization
Any suggestions on how to address the initialization error is appreciated. I am a newbie!!
Best Answer
A good start would be to actually initialize the parameters :) The problem is that if not initialized correctly,
total
might be a small number, say 5, which is less thanz[1,1:4]
, which means that az[1,1:4]
of12
would be logically impossible, hence the errorObserved node inconsistent with unobserved parents at initialization
.Just initialize
total
to something safe likemax(z) + 1
:Edit:
Ok, in general you get the error Observed node inconsistent with unobserved parents at initialization for the reasons I specified above. But when I look more closely at your code, I'm not sure that any initialization will work. The problem is that for each row of z you know the length, it can't be anything else than the sum of that row. Therefore it it very strange that you have it as a parameter, as it should just be a variable. try creating a vector length[i] with this property and give it as a variable to JAGS.
If you want to keep
total
in your model there is nothing stopping you from doing that. If you have created the vectorlength
that conains the sums of each row inz
then you can add something like the following statement in you for loop