rjagsrjags

Attempt to redefine node compilation error in JAGS


I am trying to run the following JAGS code from R. I am just showing some part of the code where the error is occurring.

for(mmm in 1 : p){ 
                 for(jj in 1 : K){
                  vv[jj] ~ dbeta(1,1);
                 }
        pp[1] <- vv[1];
                 for (jjj in 2 : K){
                          pp[jjj] <- vv[jjj] * (1 - vv[jjj-1]) * pp[jjj-1]/vv[jjj-1];
                 }
    }

The error is Attempt to redefine node vv[1] on line 3. I am not sure why the error is occuring. Any help would be appreciated.


Solution

  • You have this 1:K loop nested within your 1:p loop. So when mmm goes from 1 to 2 you are overwriting the values in vv. Without knowing more about the model there are two possible solutions.

    1. Remove these from the nested for loop.
    2. Index the values you have inside of the 1:p loop by mmm.

    Assuming that the second answer is what you need it would look something like this:

    for(mmm in 1 : p){ 
      for(jj in 1 : K){
        vv[mmm, jj] ~ dbeta(1,1);
      }
      pp[mmm,1] <- vv[mmm,1];
      for (jjj in 2 : K){
        pp[mmm,jjj] <- vv[mmm,jjj] * 
        (1 - vv[mmm,jjj-1]) * pp[mmm,jjj-1]/vv[mmm,jjj-1];
      }
    }