# 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: UseCase1_PRIOR_univariate
# Dated: Sun Jul 31 15:14:23 CEST 2016

model {
  for (ind_subj in 1:N_subj) {
    for (ind_t in 1:N_t[ind_subj]) {
      logtWTinterp[ind_subj,ind_t] <- interp.function(grid[ind_subj,ind_t], grid_logtWT[ind_subj,1:N_t_logtWT[ind_subj]], logtWT[ind_subj,1:N_t_logtWT[ind_subj]])
      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] + 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]
    }
    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_logtWT
    theta[ind_subj,3] <- N_t_logtWT[ind_subj]
    theta[ind_subj,3+2*max_m_logtWT +1] <- ETA_CL[ind_subj]
    theta[ind_subj,3+2*max_m_logtWT +2] <- BETA_CL_WT
    theta[ind_subj,3+2*max_m_logtWT +3] <- ETA_V[ind_subj]
    theta[ind_subj,3+2*max_m_logtWT +4] <- BETA_V_WT
    theta[ind_subj,3+2*max_m_logtWT +5] <- ETA_KA[ind_subj]
    theta[ind_subj,3+2*max_m_logtWT +6] <- ETA_TLAG[ind_subj]
    theta[ind_subj,3+2*max_m_logtWT +7] <- lPOP_V
    theta[ind_subj,3+2*max_m_logtWT +8] <- lPOP_KA
    theta[ind_subj,3+2*max_m_logtWT +9] <- lPOP_TLAG
    for (i in 1 : max_m_logtWT){ 
	theta[ind_subj,i+3] <- grid_logtWT[ind_subj,i]
	}
	for (i in 1 : max_m_logtWT){ 
	theta[ind_subj,i+3+max_m_logtWT] <- logtWT[ind_subj,i]
	}
      theta[ind_subj,3+2*max_m_logtWT+10] <- 0 #IC GUT
      theta[ind_subj,3+2*max_m_logtWT+11] <- 0 #IC CENTRAL
      theta[ind_subj,3+2*max_m_logtWT+12] <- 1 #F GUT
      theta[ind_subj,3+2*max_m_logtWT+13] <- 1 #F CENTRAL
      theta[ind_subj,3+2*max_m_logtWT+14] <- 0 #TLAG GUT
      theta[ind_subj,3+2*max_m_logtWT+15] <- 0 #TLAG CENTRAL
      for (ind_q in 1:N_t[ind_subj]) {
        GUT[ind_subj,ind_q] <- der99wb_unipv[ind_subj,ind_q,1] + 0
        CENTRAL[ind_subj,ind_q] <- der99wb_unipv[ind_subj,ind_q,2] + 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] <- COV_CL_V
    OMEGA1[2,1] <- COV_CL_V
    OMEGA1[2,2] <- pow(PPV_V,2)
    MU_POP_CL <- 0.2
    MU_POP_V <- 10
    MU_POP_KA <- 0.3
    MU_POP_TLAG <- 0.75
    VAR_POP_CL <- 1
    VAR_POP_V <- 1
    VAR_POP_KA <- 1
    VAR_POP_TLAG <- 0.1
    a_POP_RUV_ADD <- 1.1
    b_POP_RUV_ADD <- 3
    a_POP_RUV_PROP <- 1.1
    b_POP_RUV_PROP <- 3
    lMU_POP_CL <- (log(MU_POP_CL))
    POP_CL <- (exp(lPOP_V))
    lMU_POP_V <- (log(MU_POP_V))
    POP_V <- (exp(lPOP_V))
    lMU_POP_KA <- (log(MU_POP_KA))
    POP_KA <- (exp(lPOP_KA))
    lMU_POP_TLAG <- (log(MU_POP_TLAG))
    POP_TLAG <- (exp(lPOP_TLAG))
    PPV_CL <- (sqrt((1 / TAU_CL)))
    PPV_V <- (sqrt((1 / TAU_V)))
    PPV_KA <- (sqrt((1 / TAU_KA)))
    RUV_ADD <- (sqrt((1 / invRUV_ADD)))
    RUV_PROP <- (sqrt((1 / invRUV_PROP)))
    PPV_TLAG <- 0.01
    BETA_CL_WT <- 0.75
    BETA_V_WT <- 1
    # PRIORS
    lPOP_KA ~ dnorm(lMU_POP_KA, lPOP_KA_prec)
    lPOP_KA_prec <- (1/(VAR_POP_KA))
    lPOP_CL ~ dnorm(lMU_POP_CL, lPOP_CL_prec)
    lPOP_CL_prec <- (1/(VAR_POP_CL))
    lPOP_TLAG ~ dnorm(lMU_POP_TLAG, lPOP_TLAG_prec)
    lPOP_TLAG_prec <- (1/(VAR_POP_TLAG))
    invRUV_ADD ~ dgamma(a_POP_RUV_ADD, invRUV_ADD_rate)
    invRUV_ADD_rate <- b_POP_RUV_ADD
    TAU_KA ~ dgamma(0.001, TAU_KA_rate)
    TAU_KA_rate <- 0.001
    lPOP_V ~ dnorm(lMU_POP_V, lPOP_V_prec)
    lPOP_V_prec <- (1/(VAR_POP_V))
    TAU_V ~ dgamma(0.001, TAU_V_rate)
    TAU_V_rate <- 0.001
    invRUV_PROP ~ dgamma(a_POP_RUV_PROP, invRUV_PROP_rate)
    invRUV_PROP_rate <- b_POP_RUV_PROP
    TAU_CL ~ dgamma(0.001, TAU_CL_rate)
    TAU_CL_rate <- 0.001
    COV_CL_V ~ dunif(COV_CL_V_min,COV_CL_V_max)
    COV_CL_V_min <- (-(1))
    COV_CL_V_max <- 1
}
