# UseCase5 : Uses GROUP_VARIABLES specification for fixed effect model and equation specification for INDIVIDUAL_VARIABLES.
# Warfarin population pharmacokinetic model with categorical and transformed covariates
# Dosing regimen: single oral administration (at time 0)
# Structural model : 1 compartment model (CL,V and TLAG)
# Covariate model : Weight on V and CL, SEX on CL and AGE on CL 
# Variability model: 
#   - IIV on CL, V, ka and TLAG
#   - Correlation between CL and V random variables
#   - Combined residual error model

warfarin_PK_covariate_dat = dataObj {
	DECLARED_VARIABLES{
		INPUT_KA :: dosingTarget
		Y ::observation
	}
	
	DATA_INPUT_VARIABLES {
		ID : { use is id }
		TIME : { use is idv }
		WT : { use is covariate }
		AGE : { use is covariate }
		SEXF : {use is catCov withCategories {female when 1, male when 0} }
		AMT : { use is amt, variable = INPUT_KA } 
		DVID : { use is dvid}
		DV : { use is dv, variable = Y }
		MDV : { use is mdv } 
	} # end DATA_INPUT_VARIABLES
	
	SOURCE {
      srcfile : {file="warfarin_conc_sexf.csv", 
      			 inputFormat is nonmemFormat} 
	} # end SOURCE
} # end data object

warfarin_PK_covariate_prior = priorObj {

	PRIOR_PARAMETERS{
	}
	
	PRIOR_VARIABLE_DEFINITION{
		
		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
		
#		POP_CL ~ LogNormal2(meanLog=MU_CL, varLog = VAR_CL)
#		POP_V ~ LogNormal2(meanLog=MU_V, varLog = VAR_V)
#		POP_KA ~ LogNormal2(meanLog=MU_KA, varLog = VAR_KA)
#		POP_TLAG ~ LogNormal2(meanLog=MU_TLAG, varLog = VAR_TLAG)
#		POP_FORAL ~ LogNormal2(meanLog=MU_FORAL, varLog = VAR_FORAL)
#		PPV_CL ~ LogNormal2(meanLog=MU_PPV_CL, varLog = VAR_PPV_CL)
#		PPV_V ~ LogNormal2(meanLog=MU_PPV_V, varLog = VAR_PPV_V)
#		PPV_KA ~ LogNormal2(meanLog=MU_PPV_KA, varLog = VAR_PPV_KA)
#		PPV_FORAL ~ LogNormal2(meanLog=MU_PPV_FORAL, varLog = VAR_PPV_FORAL)
#		CORR_CL_V ~ LogNormal2(meanLog=MU_CORR_CL_V, varLog = VAR_CORR_CL_V)
#		BETA_CL_AGE ~ LogNormal2(meanLog=MU_BETA_CL_AGE, varLog = VAR_BETA_CL_AGE)
#		POP_FCL_FEM ~ LogNormal2(meanLog=MU_POP_FCL_FEM, varLog = VAR_POP_FCL_FEM)
#		RUV_PROP ~ LogNormal2(meanLog=MU_RUV_PROP, varLog = VAR_RUV_PROP)
#		RUV_ADD ~ LogNormal2(meanLog=MU_RUV_ADD, varLog = VAR_RUV_ADD)
	}
}


warfarin_PK_covariate_par = parObj {
	# By default a parameter is to be estimated if fix is omitted
 	STRUCTURAL {
		POP_CL : { value = 0.1, lo = 0.001 }
		POP_V : { value = 8, lo = 0.001 } 
		POP_KA : { value = 0.362, lo = 0.001 }
		POP_TLAG : { value=1, lo=0.001 }
		BETA_CL_WT : { value = 0.75, fix = true }
		BETA_V_WT : { value = 1, fix = true } 
		BETA_CL_AGE : { value=0.001 }
		POP_FCL_FEM : { value=1, lo = 0 }
		} # end STRUCTURAL
	VARIABILITY {
		PPV_CL : { value = 0.1 }
		PPV_V : { value = 0.1 }
		PPV_KA : { value = 0.1 }
		PPV_TLAG : { value = 0.1, fix=true }
		PPV_FORAL : { value = 0.1 }
		CORR_CL_V : { value = 0.01 }
		RUV_PROP : { value = 0.1, lo = 0 }
		RUV_ADD : { value = 0.1, lo = 0.0001 } 		
	} # end VARIABILITY
} # end of parameter object 

warfarin_PK_covariate_mdl = mdlObj {
	IDV { T }
	
	COVARIATES{
		WT
		AGE
		SEXF withCategories {female, male} 
		logtWT = ln(WT/70)
		tAGE = AGE - 40
	}

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

	STRUCTURAL_PARAMETERS {
		POP_CL
		POP_V
		POP_KA
		POP_TLAG
		BETA_CL_WT
		BETA_CL_AGE
		BETA_V_WT
		POP_FCL_FEM
	} # end STRUCTURAL_PARAMETERS
	
	VARIABILITY_PARAMETERS {
		PPV_CL
		PPV_V
		CORR_CL_V
		PPV_KA
		PPV_TLAG
		RUV_PROP
		RUV_ADD
		} # end VARIABILITY_PARAMETERS 

	RANDOM_VARIABLE_DEFINITION(level=ID) {
		ETA_CL ~ Normal(mean = 0, sd = PPV_CL)
		ETA_V ~ Normal(mean = 0, sd = PPV_V)
		:: {type is correlation, rv1=ETA_CL, rv2=ETA_V, value=CORR_CL_V}		
		ETA_KA ~ Normal(mean = 0, sd = PPV_KA)
		ETA_TLAG ~ Normal(mean = 0, sd = PPV_TLAG) # We define correlation here
	} # end RANDOM_VARIABLE_DEFINITION 
	
	GROUP_VARIABLES{
		FAGECL = exp(BETA_CL_AGE * tAGE)
		FSEXCL = if(SEXF == SEXF.female) then POP_FCL_FEM else 1
		GRPCL = ln(POP_CL * FAGECL * FSEXCL ) + BETA_CL_WT * logtWT
		GRPV = POP_V * (WT/70)^BETA_V_WT
	}
	
	INDIVIDUAL_VARIABLES { # This maps to the "Type 2" individual parameter definition in PharmML
	    CL = GRPCL * (ETA_CL)
	    V = GRPV * (ETA_V)
	    KA = POP_KA * (ETA_KA)
	    TLAG = POP_TLAG * (ETA_TLAG) 
	} # end INDIVIDUAL_VARIABLES

	
	MODEL_PREDICTION {
		COMPARTMENT {
			INPUT_KA      : {type is depot, to=CENTRAL, ka=KA, tlag=TLAG}
			CENTRAL       : {type is compartment}
                     	 :: {type is elimination, from=CENTRAL, v=V, cl=CL}
   		}# end COMPARTMENT

	    CC = CENTRAL / V 
	} # end MODEL_PREDICTION
	
	RANDOM_VARIABLE_DEFINITION(level=DV) {
		EPS_Y ~ Normal(mean = 0, var = 1) # This maps the standard error model in PharmML. The error model is predefined. 
	} # end RANDOM_VARIABLE_DEFINITION 

	OBSERVATION {
	    Y : {type is combinedError1, additive = RUV_ADD, proportional = RUV_PROP, eps = EPS_Y, 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_covariate_dat : { type is dataObj }
		warfarin_PK_covariate_prior : { type is priorObj }
		warfarin_PK_covariate_mdl : { type is mdlObj }
		warfarin_PK_ODE_task : { type is taskObj }
	}
}
