4

I have some problem with this code in winbugs. The model is sintatically correct and data are loaded, but when I compile, software output is "multiple definitions of node Z". I don't know how to solve the problem.

This is the model:

#BUGS Model
model {
for (i in 1:n){
  for (j in 1:p){
    Y[i , j] ~ dcat ( prob [i , j , 1: M[j]])
    B <- sum(alpha[j])  
  } 
  theta [i] ~ dnorm (0.0 , 1.0)
}
for (i in 1:n){
  for (j in 1:p){
    for (k in 1:M[j]){
      Z <- sum(delta [k ])
      eta [i , j , k] <- 1.7* alpha [j] * (B * (theta [i] - beta [j] ) + Z)
      exp.eta[i , j , k] <- exp( eta[i , j , k])
      psum[ i , j , k] <- sum(eta[i , j , 1:k])
      prob[i , j , k] <- exp.eta[i , j , k] / psum[i , j , 1:M[j]]
    }
  }
}
for (j in 1:p){
  alpha [j] ~ dnorm (0 , pr.alpha) I(0 , )
  for (k in 2:M[j]){
    delta [k] ~ dnorm (0.0 , 1.0)
  }
  for (k in 1:M[j]){
    beta [j] ~ dnorm (0 , pr.beta )
  }
}
delta [1] <- 0.0
pr.alpha <- pow(1 , -2)
pr.beta <- pow(1, -2)
}

#data
list(n=10, p=8)

M[] M[] M[] M[] M[] M[] M[] M[]
2 2 4 2 2 3 4 2 
2 1 1 2 1 2 2 3
1 2 1 3 1 1 4 4
2 1 1 2 1 1 2 4 
3 4 4 3 3 3 1 1 
4 3 4 4 4 4 4 4 
1 1 2 2 1 2 4 4 
2 1 1 3 1 4 2 4 
3 4 1 1 1 2 2 2 
2 2 2 1 4 4 4 4 
END

Thanks to everyone that will answer.

mina
  • 55
  • 7
  • @mima Can you provide all the data, or a sample to be able to make a reproducible example? When I try and compile, I get message that `M` is not defined – guyabel May 27 '14 at 11:31
  • So is `M` a matrix or a vector? In the model you are using `M` as a vector (one long column of numbers), but in your comment it is a matrix? – guyabel May 28 '14 at 08:38
  • this is the model for GPCM(Generalized partial credit model)in item response theory. that "i" is number of subject, "j" is number of item and "M[j]" is number of response category item j. so I have a i*j matrix. – mina May 28 '14 at 09:35
  • possible duplicate of ["Multiple definition of node a" error in Winbugs](http://stackoverflow.com/questions/14509546/multiple-definition-of-node-a-error-in-winbugs) – David Manheim May 27 '15 at 19:25

1 Answers1

3

Your problems lie in defining some nodes multiple times in BUGS loops. For example B is defined np times in the first i and j loop. BUGS will not allow this. You cannot override a node value. You need to either

1) Add some indexes to B, Z, delta[k] and beta[j] to enable BUGS to store simulated values within elements of nodes during the loops. e.g replace B with B[i,j] and Z with Z[i,j,k]

or

2) Move B, Z, delta[k] and beta[j] to loops that only cover the indexes they already have. i.e. B, Z not in a loop as they have no index, delta[k] only in a for(k in 1:...) loop.

The decision depends on what you have in mind for your model and what you want parameters you want to store.

guyabel
  • 8,014
  • 6
  • 57
  • 86
  • B and Z is a constant and I can't add index to them. in above model I put B in for(j in 1:...) loop and I haven't problem with it. but z should be in 2 loop : for(j in 1:...) and for(k in 1:...). and it make problem. – mina May 28 '14 at 09:59
  • thank you for your answer. If you want I can emailed the formula for this model. I'm really confounded. – mina May 28 '14 at 10:35
  • `B` will be a problem, as will `delta` and `beta`, WinBUGS only tells you one error message at a time. – guyabel May 28 '14 at 10:49
  • @mina so from your comment it sounds like `B` and `Z` are single constants and so need to be outside the loops. – guyabel May 28 '14 at 10:51
  • if they were outside the loop, this error shows: "expected variable name". it's because of index for delta and beta. – mina May 28 '14 at 11:22
  • if I change partof model to this: `model{ for (i in 1:n){ for (j in 1:p){ Y[i,j] ~ dcat ( prob[i, j , 1: M[j]]) } theta[i] ~ dnorm (0.0 , 1.0) } delta[1] <- 0.0 for (i in 1:n){ for (j in 1:p){ for (k in 1:M[j]){ B[j]<-sum(alpha[1:p]) Z[j]<-sum(delta[1:k]) eta [i,j,k] <- 1.7*alpha[j]*(B[j]*(theta[i]-beta[j])+Z[j]) exp.eta[i,j,k]<- exp(eta[i,j,k]) psum[ i , j , k] <- sum(eta[i , j , 1:k]) prob[i , j , k] <- exp.eta[i , j , k] / psum[i , j , 1:M[j]] } } }` this error shows: "multiple definitions of node Z[1]" – mina May 28 '14 at 11:58
  • @mina see my answer, `Z` would need to have all indexes for the loops that it sits within, otherwise you are overwriting (not allowed by BUGS) – guyabel May 28 '14 at 12:26
  • @mina. You probably should undo your question edit, and create a new question for the update.... otherwise others that have the same (original) problem and find this question in the future will be lost. – guyabel May 28 '14 at 14:12