model{
  for (i in 1:p){
    for (j in 1:A){
      for (k in 1:T){
        dxt[i,j,k]~dbin(q[i,j,k],ext[i,j,k])
        logit(q[i,j,k])<-alpha[i,j]+kappa1[i,k]+kappa2[i,k]*(xbar-x[j])+kappa3[i,k]*(x_posdiff[j])+gamma[i,k-j+A]
      }
    }
  }

  for (i in 1:p){
  for (j in 2:(C-2)){
  gamma[i,j]<-gamma_rest_mat[i,j-1]
  }

  gamma[i,C]<-(sum(cohorts[1:(C-3)]*cohorts_rev[4:C]*gamma_rest_mat[i,]))/(C-1)
  gamma[i,C-1]<-(sum(cohorts[1:(C-3)]*cohorts_rev[3:(C-1)]*gamma_rest_mat[i,]))/(2-C)
  gamma[i,1]<-(sum(cohorts_rev[3:(C-1)]*cohorts_rev[4:C]*gamma_rest_mat[i,]))/((1-C)*(C-2))

  }

  for (i in 1:p){
    for (j in 3:(C-3)){
    prior_mean_gamma[i,j]<-gamma_rest_mat[i,j-1]+rho_gamma*(gamma_rest_mat[i,j-1]-gamma_rest_mat[i,j-2])	
    gamma_rest_mat[i,j]~dmnorm(prior_mean_gamma[i,j],i_sigma2_gamma)
    }
    prior_mean_gamma[i,2]<-gamma_rest_mat[i,1]	
    gamma_rest_mat[i,2]~dmnorm(prior_mean_gamma[i,2],i_sigma2_gamma*(1-rho_gamma^2))
    prior_mean_gamma[i,1]<-0	
    gamma_rest_mat[i,1]~dmnorm(prior_mean_gamma[i,1],0.01*i_sigma2_gamma)
  }

  for (i in 1:p){
  for (j in 2:T){
  kappa1[i,j]<-kappa1_rest_mat[i,j-1]
  }

  kappa1[i,1]<--sum(kappa1_rest_mat[i,])

  }

  for (i in 1:p){
    for (j in 2:(T-1)){
    prior_mean_kappa1[i,j]<-eta1_mean[j+1]+rho1*(kappa1_rest_mat[i,j-1]-eta1_mean[j])	
    kappa1_rest_mat[i,j]~dnorm(prior_mean_kappa1[i,j],i_sigma2_kappa1)
    }
    prior_mean_kappa1[i,1]<-eta1_mean[2]+0 # the 0 should have been rho1*(kappa1[i,1]-eta1_mean[1]) but it does not work because the model could not be uniquely defined	
    kappa1_rest_mat[i,1]~dnorm(prior_mean_kappa1[i,1],i_sigma2_kappa1)
  }

  eta1_mean<-c(matrix_kappa_X%*%eta1)


  for (i in 1:p){
  for (j in 2:T){
  kappa2[i,j]<-kappa2_rest_mat[i,j-1]
  }

  kappa2[i,1]<--sum(kappa2_rest_mat[i,])

  }

  for (i in 1:p){
    for (j in 2:(T-1)){
    prior_mean_kappa2[i,j]<-eta2_mean[j+1]+rho2*(kappa2_rest_mat[i,j-1]-eta2_mean[j])	
    kappa2_rest_mat[i,j]~dnorm(prior_mean_kappa2[i,j],i_sigma2_kappa2)
    }
    prior_mean_kappa2[i,1]<-eta2_mean[2]+0 # the 0 should have been rho2*(kappa2[i,1]-eta2_mean[1]) but it does not work because the model could not be uniquely defined	
    kappa2_rest_mat[i,1]~dnorm(prior_mean_kappa2[i,1],i_sigma2_kappa2)
  }

  eta2_mean<-c(matrix_kappa_X%*%eta2)

  for (i in 1:p){
  for (j in 2:T){
  kappa3[i,j]<-kappa3_rest_mat[i,j-1]
  }

  kappa3[i,1]<--sum(kappa3_rest_mat[i,])

  }

for (i in 1:p){
    for (j in 2:(T-1)){
    prior_mean_kappa3[i,j]<-eta3_mean[j+1]+rho3*(kappa3_rest_mat[i,j-1]-eta3_mean[j])	
    kappa3_rest_mat[i,j]~dnorm(prior_mean_kappa3[i,j],i_sigma2_kappa3)
    }
    prior_mean_kappa3[i,1]<-eta3_mean[2]+0 # the 0 should have been rho3*(kappa3[i,1]-eta3_mean[1]) but it does not work because the model could not be uniquely defined	
    kappa3_rest_mat[i,1]~dnorm(prior_mean_kappa3[i,1],i_sigma2_kappa3)
  }

  eta3_mean<-c(matrix_kappa_X%*%eta3)

  for (i in 1:p){
  for (j in 1:A){
  alpha[i,j]~dnorm(0,0.001)
  }
  }

  eta1~dmnorm(prior_mean_eta1,prior_prec_eta1) #supply prior_prec_eta1<-solve(matrix(c(400,0,0,2),nrow=2));prior_mean_eta1<-c(0,0)
  rho1~dnorm(0,1) T(-1,1)
  sigma2_kappa1<-1/i_sigma2_kappa1
  i_sigma2_kappa1~dgamma(1,0.0001)

  eta2~dmnorm(prior_mean_eta2,prior_prec_eta2) #supply prior_prec_eta2<-solve(matrix(c(400,0,0,2),nrow=2));prior_mean_eta2<-c(0,0)
  rho2~dnorm(0,1) T(-1,1)
  sigma2_kappa2<-1/i_sigma2_kappa2
  i_sigma2_kappa2~dgamma(1,0.0001)

  eta3~dmnorm(prior_mean_eta3,prior_prec_eta3) #supply prior_prec_eta3<-solve(matrix(c(400,0,0,2),nrow=2));prior_mean_eta3<-c(0,0)
  rho3~dnorm(0,1) T(-1,1)
  sigma2_kappa3<-1/i_sigma2_kappa3
  i_sigma2_kappa3~dgamma(1,0.0001)

  rho_gamma~dnorm(0,1) T(-1,1)
  i_sigma2_gamma<-1/sigma2_gamma
  sigma2_gamma<-sigma_gamma^2
  sigma_gamma~dunif(0,1)

}
