title: Index out of range GraphCat.Parent OpenBUGS
body: <p>I am trying to implement a FHMM in R and OpenBUGS, but am now stuck on the following error.</p>
  
  <p>At first OpenBUGS simply gave me the annoying "Something went wrong" error, after renaming Traphandler.ocf to Traphandler_simple.ocf, I was able to see that an "index out of range" error occured in GraphCat.Parents. Even though, I was unable to locate the offending piece of code (know nothing about Fortran and little about MCMC)</p>
  
  <p>The structure of the FHMM I was trying to construct has M=3 sub chains (s[m,n] where m=1,2,3 and n=1,...,100 in my code), each sub chain has K=2 states, and the emissions y is a linear Gaussian of the 3 chains' emissions</p>
  
  <p>Here is my model,</p>
  
  <pre><code># M=3, K=2, N=100
  
  model &lt;- function() {
    for( m in 1:M ) {
      s[m,1] ~ dcat(pi[m,1:K])    # initial states of each sub-chain
    }
    for( m in 1:M ) {
      for( i in 2:N ) {
        s[m,i] ~ dcat(trans[s[m,i-1],1:K,m])    # dim(trans) is c(2,2,3)
      }
    }
    for( m in 1:M ) {
      for( i in 1:N ) {
        mu0[m,i] &lt;- mu[m,s[m,i]]
        sigma[m,i] &lt;- 1/tau[m,s[m,i]]
      }
    }
  
    for( i in 1:N ) {
      mm[i] &lt;- sum(mu0[1:M,i])
      tt[i] &lt;- 1/sum(sigma[1:M,i])
      y[i] ~ dnorm(mm[i],tt[i])
    }
  }
  </code></pre>
  
  
  
  <p>And this is how I set initial values and data in R,</p>
  
  <pre><code># Initial values
  s.B &lt;- matrix(rep(M*N),nrow=M,ncol=N)
  for(i in 1:M) {
    s.B[i,] &lt;- sample(1:K, N, replace=TRUE, rdirichlet(1,rep(1, K)))
  }
  inits &lt;- function() {list(s=s.B)}
  
  # data
  P1 &lt;- array(dim=c(2,2,3))
  for( i in 1:M ) {
    P1[,,i] &lt;- rdirichlet(K,rep(1, K))
  }
  K &lt;- 2,
  M &lt;- 3,
  N &lt;- 100,
  pi &lt;- rdirichlet(M,rep(1, K)),
  trans &lt;- P1,
  mu &lt;- mus + rnorm(M*K,0,1),
  sigma &lt;- sigmas + rnorm(M*K,0,0.3)
  tau &lt;- 1/sigma
  
  # y is the observed sequence of data
  data &lt;- list("y","pi","trans","mu","tau","N","K","M")
  
  # Parameters
  params &lt;- c("s")
  </code></pre>
  
  <p>It seems the error occurred when OpenBUGS was trying to sample s[1,2],
  but I have not a slightly related clue of what that means.
  (stackoverflow does not allow me to post screen capture until I have 10 reputations ...)</p>