data {
	Omega[1, 1] <- 1
	Omega[1, 2] <- 0
	Omega[2, 1] <- 0
	Omega[2, 2] <- 1
	
	for(k in 1:N){
		first.loc[k, 1] <- y[Yidx[k], 1]
		first.loc[k, 2] <- y[Yidx[k], 2]
		}
	}
model
{

# priors on process uncertainty
iSigma[1:2, 1:2] ~ dwish(Omega[, ], 2)	
Sigma[1:2, 1:2] <- inverse(iSigma[, ])

gamma[1] ~ dbeta(2, 1.5) 	
dev ~ dbeta(1, 1)			
gamma[2] <- gamma[1] * dev

alpha[1] ~ dbeta(1, 1)  	
alpha[2] ~ dbeta(1, 1)  	

lambda[1] ~ dbeta(1, 1)		
lambda[2] <- 1 - lambda[1]	

for(k in 1:N){
	logpsi[k] ~ dunif(-10, 10)  
	psi[k] <- exp(logpsi[k])
  
  ## estimate first behavioural state for each individual
	b[Xidx[k]] ~ dcat(lambda[])  

	for(j in 1:2){
		x[Xidx[k], j] ~ dt(first.loc[k, j], itau2[Yidx[k], j] * psi[k], nu[Yidx[k], j])	
		}

	## Process equation
	x[(Xidx[k]+1), 1:2] ~ dmnorm(x[Xidx[k], ], iSigma[, ])

	for(t in (Xidx[k]+1):(Xidx[k+1]-2)){
	  phi[t, 1] <- alpha[b[t-1]]
	  phi[t, 2] <- 1 - alpha[b[t-1]]
	  b[t] ~ dcat(phi[t, ])

    x.mn[t, 1:2] <- x[t, 1:2] +  (x[t, 1:2] - x[t-1, 1:2]) * gamma[b[t]]
	  x[t+1, 1:2] ~ dmnorm(x.mn[t, ], iSigma[, ])	
	  }
  
  ## estimate final behavioural state
	zeta[k, 1] <- alpha[b[Xidx[k+1]-2]]
	zeta[k, 2] <- 1 - zeta[k, 1]
	b[Xidx[k+1]-1] ~ dcat(zeta[k, ])
	
	## Measurement equation
  for(i in (Yidx[k]+1):(Yidx[k+1]-2)) {	
    for(j in 1:2) {
      yhat[i, j] <- w[i] * x[idx[i], j] + (1 - w[i]) * x[idx[i+1], j]
         y[i, j] ~  dt(yhat[i, j], itau2[i, j] * psi[k], nu[i, j])
      }
    }	
  }
}

