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

  for (j in 2:(C-2)){
  gamma[j]<-gamma_rest[j-1]
  }

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

  for (k in 1:h){
    gamma[k+C]<-gamma_forecast_rest[k]
  }  

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

    for (j in 3:h){
    prior_mean_gamma_forecast[j]<-gamma_forecast_rest[j-1]+rho_gamma*(gamma_forecast_rest[j-1]-gamma_forecast_rest[j-2])	
    gamma_forecast_rest[j]~dmnorm(prior_mean_gamma_forecast[j],i_sigma2_gamma)
    }
    prior_mean_gamma_forecast[2]<-gamma_forecast_rest[1]+rho_gamma*(gamma_forecast_rest[1]-gamma[C])  #or swap gamma[C] with (sum(cohorts[1:(C-2)]*gamma_rest))/(1-C)	
    gamma_forecast_rest[2]~dmnorm(prior_mean_gamma_forecast[2],i_sigma2_gamma)
    prior_mean_gamma_forecast[1]<-gamma[C]+rho_gamma*(gamma[C]-gamma[C-1])  #or swap gamma[C-1] with (sum(cohorts_rev[3:C]*gamma_rest))/(1-C)	
    gamma_forecast_rest[1]~dmnorm(prior_mean_gamma_forecast[1],i_sigma2_gamma)

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

  for (k in 1:h){
    kappa1[i,k+T]<-kappa1_forecast_rest_mat[i,k]
  }

  }

  for (i in 1:p){
    for (j in 2:T){
    prior_mean_kappa1[i,j]<-eta1_mean[j]+rho1*(kappa1_rest_mat[i,j-1]-eta1_mean[j-1])	
    kappa1_rest_mat[i,j]~dnorm(prior_mean_kappa1[i,j],i_sigma2_kappa1)
    }
    prior_mean_kappa1[i,1]<-eta1_mean[1]+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)
  }

  for (i in 1:p){
    for (j in 2:h){
    prior_mean_kappa1_forecast[i,j]<-eta1_mean[T+j]+rho1*(kappa1_forecast_rest_mat[i,j-1]-eta1_mean[T+j-1])	
    kappa1_forecast_rest_mat[i,j]~dnorm(prior_mean_kappa1_forecast[i,j],i_sigma2_kappa1)
    }
    prior_mean_kappa1_forecast[i,1]<-eta1_mean[T+1]+rho1*(kappa1_rest_mat[i,T-1]-eta1_mean[T])	
    kappa1_forecast_rest_mat[i,1]~dnorm(prior_mean_kappa1_forecast[i,1],i_sigma2_kappa1)
  }

  eta1_mean<-c(matrix_kappa_X%*%eta1)


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

  for (k in 1:h){
    kappa2[i,k+T]<-kappa2_forecast_rest_mat[i,k]
  }

  }

  for (i in 1:p){
    for (j in 2:T){
    prior_mean_kappa2[i,j]<-eta2_mean[j]+rho2*(kappa2_rest_mat[i,j-1]-eta2_mean[j-1])	
    kappa2_rest_mat[i,j]~dnorm(prior_mean_kappa2[i,j],i_sigma2_kappa2)
    }
    prior_mean_kappa2[i,1]<-eta2_mean[1]+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)
  }

  for (i in 1:p){
    for (j in 2:h){
    prior_mean_kappa2_forecast[i,j]<-eta2_mean[T+j]+rho2*(kappa2_forecast_rest_mat[i,j-1]-eta2_mean[T+j-1])	
    kappa2_forecast_rest_mat[i,j]~dnorm(prior_mean_kappa2_forecast[i,j],i_sigma2_kappa2)
    }
    prior_mean_kappa2_forecast[i,1]<-eta2_mean[T+1]+rho2*(kappa2_rest_mat[i,T-1]-eta2_mean[T])	
    kappa2_forecast_rest_mat[i,1]~dnorm(prior_mean_kappa2_forecast[i,1],i_sigma2_kappa2)
  }

  eta2_mean<-c(matrix_kappa_X%*%eta2)

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

  for (k in 1:h){
    kappa3[i,k+T]<-kappa3_forecast_rest_mat[i,k]
  }

  }

  for (i in 1:p){
    for (j in 2:T){
    prior_mean_kappa3[i,j]<-eta3_mean[j]+rho3*(kappa3_rest_mat[i,j-1]-eta3_mean[j-1])	
    kappa3_rest_mat[i,j]~dnorm(prior_mean_kappa3[i,j],i_sigma2_kappa3)
    }
    prior_mean_kappa3[i,1]<-eta3_mean[1]+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)
  }

  for (i in 1:p){
    for (j in 2:h){
    prior_mean_kappa3_forecast[i,j]<-eta3_mean[T+j]+rho3*(kappa3_forecast_rest_mat[i,j-1]-eta3_mean[T+j-1])	
    kappa3_forecast_rest_mat[i,j]~dnorm(prior_mean_kappa3_forecast[i,j],i_sigma2_kappa3)
    }
    prior_mean_kappa3_forecast[i,1]<-eta3_mean[T+1]+rho3*(kappa3_rest_mat[i,T-1]-eta3_mean[T])	
    kappa3_forecast_rest_mat[i,1]~dnorm(prior_mean_kappa3_forecast[i,1],i_sigma2_kappa3)
  }

  eta3_mean<-c(matrix_kappa_X%*%eta3)

  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_eta2<-solve(matrix(c(400,0,0,2),nrow=2));prior_mean_eta2<-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)

}
