# 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: warfarin_PK_ODE_mog
# File: UseCase2
# Dated: Mon Aug 01 00:26:03 CEST 2016

model {
  for (ind_subj in 1:N_subj) {
    for (ind_t in 1:N_t[ind_subj]) {
      WTinterp[ind_subj,ind_t] <- interp.function(grid[ind_subj,ind_t], grid_WT[ind_subj,1:N_t_WT[ind_subj]], WT[ind_subj,1:N_t_WT[ind_subj]])
      k[ind_subj,ind_t] <- (CL[ind_subj,ind_t] / V[ind_subj,ind_t])
      logtWTinterp[ind_subj,ind_t] <- (log((WTinterp[ind_subj,ind_t] / 70)))
      Y_mean[ind_subj,ind_t] <- (piecewise_CC[ind_subj,ind_t] + 0)
      Y_prec[ind_subj,ind_t] <- 1 / ((pow(((RUV_ADD + (RUV_PROP * piecewise_CC[ind_subj,ind_t]))),2))*1)
      Y[ind_subj,ind_t] ~ dnorm(Y_mean[ind_subj,ind_t], Y_prec[ind_subj,ind_t])
      Y_res[ind_subj,ind_t] <- Y[ind_subj,ind_t]-Y_mean[ind_subj,ind_t]
      log(CL[ind_subj,ind_t]) <- log(POP_CL) + BETA_CL_WT * logtWTinterp[ind_subj,ind_t] + ETA_CL[ind_subj]
      log(V[ind_subj,ind_t]) <- log(POP_V) + BETA_V_WT * logtWTinterp[ind_subj,ind_t] + ETA_V[ind_subj]
      piecewise_CC[ind_subj,ind_t] <- function.piecewise1(VECTOR1[ind_subj,ind_t,])
      VECTOR1[ind_subj,ind_t,1] <- POP_TLAG
      VECTOR1[ind_subj,ind_t,2] <- BETA_V_WT
      VECTOR1[ind_subj,ind_t,3] <- ETA_V[ind_subj]
      VECTOR1[ind_subj,ind_t,4] <- ETA_KA[ind_subj]
      VECTOR1[ind_subj,ind_t,5] <- ETA_CL[ind_subj]
      VECTOR1[ind_subj,ind_t,6] <- BETA_CL_WT
      VECTOR1[ind_subj,ind_t,7] <- POP_KA
      VECTOR1[ind_subj,ind_t,8] <- D[ind_subj,1]
      VECTOR1[ind_subj,ind_t,9] <- WTinterp[ind_subj,ind_t]
      VECTOR1[ind_subj,ind_t,10] <- grid[ind_subj,ind_t]
      VECTOR1[ind_subj,ind_t,11] <- POP_V
      VECTOR1[ind_subj,ind_t,12] <- POP_CL
      VECTOR1[ind_subj,ind_t,13] <- ETA_TLAG[ind_subj]
    }
    ETA_KA[ind_subj] ~ dnorm(0, ETA_KA_prec)
    ETA_TLAG[ind_subj] ~ dnorm(0, ETA_TLAG_prec)
    log(KA[ind_subj])<- log(POP_KA) + ETA_KA[ind_subj]
    log(TLAG[ind_subj])<- log(POP_TLAG) + ETA_TLAG[ind_subj]
    ETA1[ind_subj,1:2] ~ dmnorm(mu1[], T1[,])
    ETA_CL[ind_subj] <- ETA1[ind_subj,1]
    ETA_V[ind_subj] <- ETA1[ind_subj,2]
  }
  ETA_KA_prec <- (1/(pow(PPV_KA,2)))
  ETA_TLAG_prec <- (1/(pow(PPV_TLAG,2)))
  T1[1:2,1:2] <- inverse(OMEGA1[,])
  mu1[1] <- 0.0
  mu1[2] <- 0.0
  OMEGA1[1,1] <- pow(PPV_CL,2)
  OMEGA1[1,2] <- CORR_CL_V*PPV_CL*PPV_V
  OMEGA1[2,1] <- CORR_CL_V*PPV_CL*PPV_V
  OMEGA1[2,2] <- pow(PPV_V,2)
  MU_CL <- (log(0.1))
  MU_V <- (log(8))
  MU_KA <- (log(0.362))
  MU_TLAG <- (log(1))
  MU_PPV_CL <- (log(0.1))
  MU_PPV_V <- (log(0.1))
  MU_PPV_KA <- (log(0.1))
  MU_CORR_CL_V <- (log(0.01))
  MU_RUV_PROP <- (log(0.1))
  MU_RUV_ADD <- (log(0.1))
  VAR_CL <- 1.5
  VAR_V <- 1
  VAR_KA <- 1.5
  VAR_TLAG <- 1.5
  VAR_PPV_CL <- 1.5
  VAR_PPV_V <- 1.5
  VAR_PPV_KA <- 1.5
  VAR_CORR_CL_V <- 2
  VAR_RUV_PROP <- 1.5
  VAR_RUV_ADD <- 1.5
  BETA_CL_WT <- 0.75
  BETA_V_WT <- 1
  PPV_TLAG <- 0.1
  POP_CL <- 0.1
  POP_V <- 8
  POP_KA <- 0.36
  POP_TLAG <- 1
  PPV_CL <- 0.1
  PPV_V <- 0.14
  PPV_KA <- 0.94
  CORR_CL_V <- 0.29
  RUV_PROP <- 0.11
  RUV_ADD <- 0.001
  # PRIORS
}
