model{
## Variance structure
  for(g in 1:2) { # Sector (public,private) loop for rho
    for(i in 1:M_count){ # Method loop i
      mu_delta[g,i] <- 0
      sd_delta[g,i] ~ dunif(0,10)  # ~ dt(0,1,1)T(0,)
      sigma_delta[i,i,g] <- pow(sd_delta[g,i],2)
    }

    sigma_delta[1,2,g] <- rho[1,g]*sd_delta[g,1]*sd_delta[g,2]
    sigma_delta[1,3,g] <- rho[2,g]*sd_delta[g,1]*sd_delta[g,3]
    sigma_delta[1,4,g] <- rho[3,g]*sd_delta[g,1]*sd_delta[g,4]
    sigma_delta[1,5,g] <- rho[4,g]*sd_delta[g,1]*sd_delta[g,5]
    sigma_delta[2,1,g] <- rho[1,g]*sd_delta[g,1]*sd_delta[g,2]
    sigma_delta[2,3,g] <- rho[5,g]*sd_delta[g,2]*sd_delta[g,3]
    sigma_delta[2,4,g] <- rho[6,g]*sd_delta[g,2]*sd_delta[g,4]
    sigma_delta[2,5,g] <- rho[7,g]*sd_delta[g,2]*sd_delta[g,5]
    sigma_delta[3,1,g] <- rho[2,g]*sd_delta[g,1]*sd_delta[g,3]
    sigma_delta[3,2,g] <- rho[5,g]*sd_delta[g,2]*sd_delta[g,3]
    sigma_delta[3,4,g] <- rho[8,g]*sd_delta[g,3]*sd_delta[g,4]
    sigma_delta[3,5,g] <- rho[9,g]*sd_delta[g,3]*sd_delta[g,5]
    sigma_delta[4,1,g] <- rho[3,g]*sd_delta[g,1]*sd_delta[g,4]
    sigma_delta[4,2,g] <- rho[6,g]*sd_delta[g,2]*sd_delta[g,4]
    sigma_delta[4,3,g] <- rho[8,g]*sd_delta[g,3]*sd_delta[g,4]
    sigma_delta[4,5,g] <- rho[10,g]*sd_delta[g,4]*sd_delta[g,5]
    sigma_delta[5,1,g] <- rho[4,g]*sd_delta[g,1]*sd_delta[g,5]
    sigma_delta[5,2,g] <- rho[7,g]*sd_delta[g,2]*sd_delta[g,5]
    sigma_delta[5,3,g] <- rho[9,g]*sd_delta[g,3]*sd_delta[g,5]
    sigma_delta[5,4,g] <- rho[10,g]*sd_delta[g,4]*sd_delta[g,5]
  } # end G loop

  inv.sigma_delta[1:M_count,1:M_count,1] <- inverse(sigma_delta[,,1])
  inv.sigma_delta[1:M_count,1:M_count,2] <- inverse(sigma_delta[,,2])

## Model Estimates
for(p in 1:P_count) { # province loop matched to C
  for(m in 1:M_count){ # method loop
    for (t in 1:n_years) {
      z[m,p,t] <- alpha_pms[1,m,p] + inprod(B.ik[p,t,],beta.k[1,m,p,])
      r[m,p,t] <- alpha_pms[2,m,p] + inprod(B.ik[p,t,],beta.k[2,m,p,])
    } # end time loop
  } # end M loop
} # end P loop


## Parameter Estimates
  for(s in 1:2){ # sector loop
    for(p in 1:P_count) { # province loop
      for(m in 1:M_count){ # method loop
        alpha_pms[s,m,p] ~ dnorm(alpha_cms[s,m,matchcountry[p]],tau_alpha_pms[s])
        beta.k[s,m,p,kstar[p]] <- 0
        for(j in 1:(kstar[p]-1)){ # before kstar[p]
          beta.k[s,m,p,(kstar[p] - j)] <- beta.k[s,m,p,(kstar[p] - j)+1] - delta.k[s,m,p,(kstar[p] - j)]
        } # end K1 loop
        for(j in (kstar[p]+1):K){ # after kstar[p]
          beta.k[s,m,p,j] <- beta.k[s,m,p,(j-1)] + delta.k[s,m,p,(j-1)]
        } # end K2 loop
      } # end m loop
      for(j in 1:H){
        delta.k[s,c(1:M_count),p,j] ~ dmnorm(mu_delta[s,],inv.sigma_delta[,,s])
      } # end H loop
    } # end P loop
  } # end S loop

## Estimating all the Categories here (including total private)
  for(p in 1:P_count){ # province loop
    for(m in 1:M_count){ # method loop
      for (t in 1:n_years) { # years loop
         P[1,m,p,t] <- 1/(1+exp(-(z[m,p,t]))) # modelling this as before assuming that z[m,p,t] is log(pi_public/pi_private)

         Q[m,p,t] <- 1/(1+exp(-(r[m,p,t]))) # logit-inverse of ratio

         U[m,p,t] <- 1-P[1,m,p,t] # this then gives you the total private sector

         P[2,m,p,t] <- Q[m,p,t]*U[m,p,t] # this is assuming that the logit(P[2,m,p,t]/U[m,p,t]) = r[m,p,t] i.e., we are modelling the ratio of private medical to private

         P[3,m,p,t] <- U[m,p,t] - P[2,m,p,t] # other = private - private medical

         logit.Public[m,p,t] <- logit(P[1,m,p,t]) # For logit-normal data model
         logit.CM[m,p,t] <- logit(P[2,m,p,t]) # for logit normal data model

      }
    }
  }

## Likelihood
  for (k in 1:n_obs) {
    y[k, 1] ~ dnorm(logit.Public[matchmethod[k],matchsubnat[k],matchyears[k]], tau_y[k,1])
    y[k, 2] ~ dnorm(logit.CM[matchmethod[k],matchsubnat[k],matchyears[k]], tau_y[k,2])
    tau_y[k,1:2] <- 1/(se_prop[k,1:2]^2)
    }

## Priors
  for(s in 1:2) { # cross method variance (within a country)
    for(m in 1:M_count){ # method loop
      for(c in 1:C_count) { # country loop
        alpha_cms[s,m,c] ~ dnorm(beta_r[s,m,matchregion[c]],tau_alpha_cms[s])
      }
      for(r in 1:R_count){ # regional
        beta_r[s,m,r] ~ dnorm(beta_world[s,m],tau_beta[s])
      }
       # world intercept
       beta_world[s,m] ~ dnorm(0,0.1)
    }
    tau_alpha_pms[s] <- sigma_alpha_pms[s]^-2
    sigma_alpha_pms[s] ~ dt(0,1,1)T(0,)

    tau_alpha_cms[s] <- sigma_alpha_cms[s]^-2
    sigma_alpha_cms[s] ~ dt(0,1,1)T(0,)

    tau_beta[s] <- sigma_beta[s]^-2
    sigma_beta[s] ~ dt(0,1,1)T(0,)
  }
}
