# 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: UseCase5
# Dated: Mon Aug 01 11:18:59 CEST 2016

model {
  for (ind_subj in 1:N_subj) {
    for (ind_t in 1:N_t[ind_subj]) {
      SEXFinterp[ind_subj,ind_t] <- interp.function.cost(grid[ind_subj,ind_t], grid_SEXF[ind_subj,1:N_t_SEXF[ind_subj]], SEXF[ind_subj,1:N_t_SEXF[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]])
      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]])
      logtWTinterp[ind_subj,ind_t] <- (log((WTinterp[ind_subj,ind_t] / 70)))
      tAGEinterp[ind_subj,ind_t] <- (AGEinterp[ind_subj,ind_t] - 40)
      Y_mean[ind_subj,ind_t] <- (CC[ind_subj,ind_t] + 0)
      Y_prec[ind_subj,ind_t] <- 1 / ((pow(((RUV_ADD + (RUV_PROP * 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] + POP_FCL_FEM * piece_SEXF[ind_subj,ind_t]  + BETA_CL_AGE * tAGEinterp[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]
      piece_SEXF[ind_subj,ind_t] <- function.covariate1(SEXFinterp[ind_subj,ind_t])
    }
    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]
    der99wb_unipv[ind_subj,1:N_t[ind_subj], 1:2]<- 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_WT
    theta[ind_subj,3] <- max_m_AGE
    theta[ind_subj,4] <- N_t_WT[ind_subj]
    theta[ind_subj,5] <- N_t_AGE[ind_subj]
    theta[ind_subj,6] <- n_cov_cat
    theta[ind_subj,7] <- max_m_SEXF
    theta[ind_subj,8] <- N_t_SEXF[ind_subj]
    theta[ind_subj,8+2*max_m_WT+2*max_m_AGE+2*max_m_SEXF +1] <- ETA_KA[ind_subj]
    theta[ind_subj,8+2*max_m_WT+2*max_m_AGE+2*max_m_SEXF +2] <- POP_KA
    theta[ind_subj,8+2*max_m_WT+2*max_m_AGE+2*max_m_SEXF +3] <- ETA_CL[ind_subj]
    theta[ind_subj,8+2*max_m_WT+2*max_m_AGE+2*max_m_SEXF +4] <- POP_CL
    theta[ind_subj,8+2*max_m_WT+2*max_m_AGE+2*max_m_SEXF +5] <- BETA_CL_WT
    theta[ind_subj,8+2*max_m_WT+2*max_m_AGE+2*max_m_SEXF +6] <- POP_FCL_FEM
    theta[ind_subj,8+2*max_m_WT+2*max_m_AGE+2*max_m_SEXF +7] <- BETA_CL_AGE
    theta[ind_subj,8+2*max_m_WT+2*max_m_AGE+2*max_m_SEXF +8] <- ETA_V[ind_subj]
    theta[ind_subj,8+2*max_m_WT+2*max_m_AGE+2*max_m_SEXF +9] <- POP_V
    theta[ind_subj,8+2*max_m_WT+2*max_m_AGE+2*max_m_SEXF +10] <- BETA_V_WT
    for (i in 1 : max_m_WT){ 
	theta[ind_subj,i+8] <- grid_WT[ind_subj,i]
	}
	for (i in 1 : max_m_WT){ 
	theta[ind_subj,i+8+max_m_WT] <- WT[ind_subj,i]
	}

	for (i in 1 : max_m_AGE){ 
	theta[ind_subj,i+8+2*max_m_WT] <- grid_AGE[ind_subj,i]
	}
	for (i in 1 : max_m_AGE){ 
	theta[ind_subj,i+8+2*max_m_WT+max_m_AGE] <- AGE[ind_subj,i]
	}

	for (i in 1 : max_m_SEXF){ 
	theta[ind_subj,i+8+2*max_m_WT+2*max_m_AGE] <- grid_SEXF[ind_subj,i]
	}
	for (i in 1 : max_m_SEXF){ 
	theta[ind_subj,i+8+2*max_m_WT+2*max_m_AGE+max_m_SEXF] <- SEXF[ind_subj,i]
	}
      theta[ind_subj,8+2*max_m_WT+2*max_m_AGE+2*max_m_SEXF+11] <- 0.0 #IC Ad1
      theta[ind_subj,8+2*max_m_WT+2*max_m_AGE+2*max_m_SEXF+12] <- 0.0 #IC CENTRAL
      theta[ind_subj,8+2*max_m_WT+2*max_m_AGE+2*max_m_SEXF+13] <- 1 #F Ad1
      theta[ind_subj,8+2*max_m_WT+2*max_m_AGE+2*max_m_SEXF+14] <- 1 #F CENTRAL
      theta[ind_subj,8+2*max_m_WT+2*max_m_AGE+2*max_m_SEXF+15] <- TLAG[ind_subj] #TLAG Ad1
      theta[ind_subj,8+2*max_m_WT+2*max_m_AGE+2*max_m_SEXF+16] <- 0 #TLAG CENTRAL
      for (ind_q in 1:N_t[ind_subj]) {
        Ad1[ind_subj,ind_q] <- der99wb_unipv[ind_subj,ind_q,1] + 0.0
        CENTRAL[ind_subj,ind_q] <- der99wb_unipv[ind_subj,ind_q,2] + 0.0
        CC[ind_subj,ind_q] <- (CENTRAL[ind_subj,ind_q] / V[ind_subj,ind_q])
      }
    }
    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_FORAL <- (log(0.7))
    MU_PPV_FORAL <- (log(0.1))
    MU_CORR_CL_V <- (log(0.01))
    MU_BETA_CL_AGE <- (log(0.001))
    MU_POP_FCL_FEM <- (log(1))
    MU_RUV_PROP <- (log(0.1))
    MU_RUV_ADD <- (log(0.1))
    VAR_CL <- pow((log(pow(0.1,2))),2)
    VAR_V <- pow((log(pow(8,2))),2)
    VAR_KA <- pow((log(pow(0.362,2))),2)
    VAR_TLAG <- pow((log(pow(1,2))),2)
    VAR_PPV_CL <- pow((log(pow(0.1,2))),2)
    VAR_PPV_V <- pow((log(pow(0.1,2))),2)
    VAR_PPV_KA <- pow((log(pow(0.1,2))),2)
    VAR_FORAL <- pow((log(pow(0.7,2))),2)
    VAR_PPV_FORAL <- pow((log(pow(0.1,2))),2)
    VAR_CORR_CL_V <- pow((log(pow(0.01,2))),2)
    VAR_BETA_CL_AGE <- pow((log(pow(0.001,2))),2)
    VAR_POP_FCL_FEM <- pow((log(pow(1,2))),2)
    VAR_RUV_PROP <- pow((log(pow(0.1,2))),2)
    VAR_RUV_ADD <- pow((log(pow(0.1,2))),2)
    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
    BETA_CL_AGE <- 0.001
    POP_FCL_FEM <- 1
    POP_FORAL <- 0.1
    # PRIORS
}
