# 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: magni2000_mog
# File: Magni_2000_diabetes_C-peptide2
# Dated: Mon Aug 01 11:13:44 CEST 2016

model {
  for (ind_subj in 1:N_subj) {
    for (ind_t in 1:N_t[ind_subj]) {
      WEIGHTinterp[ind_subj,ind_t] <- interp.function(grid[ind_subj,ind_t], grid_WEIGHT[ind_subj,1:N_t_WEIGHT[ind_subj]], WEIGHT[ind_subj,1:N_t_WEIGHT[ind_subj]])
      HEIGHTinterp[ind_subj,ind_t] <- interp.function(grid[ind_subj,ind_t], grid_HEIGHT[ind_subj,1:N_t_HEIGHT[ind_subj]], HEIGHT[ind_subj,1:N_t_HEIGHT[ind_subj]])
      AGEinterp[ind_subj,ind_t] <- interp.function(grid[ind_subj,ind_t], grid_AGE[ind_subj,1:N_t_AGE[ind_subj]], AGE[ind_subj,1:N_t_AGE[ind_subj]])
      HSTATUSinterp[ind_subj,ind_t] <- interp.function(grid[ind_subj,ind_t], grid_HSTATUS[ind_subj,1:N_t_HSTATUS[ind_subj]], HSTATUS[ind_subj,1:N_t_HSTATUS[ind_subj]])
      FEMALEinterp[ind_subj,ind_t] <- interp.function(grid[ind_subj,ind_t], grid_FEMALE[ind_subj,1:N_t_FEMALE[ind_subj]], FEMALE[ind_subj,1:N_t_FEMALE[ind_subj]])
      ts_PRED[ind_subj,ind_t] <- ts_IND[ind_subj,ind_t]
      F_PRED[ind_subj,ind_t] <- F_IND[ind_subj,ind_t]
      tl_PRED[ind_subj,ind_t] <- tl_IND[ind_subj,ind_t]
      V_PRED[ind_subj,ind_t] <- V_IND[ind_subj,ind_t]
      Y1_mean[ind_subj,ind_t] <- (ts_PRED[ind_subj,ind_t] + 0)
      Y1[ind_subj,ind_t] ~ dnorm(Y1_mean[ind_subj,ind_t], Y1_prec)
      Y1_res[ind_subj,ind_t] <- Y1[ind_subj,ind_t]-Y1_mean[ind_subj,ind_t]
      Y2_mean[ind_subj,ind_t] <- (F_PRED[ind_subj,ind_t] + 0)
      Y2[ind_subj,ind_t] ~ dnorm(Y2_mean[ind_subj,ind_t], Y2_prec)
      Y2_res[ind_subj,ind_t] <- Y2[ind_subj,ind_t]-Y2_mean[ind_subj,ind_t]
      Y3_mean[ind_subj,ind_t] <- (tl_PRED[ind_subj,ind_t] + 0)
      Y3[ind_subj,ind_t] ~ dnorm(Y3_mean[ind_subj,ind_t], Y3_prec)
      Y3_res[ind_subj,ind_t] <- Y3[ind_subj,ind_t]-Y3_mean[ind_subj,ind_t]
      Y4_mean[ind_subj,ind_t] <- (V_PRED[ind_subj,ind_t] + 0)
      Y4[ind_subj,ind_t] ~ dnorm(Y4_mean[ind_subj,ind_t], Y4_prec)
      Y4_res[ind_subj,ind_t] <- Y4[ind_subj,ind_t]-Y4_mean[ind_subj,ind_t]
      ts_IND[ind_subj,ind_t] <- (piecewise_GROUP_ts[ind_subj,ind_t] + eta_4[1])
      F_IND[ind_subj,ind_t] <- (piecewise_GROUP_F[ind_subj,ind_t] + eta_4[2])
      tl_IND[ind_subj,ind_t] <- (GROUP_tl[ind_subj,ind_t] + eta_4[3])
      V_IND[ind_subj,ind_t] <- (piecewise_GROUP_V[ind_subj,ind_t] + eta_4[4])
      piecewise_GROUP_ts[ind_subj,ind_t] <- function.piecewise1(VECTOR1[ind_subj,ind_t,])
      VECTOR1[ind_subj,ind_t,1] <- mtso
      VECTOR1[ind_subj,ind_t,2] <- HSTATUSinterp[ind_subj,ind_t]
      VECTOR1[ind_subj,ind_t,3] <- mtsn
      VECTOR1[ind_subj,ind_t,4] <- mtsd
      piecewise_GROUP_F[ind_subj,ind_t] <- function.piecewise2(VECTOR2[ind_subj,ind_t,])
      VECTOR2[ind_subj,ind_t,1] <- mFn
      VECTOR2[ind_subj,ind_t,2] <- mFd
      VECTOR2[ind_subj,ind_t,3] <- HSTATUSinterp[ind_subj,ind_t]
      VECTOR2[ind_subj,ind_t,4] <- mFo
      piecewise_GROUP_V[ind_subj,ind_t] <- function.piecewise3(VECTOR3[ind_subj,ind_t,])
      VECTOR3[ind_subj,ind_t,1] <- bVf
      VECTOR3[ind_subj,ind_t,2] <- WEIGHTinterp[ind_subj,ind_t]
      VECTOR3[ind_subj,ind_t,3] <- HEIGHTinterp[ind_subj,ind_t]
      VECTOR3[ind_subj,ind_t,4] <- FEMALEinterp[ind_subj,ind_t]
      VECTOR3[ind_subj,ind_t,5] <- bVm
      VECTOR3[ind_subj,ind_t,6] <- aVf
      VECTOR3[ind_subj,ind_t,7] <- aVm
      BSA[ind_subj,ind_t] <- ((pow(WEIGHTinterp[ind_subj,ind_t],0.425) * pow(HEIGHTinterp[ind_subj,ind_t],0.725)) * 0.007184)
      GROUP_tl[ind_subj,ind_t] <- (atl + (btl * AGEinterp[ind_subj,ind_t]))
    }
  }
  Y1_prec <- 1 / ((pow((ADD_SMALL),2))*1)
  Y2_prec <- 1 / ((pow((ADD_SMALL),2))*1)
  Y3_prec <- 1 / ((pow((ADD_SMALL),2))*1)
  Y4_prec <- 1 / ((pow((ADD_SMALL),2))*1)
  eta_4[1:4] ~ dmnorm(eta_4_mean[],eta_4_prec[,])
  eta_4_mean[1] <- 0
  eta_4_mean[2] <- 0
  eta_4_mean[3] <- 0
  eta_4_mean[4] <- 0
  eta_4_prec[1:4,1:4] <- inverse(MATRIX_1[,])
  MU_mtsn <- 5
  MU_mtso <- 5
  MU_mtsd <- 5
  MU_mFn <- 1
  MU_mFo <- 1
  MU_mFd <- 1
  MU_atl <- 30
  MU_btl <- 1
  MU_aVm <- 1
  MU_bVm <- 1
  MU_aVf <- 1
  MU_bVf <- 1
  VAR_mtsn <- pow(5,2)
  VAR_mtso <- pow(5,2)
  VAR_mtsd <- pow(5,2)
  VAR_mFn <- 1
  VAR_mFo <- 1
  VAR_mFd <- 1
  VAR_atl <- pow(30,2)
  VAR_btl <- 1
  VAR_aVm <- 1
  VAR_bVm <- 1
  VAR_aVf <- 1
  VAR_bVf <- 1
  rho <- 10
  ADD_SMALL <- 1.0E-20
  R[1:4,1:4] <- inverse(Rinv[,])
  MATRIX_1[1:4,1:4] <- inverse(invOMEGA_PAR[,])
  # PRIORS
  mtsd ~ dnorm(MU_mtsd, mtsd_prec)
  mtsd_prec <- (1/(VAR_mtsd))
  mFn ~ dnorm(MU_mFn, mFn_prec)
  mFn_prec <- (1/(VAR_mFn))
  atl ~ dnorm(MU_atl, atl_prec)
  atl_prec <- (1/(VAR_atl))
  mtsn ~ dnorm(MU_mtsn, mtsn_prec)
  mtsn_prec <- (1/(VAR_mtsn))
  mtso ~ dnorm(MU_mtso, mtso_prec)
  mtso_prec <- (1/(VAR_mtso))
  aVm ~ dnorm(MU_aVm, aVm_prec)
  aVm_prec <- (1/(VAR_aVm))
  btl ~ dnorm(MU_btl, btl_prec)
  btl_prec <- (1/(VAR_btl))
  bVf ~ dnorm(MU_bVf, bVf_prec)
  bVf_prec <- (1/(VAR_bVf))
  mFo ~ dnorm(MU_mFo, mFo_prec)
  mFo_prec <- (1/(VAR_mFo))
  invOMEGA_PAR[1:4,1:4] ~ dwish(invOMEGA_PAR_inverseScaleMatrix[,],rho)
  invOMEGA_PAR_inverseScaleMatrix[1:4,1:4] <- inverse(R[,])
  bVm ~ dnorm(MU_bVm, bVm_prec)
  bVm_prec <- (1/(VAR_bVm))
  aVf ~ dnorm(MU_aVf, aVf_prec)
  aVf_prec <- (1/(VAR_aVf))
  mFd ~ dnorm(MU_mFd, mFd_prec)
  mFd_prec <- (1/(VAR_mFd))
  R[1,1] <- 0.5
  R[1,2] <- 0
  R[1,3] <- 0
  R[1,4] <- 0
  R[2,1] <- 0
  R[2,2] <- 0.1
  R[2,3] <- 0
  R[2,4] <- 0
  R[3,1] <- 0
  R[3,2] <- 0
  R[3,3] <- 3
  R[3,4] <- 0
  R[4,1] <- 0
  R[4,2] <- 0
  R[4,3] <- 0
  R[4,4] <- 0.4
}
