# 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: example335
# File: example335
# Dated: Fri Aug 05 16:06:02 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)
    SD_ADD[ind_subj] <- (1 / (sqrt(TAU[ind_subj])))
    eta_K[ind_subj] ~ dnorm(0, eta_K_prec)
    eta_V[ind_subj] ~ dnorm(0, eta_V_prec)
    eta_T[ind_subj] ~ dnorm(0, eta_T_prec)
    log(K[ind_subj])<- log(POP_K) + eta_K[ind_subj]
    log(V[ind_subj])<- log(POP_V) + eta_V[ind_subj]
    log(TAU[ind_subj])<- log(POP_T) + eta_T[ind_subj]
  }
  eta_K_prec <- (1/(OMEGA_K))
  eta_V_prec <- (1/(OMEGA_V))
  eta_T_prec <- (1/(OMEGA_T))
  MU_POP_K <- 0.1
  MU_POP_V <- 8
  SIGMA_POP_P[1,1] <- 0.1
  SIGMA_POP_P[1,2] <- 5.0E-5
  SIGMA_POP_P[2,1] <- 5.0E-5
  SIGMA_POP_P[2,2] <- 0.1
  a_OMEGA_K <- 1
  b_OMEGA_K <- 0.01
  a_OMEGA_V <- 1
  b_OMEGA_V <- 0.01
  a_OMEGA_T <- 1
  b_OMEGA_T <- 0.01
  a_POP_T <- 1
  b_POP_T <- 0.01
  lMU_POP_P[1] <- (log(MU_POP_K))
  lMU_POP_P[2] <- (log(MU_POP_V))
  POP_K <- (exp(lPOP_P[1]))
  POP_V <- (exp(lPOP_P[2]))
  OMEGA_K <- (1 / TAU_K)
  OMEGA_V <- (1 / TAU_V)
  OMEGA_T <- (1 / TAU_T)
  # PRIORS
  TAU_V ~ dgamma(TAU_V_shape,TAU_V_rate)
  TAU_V_shape <- a_OMEGA_V
  TAU_V_rate <- b_OMEGA_V
  POP_T ~ dgamma(POP_T_shape,POP_T_rate)
  POP_T_shape <- a_POP_T
  POP_T_rate <- b_POP_T
  TAU_T ~ dgamma(TAU_T_shape,TAU_T_rate)
  TAU_T_shape <- a_OMEGA_T
  TAU_T_rate <- b_OMEGA_T
  TAU_K ~ dgamma(TAU_K_shape,TAU_K_rate)
  TAU_K_shape <- a_OMEGA_K
  TAU_K_rate <- b_OMEGA_K
  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[,])
}
