# 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: Hansson
# Dated: Mon Aug 01 08:05:44 CEST 2016

model {
  for (ind_subj in 1:N_subj) {
    for (ind_t in 1:N_t[ind_subj]) {
      CLinterp[ind_subj,ind_t] <- interp.function(grid[ind_subj,ind_t], grid_CL[ind_subj,1:N_t_CL[ind_subj]], CL[ind_subj,1:N_t_CL[ind_subj]])
      LNVEGF_obs_mean[ind_subj,ind_t] <- (VEGF[ind_subj,ind_t] + 0)
      LNVEGF_obs_prec[ind_subj,ind_t] <- 1 / ((pow(((sqrt(((POP_RES_VEGF_PROP * POP_RES_VEGF_PROP) + pow((0 / VEGF[ind_subj,ind_t]),2))))),2))*SIGMA_RES_W)
      LNVEGF_obs[ind_subj,ind_t] ~ dnorm(LNVEGF_obs_mean[ind_subj,ind_t], LNVEGF_obs_prec[ind_subj,ind_t])
      LNVEGF_obs_res[ind_subj,ind_t] <- LNVEGF_obs[ind_subj,ind_t]-LNVEGF_obs_mean[ind_subj,ind_t]
      LNsVEGFR2_obs_mean[ind_subj,ind_t] <- (sVEGFR2[ind_subj,ind_t] + 0)
      LNsVEGFR2_obs_prec[ind_subj,ind_t] <- 1 / ((pow(((sqrt(((POP_RES_sVEGFR2_PROP * POP_RES_sVEGFR2_PROP) + pow((POP_RES_sVEGFR2_ADD / sVEGFR2[ind_subj,ind_t]),2))))),2))*SIGMA_RES_W)
      LNsVEGFR2_obs[ind_subj,ind_t] ~ dnorm(LNsVEGFR2_obs_mean[ind_subj,ind_t], LNsVEGFR2_obs_prec[ind_subj,ind_t])
      LNsVEGFR2_obs_res[ind_subj,ind_t] <- LNsVEGFR2_obs[ind_subj,ind_t]-LNsVEGFR2_obs_mean[ind_subj,ind_t]
      LNsVEGFR3_obs_mean[ind_subj,ind_t] <- (sVEGFR3[ind_subj,ind_t] + 0)
      LNsVEGFR3_obs_prec[ind_subj,ind_t] <- 1 / ((pow(((sqrt(((POP_RES_sVEGFR3_PROP * POP_RES_sVEGFR3_PROP) + pow((0 / sVEGFR3[ind_subj,ind_t]),2))))),2))*SIGMA_RES_W)
      LNsVEGFR3_obs[ind_subj,ind_t] ~ dnorm(LNsVEGFR3_obs_mean[ind_subj,ind_t], LNsVEGFR3_obs_prec[ind_subj,ind_t])
      LNsVEGFR3_obs_res[ind_subj,ind_t] <- LNsVEGFR3_obs[ind_subj,ind_t]-LNsVEGFR3_obs_mean[ind_subj,ind_t]
      LNsKIT_obs_mean[ind_subj,ind_t] <- (sKIT[ind_subj,ind_t] + 0)
      LNsKIT_obs_prec[ind_subj,ind_t] <- 1 / ((pow(((sqrt(((POP_RES_sKIT_PROP * POP_RES_sKIT_PROP) + pow((0 / sKIT[ind_subj,ind_t]),2))))),2))*SIGMA_RES_W)
      LNsKIT_obs[ind_subj,ind_t] ~ dnorm(LNsKIT_obs_mean[ind_subj,ind_t], LNsKIT_obs_prec[ind_subj,ind_t])
      LNsKIT_obs_res[ind_subj,ind_t] <- LNsKIT_obs[ind_subj,ind_t]-LNsKIT_obs_mean[ind_subj,ind_t]
    }
    IMAX1[ind_subj] <- POP_IMAX
    IMAX2[ind_subj] <- POP_IMAX
    IMAX3[ind_subj] <- POP_IMAX
    IMAXS[ind_subj] <- POP_IMAX
    HILL[ind_subj] <- POP_HILL
    HILL2[ind_subj] <- POP_HILL2
    KOUT[ind_subj] <- (1 / MRT1[ind_subj])
    KOUT2[ind_subj] <- (1 / MRT2[ind_subj])
    KOUT3[ind_subj] <- (1 / MRT3[ind_subj])
    KOUTS[ind_subj] <- (1 / MRTS[ind_subj])
    KIN2[ind_subj] <- (BM02[ind_subj] * KOUT2[ind_subj])
    KIN3[ind_subj] <- (BM03[ind_subj] * KOUT3[ind_subj])
    TVSLP[ind_subj] <- (POP_TVSLP / 1000)
    TVSLPS[ind_subj] <- (POP_TVSLP / 1000)
    eta_BM0[ind_subj] ~ dnorm(0, eta_BM0_prec)
    eta_BM02[ind_subj] ~ dnorm(0, eta_BM02_prec)
    eta_BM03[ind_subj] ~ dnorm(0, eta_BM03_prec)
    eta_BM0S[ind_subj] ~ dnorm(0, eta_BM0S_prec)
    eta_MRT_VEGFs[ind_subj] ~ dnorm(0, eta_MRT_VEGFs_prec)
    eta_MRT_sKIT[ind_subj] ~ dnorm(0, eta_MRT_sKIT_prec)
    eta_IC50[ind_subj] ~ dnorm(0, eta_IC50_prec)
    eta_IC502[ind_subj] ~ dnorm(0, eta_IC502_prec)
    eta_IC503[ind_subj] ~ dnorm(0, eta_IC503_prec)
    eta_IC50S[ind_subj] ~ dnorm(0, eta_IC50S_prec)
    eta_TVSLP[ind_subj] ~ dnorm(0, eta_TVSLP_prec)
    eta_TVSLPS[ind_subj] ~ dnorm(0, eta_TVSLPS_prec)
    log(BM0[ind_subj])<- log(POP_BM0) + eta_BM0[ind_subj]
    log(BM02[ind_subj])<- log(POP_BM02) + eta_BM02[ind_subj]
    log(BM03[ind_subj])<- log(POP_BM03) + eta_BM03[ind_subj]
    log(BM0S[ind_subj])<- log(POP_BM0S) + eta_BM0S[ind_subj]
    log(IC50[ind_subj])<- log(POP_IC50) + eta_IC50[ind_subj]
    log(IC502[ind_subj])<- log(POP_IC50) + eta_IC502[ind_subj]
    log(IC503[ind_subj])<- log(POP_IC50) + eta_IC503[ind_subj]
    log(IC50S[ind_subj])<- log(POP_IC50) + eta_IC50S[ind_subj]
    log(MRT1[ind_subj])<- log(POP_MRT) + eta_MRT_VEGFs[ind_subj]
    log(MRT2[ind_subj])<- log(POP_MRT2) + eta_MRT_VEGFs[ind_subj]
    log(MRT3[ind_subj])<- log(POP_MRT3) + eta_MRT_VEGFs[ind_subj]
    log(MRTS[ind_subj])<- log(POP_MRTS) + eta_MRT_sKIT[ind_subj]
    log(DPSLP[ind_subj])<- log(TVSLP[ind_subj]) + eta_TVSLP[ind_subj]
    log(DPSLPS[ind_subj])<- log(TVSLPS[ind_subj]) + eta_TVSLPS[ind_subj]
    der99wb_unipv[ind_subj,1:N_t[ind_subj], 1:4]<- function.model1(grid[ind_subj,1:N_t[ind_subj]], amt[ind_subj,1:N_t[ind_subj]],  rate[ind_subj,1:N_t[ind_subj]], ii[ind_subj,1:N_t[ind_subj]], evid[ind_subj,1:N_t[ind_subj]], cmt[ind_subj,1:N_t[ind_subj]], addl[ind_subj,1:N_t[ind_subj]], ss[ind_subj,1:N_t[ind_subj]], theta[ind_subj,])
    theta[ind_subj,1] <- n_cov_cont
    theta[ind_subj,2] <- max_m_CL
    theta[ind_subj,3] <- N_t_CL[ind_subj]
    theta[ind_subj,3+2*max_m_CL +1] <- eta_MRT_VEGFs[ind_subj]
    theta[ind_subj,3+2*max_m_CL +2] <- POP_MRT
    theta[ind_subj,3+2*max_m_CL +3] <- POP_IMAX
    theta[ind_subj,3+2*max_m_CL +4] <- DOSE
    theta[ind_subj,3+2*max_m_CL +5] <- POP_HILL
    theta[ind_subj,3+2*max_m_CL +6] <- eta_IC50[ind_subj]
    theta[ind_subj,3+2*max_m_CL +7] <- POP_IC50
    theta[ind_subj,3+2*max_m_CL +8] <- eta_BM02[ind_subj]
    theta[ind_subj,3+2*max_m_CL +9] <- POP_BM02
    theta[ind_subj,3+2*max_m_CL +10] <- POP_HILL2
    theta[ind_subj,3+2*max_m_CL +11] <- eta_IC502[ind_subj]
    theta[ind_subj,3+2*max_m_CL +12] <- POP_MRT2
    theta[ind_subj,3+2*max_m_CL +13] <- eta_BM03[ind_subj]
    theta[ind_subj,3+2*max_m_CL +14] <- POP_BM03
    theta[ind_subj,3+2*max_m_CL +15] <- eta_IC503[ind_subj]
    theta[ind_subj,3+2*max_m_CL +16] <- POP_MRT3
    theta[ind_subj,3+2*max_m_CL +17] <- eta_IC50S[ind_subj]
    theta[ind_subj,3+2*max_m_CL +18] <- eta_MRT_sKIT[ind_subj]
    theta[ind_subj,3+2*max_m_CL +19] <- POP_MRTS
    theta[ind_subj,3+2*max_m_CL +20] <- eta_BM0[ind_subj]
    theta[ind_subj,3+2*max_m_CL +21] <- POP_BM0
    theta[ind_subj,3+2*max_m_CL +22] <- eta_TVSLP[ind_subj]
    theta[ind_subj,3+2*max_m_CL +23] <- eta_BM0S[ind_subj]
    theta[ind_subj,3+2*max_m_CL +24] <- POP_BM0S
    theta[ind_subj,3+2*max_m_CL +25] <- eta_TVSLPS[ind_subj]
    theta[ind_subj,3+2*max_m_CL +26] <- POP_TVSLP
    for (i in 1 : max_m_CL){ 
	theta[ind_subj,i+3] <- grid_CL[ind_subj,i]
	}
	for (i in 1 : max_m_CL){ 
	theta[ind_subj,i+3+max_m_CL] <- CL[ind_subj,i]
	}
      theta[ind_subj,3+2*max_m_CL+27] <- BM0[ind_subj] #IC VEGF
      theta[ind_subj,3+2*max_m_CL+28] <- BM02[ind_subj] #IC sVEGFR2
      theta[ind_subj,3+2*max_m_CL+29] <- BM03[ind_subj] #IC sVEGFR3
      theta[ind_subj,3+2*max_m_CL+30] <- BM0S[ind_subj] #IC sKIT
      theta[ind_subj,3+2*max_m_CL+31] <- 1 #F VEGF
      theta[ind_subj,3+2*max_m_CL+32] <- 1 #F sVEGFR2
      theta[ind_subj,3+2*max_m_CL+33] <- 1 #F sVEGFR3
      theta[ind_subj,3+2*max_m_CL+34] <- 1 #F sKIT
      theta[ind_subj,3+2*max_m_CL+35] <- 0 #TLAG VEGF
      theta[ind_subj,3+2*max_m_CL+36] <- 0 #TLAG sVEGFR2
      theta[ind_subj,3+2*max_m_CL+37] <- 0 #TLAG sVEGFR3
      theta[ind_subj,3+2*max_m_CL+38] <- 0 #TLAG sKIT
      for (ind_q in 1:N_t[ind_subj]) {
        VEGF[ind_subj,ind_q] <- der99wb_unipv[ind_subj,ind_q,1] + BM0[ind_subj]
        sVEGFR2[ind_subj,ind_q] <- der99wb_unipv[ind_subj,ind_q,2] + BM02[ind_subj]
        sVEGFR3[ind_subj,ind_q] <- der99wb_unipv[ind_subj,ind_q,3] + BM03[ind_subj]
        sKIT[ind_subj,ind_q] <- der99wb_unipv[ind_subj,ind_q,4] + BM0S[ind_subj]
        AUC[ind_subj,ind_q] <- (DOSE / CLinterp[ind_subj,ind_q])
        DP1[ind_subj,ind_q] <- (BM0[ind_subj] * (1 + (DPSLP[ind_subj] * grid[ind_subj,ind_q])))
        DPS[ind_subj,ind_q] <- (BM0S[ind_subj] * (1 + (DPSLPS[ind_subj] * grid[ind_subj,ind_q])))
        KIN[ind_subj,ind_q] <- (DP1[ind_subj,ind_q] * KOUT[ind_subj])
        KINS[ind_subj,ind_q] <- (DPS[ind_subj,ind_q] * KOUTS[ind_subj])
        EFF[ind_subj,ind_q] <- ((IMAX1[ind_subj] * pow(AUC[ind_subj,ind_q],HILL[ind_subj])) / (pow(IC50[ind_subj],HILL[ind_subj]) + pow(AUC[ind_subj,ind_q],HILL[ind_subj])))
        EFF2[ind_subj,ind_q] <- ((IMAX2[ind_subj] * pow(AUC[ind_subj,ind_q],HILL2[ind_subj])) / (pow(IC502[ind_subj],HILL2[ind_subj]) + pow(AUC[ind_subj,ind_q],HILL2[ind_subj])))
        EFF3[ind_subj,ind_q] <- ((IMAX3[ind_subj] * AUC[ind_subj,ind_q]) / (IC503[ind_subj] + AUC[ind_subj,ind_q]))
        EFFS[ind_subj,ind_q] <- ((IMAXS[ind_subj] * AUC[ind_subj,ind_q]) / (IC50S[ind_subj] + AUC[ind_subj,ind_q]))
      }
    }
    eta_BM0_prec <- (1/(OMEGA_BM0))
    eta_BM02_prec <- (1/(OMEGA_BM02))
    eta_BM03_prec <- (1/(OMEGA_BM03))
    eta_BM0S_prec <- (1/(OMEGA_BM0S))
    eta_MRT_VEGFs_prec <- (1/(OMEGA_MRT_VEGFs))
    eta_MRT_sKIT_prec <- (1/(OMEGA_MRT_sKIT))
    eta_IC50_prec <- (1/(OMEGA_IC50))
    eta_IC502_prec <- (1/(OMEGA_IC502))
    eta_IC503_prec <- (1/(OMEGA_IC503))
    eta_IC50S_prec <- (1/(OMEGA_IC50S))
    eta_TVSLP_prec <- (1/(OMEGA_TVSLP))
    eta_TVSLPS_prec <- (1/(OMEGA_TVSLPS))
    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
    POP_IMAX <- 1
    POP_IC50 <- 1
    POP_BM0 <- 59.7
    POP_MRT <- 91
    POP_HILL <- 3.31
    POP_TVSLP <- 0.035
    POP_BM02 <- 8670
    POP_MRT2 <- 554
    POP_HILL2 <- 1.54
    POP_BM03 <- 63900
    POP_MRT3 <- 401
    POP_BM0S <- 39200
    POP_MRTS <- 2430
    POP_RES_VEGF_PROP <- 0.445
    POP_RES_sVEGFR2_PROP <- 0.12
    POP_RES_sVEGFR2_ADD <- 583
    POP_RES_sVEGFR3_PROP <- 0.22
    POP_RES_sKIT_PROP <- 0.224
    OMEGA_BM0 <- 0.252
    OMEGA_BM02 <- 0.0369
    OMEGA_BM03 <- 0.186
    OMEGA_BM0S <- 0.254
    OMEGA_MRT_VEGFs <- 0.06
    OMEGA_MRT_sKIT <- 0.0753
    OMEGA_IC50 <- 0.253
    OMEGA_IC502 <- 0.189
    OMEGA_IC503 <- 0.398
    OMEGA_IC50S <- 5.77
    OMEGA_TVSLP <- 2.95
    OMEGA_TVSLPS <- 3.01
    SIGMA_RES_W <- 1
    # PRIORS
}
