Search code examples
bayesianpoissonwinbugswinbugs14

"Multiple definitions of node mu[1,2]" error in WinBUGS


I am trying to solve multivariate regression with multiple dependent variables on Winbugs. But I am getting errors during compilations. I tried to solve based on solutions to the same problem but was unsuccessful. Any help will be highly appreciated.

model {
for(i in 1:n) 
{ for(k in 1:J)
 {                  y[i,k]~ dpois(mu[i,])
                    log(mu[i,1]) <- beta1[1]*x1[i] + beta2[1]*x2[i] + b[,1]
                    log(mu[i,2]) <- beta1[2]*x1[i] + beta2[2]*x2[i] + b[,2]
       }}    

#  PRIORS
     for (i in 1:n) { 
      for(k in 1:J)  {
      b[i,k] <- 1
              }} 
# Scale Matrix
     for(i in 1:J)
     {
     for (j in 1:J) 
      {  
       R[i,j] <- equals(i,j)
      }}
    for (j in 1:J) {beta1[j]~ dmnorm(zero[], B[,])
       beta2[j]~ dmnorm(zero[], B[,]) }
    for(i in 1:J)
     { 
         for (j in 1:J) 
       {  B[i,j] <- 0.01*equals(i,j)
      }}
    for (i in 1:J) { zero[i] <- 0}
    }


#DATA 
list(n=3, J=2)


#DATA
y[ ,1]  x1[]    x2[]    y[,2]   
   0       9.91     8.34     1               
  3    10.48    10.14    79          
 0     10.31    9.42     40

Solution

  • The error is because you have mu nested within two for loops. Therefore, you are filling row i J times which is not possible. What you have is:

    for(i in 1:n){
    for(k in 1:J){
                 y[i,k]~ dpois(mu[i,])
                    log(mu[i,1]) <- beta1[1]*x1[i] + beta2[1]*x2[i] + b[,1]
                    log(mu[i,2]) <- beta1[2]*x1[i] + beta2[2]*x2[i] + b[,2]
       }}
    

    What it looks like it should be is:

    for(i in 1:n){
                    log(mu[i,1]) <- beta1[1]*x1[i] + beta2[1]*x2[i] + b[,1]
                    log(mu[i,2]) <- beta1[2]*x1[i] + beta2[2]*x2[i] + b[,2]
    for(k in 1:J){
                 y[i,k]~ dpois(mu[i,])
    
       }}
    

    This way you are not supplying multiple definitions to each cell in mu