# UseCase6 : Multiple distribution compartments (2cmpt model) 
# Warfarin population pharmacokinetic model 
# Dosing regimen: Multiple dosing using an oral bolus and IV Infusion using PK Macros
# Structural model : 2 compartment model (VC, KA, TLAG, K, K23, K32, VP)
# Covariate model : Weight on VC and CL 
# Variability model: 
#   - IIV on CL, VC, ka, TLAG, Q, VP
#	- IIV specified as sd, correlations
#   - Covariance between CL and V random variables
#   - Combined residual error model


warfarin_PK_2Compartments_dat = dataObj {
	DECLARED_VARIABLES{INPUT_KA::dosingTarget CC_obs::observation}

	DATA_INPUT_VARIABLES {
		ID : { use is id }
		TIME : { use is idv }
		WT : { use is covariate }
		AMT : { use is amt, define = {1 in CMT as INPUT_KA} }
		CMT : { use is cmt }
		DVID : { use is dvid}
		DV : { use is dv, variable = CC_obs }
		MDV : { use is mdv }
		logtWT:{ use is covariate }  
	} # end DATA_INPUT_VARIABLES
	
	SOURCE {
	    srcfile : {file = "warfarin_conc_cmt.csv", 
            inputFormat is nonmemFormat}
	} # end SOURCE
} # end data object

warfarin_PK_2Compartments_prior = priorObj {

	PRIOR_PARAMETERS{
		MU_CL=ln(0.1)
		MU_KA=ln(0.362)
		MU_TLAG=ln(1)
		MU_V=ln(8)
		MU_VC=ln(8)
		MU_Q=ln(0.2)
		MU_VP=ln(20)
		MU_PPV_CL=ln(0.01)
		MU_PPV_V=ln(0.3)
		MU_PPV_VC=ln(0.1)
		MU_PPV_KA=ln(0.1)
		MU_PPV_Q=ln(0.1)
		MU_PPV_VP=ln(0.1)

		VAR_CL=0.01
		VAR_V=0.09
		VAR_PPV_CL=0.0001
		VAR_PPV_V=0.001

		VAR_KA=0.001
		VAR_TLAG=0.001
		VAR_VC=0.03
		VAR_Q=0.01
		VAR_VP=0.01
		VAR_PPV_VC=ln(0.1^2)^2
		VAR_PPV_KA=ln(0.1^2)^2
		VAR_PPV_Q=ln(0.1^2)^2
		VAR_PPV_VP=ln(0.1^2)^2

		CORR_CL_V_MIN=0.1
		CORR_CL_V_MAX=0.2

		MU_RUV_PROP=ln(0.1)
		MU_RUV_ADD=ln(0.0001)
		VAR_RUV_PROP=0.1
		VAR_RUV_ADD=0.001	
		
		CLmin=0.09
		CLmax=0.11
	}
	
	PRIOR_VARIABLE_DEFINITION{
		BETA_CL_WT=0.75
		BETA_V_WT=1
		PPV_TLAG=0.1


		PPV_V=0.14
		PPV_KA =0.94
		RUV_PROP=0.11
		RUV_ADD=0.0001
		CORR_CL_V = 0.28
		POP_KA=1.7

PPV_CL=0.3
POP_TLAG=1
POP_V=8
POP_CL=0.1

		POP_VC =0.1
		POP_Q =0.1
		POP_VP =0.1
		PPV_VC =0.1
		PPV_Q = 0.1
		PPV_VP =0.1 

CORR_CL_VC=0.28
POP_BETA_CL_WT=1
POP_BETA_V_WT=1
		
#		POP_CL ~ Uniform1(minimum=CLmin, maximum = CLmax)
#		POP_V ~ Uniform1(minimum=CLmin, maximum = CLmax)
#		POP_CL ~ LogNormal2(meanLog=MU_CL, varLog = VAR_CL)
#		POP_V ~ LogNormal2(meanLog=MU_V, varLog = VAR_V)
#		PPV_CL ~ LogNormal2(meanLog=MU_PPV_CL, varLog = VAR_PPV_CL)
#		PPV_V ~ LogNormal2(meanLog=MU_PPV_V, varLog = VAR_PPV_V)
#		PPV_CL ~ Uniform1(minimum=CLmin, maximum = CLmax)
#		PPV_V ~ Uniform1(minimum=CLmin, maximum = CLmax)
#		CORR_CL_V ~ Uniform1(minimum=CORR_CL_V_MIN, maximum = CORR_CL_V_MAX)
#		RUV_PROP ~ LogNormal2(meanLog=MU_RUV_PROP, varLog = VAR_RUV_PROP)
#		RUV_ADD ~ LogNormal2(meanLog=MU_RUV_ADD, varLog = VAR_RUV_ADD)
#		RUV_PROP ~ Uniform1(minimum=CLmin, maximum = CLmax)
#		RUV_ADD ~ Uniform1(minimum=CLmin, maximum = CLmax)

#		POP_KA ~ LogNormal2(meanLog=MU_KA, varLog = VAR_KA)
#		POP_TLAG ~ LogNormal2(meanLog=MU_TLAG, varLog = VAR_TLAG)
#		POP_VC ~ LogNormal2(meanLog=MU_VC, varLog = VAR_VC)
#		POP_Q ~ LogNormal2(meanLog=MU_Q, varLog = VAR_Q)
#		POP_VP ~ LogNormal2(meanLog=MU_VP, varLog = VAR_VP)
#		PPV_KA ~ LogNormal2(meanLog=MU_PPV_KA, varLog = VAR_PPV_KA)
#		PPV_VC ~ LogNormal2(meanLog=MU_PPV_VC, varLog = VAR_PPV_VC)
#		PPV_Q ~ LogNormal2(meanLog=MU_PPV_Q, varLog = VAR_PPV_Q)
#		PPV_VP ~ LogNormal2(meanLog=MU_PPV_VP, varLog = VAR_PPV_VP)



	}
}


warfarin_PK_2Compartments_par = parObj{
  STRUCTURAL{
      POP_CL: {value=0.1, lo=0.001}
      POP_VC: {value=8, lo=0.001}
      POP_Q: {value=0.2, lo=0.001}
      POP_VP: {value=20, lo=0.001}
      POP_KA: {value=0.362, lo=0.001}
      POP_TLAG: {value=1, lo=0.001}
      POP_BETA_CL_WT: {value=0.75 ,fix=true}
      POP_BETA_V_WT: {value=1 ,fix=true}
   }# end STRUCTURAL

   VARIABILITY{
      PPV_CL: {value=0.1}
      PPV_VC: {value=0.1}
      CORR_CL_VC : {value=0.01}
      PPV_Q: {value=0.1}
      PPV_VP: {value=0.1}
      PPV_KA: {value=0.1}
      PPV_TLAG: {value=0.1, fix=true}
      RUV_PROP: {value=0.1, lo=0}
      RUV_ADD: {value=0.1, lo=0}
    }# end VARIABILITY

} # end of parameter object

warfarin_PK_2Compartments_microconstants_mdl = mdlObj{
   IDV{ TIME }

   COVARIATES{
      WT
      logtWT
   }# end COVARIATES

   VARIABILITY_LEVELS{
      ID: {type is parameter, level=2}
      DV: {type is observation, level=1}
   }# end VARIABILITY_LEVELS

   STRUCTURAL_PARAMETERS{
      POP_CL
      POP_VC
      POP_Q
      POP_VP
      POP_KA
      POP_TLAG
      POP_BETA_CL_WT
      POP_BETA_V_WT
   }# end STRUCTURAL_PARAMETERS

   VARIABILITY_PARAMETERS{
      PPV_CL
      PPV_VC
      CORR_CL_VC
      PPV_Q
      PPV_VP
      PPV_KA
      PPV_TLAG
      RUV_PROP
      RUV_ADD
   }# end VARIABILITY_PARAMETERS

   GROUP_VARIABLES{
   }# end GROUP_VARIABLES

   RANDOM_VARIABLE_DEFINITION (level=ID) {
      eta_PPV_CL ~ Normal(mean=0, sd=PPV_CL)
      eta_PPV_VC ~ Normal(mean=0, sd=PPV_VC)
      ::{type is correlation, rv1=eta_PPV_CL, rv2=eta_PPV_VC, value=CORR_CL_VC}
      eta_PPV_Q ~ Normal(mean=0, sd=PPV_Q)
      eta_PPV_VP ~ Normal(mean=0, sd=PPV_VP)
      eta_PPV_KA ~ Normal(mean=0, sd=PPV_KA)
      eta_PPV_TLAG ~ Normal(mean=0, sd=PPV_TLAG)
   }# end RANDOM_VARIABLE_DEFINITION (level=ID)

   INDIVIDUAL_VARIABLES{
     CL : {type is linear, trans is ln, pop = POP_CL, fixEff = [{coeff=POP_BETA_CL_WT, cov=logtWT}] , ranEff = eta_PPV_CL }
     VC  : {type is linear, trans is ln, pop = POP_VC, fixEff = [{coeff=POP_BETA_V_WT, cov=logtWT}] , ranEff = eta_PPV_VC }
     Q  : {type is linear, trans is ln, pop = POP_Q,  ranEff = eta_PPV_Q }
     VP  : {type is linear, trans is ln, pop = POP_VP,  ranEff = eta_PPV_VP }
     KA  : {type is linear, trans is ln, pop = POP_KA,  ranEff = eta_PPV_KA }
     TLAG  : {type is linear, trans is ln, pop = POP_TLAG, ranEff = eta_PPV_TLAG }
     
     K = CL/VC
     K23 = Q/VC
     K32 = Q/VP
     }# end INDIVIDUAL_VARIABLES

   MODEL_PREDICTION{

   COMPARTMENT{
      ##  Parameters = V,K,K23,K32,KA,S2,ALAG1
      INPUT_KA:   {type is depot, to=CENTRAL, ka=KA, tlag=TLAG}
      CENTRAL:    {type is compartment}
               :: {type is elimination, from=CENTRAL, v=VC, k=K}
      PERIPHERAL: {type is distribution, from=CENTRAL, kin=K23, kout=K32}
   }# end COMPARTMENT

      CC=CENTRAL/VC
   
   }# end MODEL_PREDICTION

   RANDOM_VARIABLE_DEFINITION (level=DV) {
      eps_RUV_EPS1 ~ Normal(mean=0, sd=1)
   }# end RANDOM_VARIABLE_DEFINITION (level=DV)

   OBSERVATION{
         CC_obs : {type is combinedError1, additive = RUV_ADD, proportional = RUV_PROP, eps = eps_RUV_EPS1, prediction = CC } 
   }# end OBSERVATION

} # end of model object

warfarin_PK_ODE_task = taskObj {
	ESTIMATE{ 
		set algo is mcmc
        TARGET_SETTINGS(target="BUGS"){
            set 
            nchains = 1,
            burnin = 1,
            niter = 1,
        #    parameters = "LAMBDA0_POP,LAMBDA1_POP,K1_POP,K2_POP,W0_POP,LAMBDA0,K2,CV",
         #    thinupdater=1,
          #  thinsamples=5, # uno dei due
        #    odesolver="LSODA", #oppure LSODA
        #    dic="true",
        	winbugsgui="true" # oppure no
        }
}
} # end of task object




warfarin_PK_ODE_mog = mogObj {
	OBJECTS{
		warfarin_PK_2Compartments_dat : { type is dataObj }
		warfarin_PK_2Compartments_prior : { type is priorObj }
		warfarin_PK_2Compartments_microconstants_mdl : { type is mdlObj }
		warfarin_PK_ODE_task : { type is taskObj }
	}
}
