# Script generated by the UniPV WinBUGS converter
# 'WinBUGSConverter' copyright (c) BMS - University of Pavia (2016)
# Converter Version: 1.0.0
# Source: PharmML 0.8.1
# Target: WINBUGS 1.4.3
# Run ID: run_id_0
# Model: Generated from MDL. MOG ID: example331
# File: example3321
# Dated: Mon Aug 01 11:43:36 CEST 2016

model {
  for (ind_subj in 1:N_subj) {
    for (ind_t in 1:N_t[ind_subj]) {
      C[ind_subj,ind_t] <- ((DOSE[ind_subj,1] / V[ind_subj]) * (exp(((-(K[ind_subj])) * grid[ind_subj,ind_t]))))
      Y_mean[ind_subj,ind_t] <- (C[ind_subj,ind_t] + 0)
      Y[ind_subj,ind_t] ~ dnorm(Y_mean[ind_subj,ind_t], Y_prec[ind_subj])
      Y_res[ind_subj,ind_t] <- Y[ind_subj,ind_t]-Y_mean[ind_subj,ind_t]
    }
    Y_prec[ind_subj] <- 1 / ((pow((SD_ADD[ind_subj]),2))*1)
    K[ind_subj] <- (exp(lP[1]))
    V[ind_subj] <- (exp(lP[2]))
    SD_ADD[ind_subj] <- (1 / (sqrt(TAU[ind_subj])))
    eta_T[ind_subj] ~ dnorm(0, eta_T_prec)
    log(TAU[ind_subj])<- log(POP_T) + eta_T[ind_subj]
  }
  lP[1:2] ~ dmnorm(lP_mean[],lP_prec[,])
  lP_mean[1] <- lPOP_P[1]
  lP_mean[2] <- lPOP_P[2]
  lP_prec[1:2,1:2] <- inverse(OMEGA_P[,])
  eta_T_prec <- (1/(OMEGA_T))
  MU_POP_K <- 0.1
  MU_POP_V <- 8
  SIGMA_POP_P[1,1] <- 1
  SIGMA_POP_P[1,2] <- 0.1
  SIGMA_POP_P[2,1] <- 0.1
  SIGMA_POP_P[2,2] <- 1
  a_OMEGA_T <- 0.3
  b_OMEGA_T <- 0.3
  R[1,1] <- 1
  R[1,2] <- 0.1
  R[2,1] <- 0.1
  R[2,2] <- 1
  rho <- 2
  a_POP_T <- 0.1
  b_POP_T <- 0.1
  lMU_POP_K <- (log(MU_POP_K))
  lMU_POP_V <- (log(MU_POP_V))
  lMU_POP_P[1] <- lMU_POP_K
  lMU_POP_P[2] <- lMU_POP_V
  POP_K <- (exp(lPOP_P[1]))
  POP_V <- (exp(lPOP_P[2]))
  # PRIORS
  POP_T ~ dgamma(a_POP_T, POP_T_rate)
  POP_T_rate <- b_POP_T
  inverseOMEGA_P[1:2,1:2] ~ dwish(OMEGA_P_inverseScaleMatrix[,],rho)
  OMEGA_P_inverseScaleMatrix[1:2,1:2] <- inverse(R[,])
  OMEGA_P[1:2,1:2] <- inverse(inverseOMEGA_P[,])
  OMEGA_T_IGamma ~ dgamma(OMEGA_T_shape,OMEGA_T_rate)
  OMEGA_T_shape <- 0.3
  OMEGA_T_scale <- 0.3
  OMEGA_T_rate <- 1/OMEGA_T_scale
  OMEGA_T <- 1/OMEGA_T_IGamma
  lPOP_P[1:2] ~ dmnorm(lPOP_P_mean[],lPOP_P_prec[,])
  lPOP_P_mean[1] <- lMU_POP_P[1]
  lPOP_P_mean[2] <- lMU_POP_P[2]
  lPOP_P_prec[1:2,1:2] <- inverse(SIGMA_POP_P[,])
}
