# Prior_Example3312 : All parameters are independent but some of them do not have prior distribution 
# Dosing regimen: single IV bolus administration (at time 0)
# Structural model : 1 compartment model (V, K)
# 1 cmpt (V) + linear elimination (K)
# C(t) = Dose / V*exp(-K*t)
# Variability model: 
#   - IIV on V, K.
##    ETA_K ~ Normal(mean, stdev); ETA_V ~ Normal(mean, stdev)
#   - Additive residual error model
#     NB: Each individual has a different residual variability 
#########
#########
## PRIOR SPECIFICATION:
## Population mean parameters:
## POP_K ~ LogNormal(MU_POP_K, VAR_POP_K)
## POP_V ~ LogNormal(MU_POP_V, VAR_POP_V)
## POP_T ~ Gamma(a_POP_T, b_POP_T)
##
## Population between subject variabilities:
## OMEGA_K ~ InverseGamma(a_TAU_K, b_TAU_K)
## OMEGA_V ~ Gamma(a_TAU_V, b_TAU_V)
## OMEGA_T ~ Gamma(a_TAU_T, b_TAU_T)
## 
## Residual error
## (1/sqrt(SD_ADD)) ~ Gamma(a_POP_T, b_POP_T)
#########

example331_dat = dataObj {
    DECLARED_VARIABLES{ DOSE::dosingTarget Y::observation}

   DATA_INPUT_VARIABLES{
      ID:{use is id}
      AMT:{use is amt, variable=DOSE}
      TIME:{use is idv}
      DV:{use is dv, variable=Y}
      MDV : {use is mdv}
   }# end DATA_INPUT_VARIABLES

   SOURCE{
   mysrcfile:{file="TestData.csv",
      inputFormat is nonmemFormat}
   }# end SOURCE
}

example3312_pri = priorObj{
  PRIOR_PARAMETERS{
        # prior on "THETA"
		MU_POP_K = 0.1
        VAR_POP_K = 0.1

        # prior on "OMEGA"
		a_OMEGA_K = 0.1
		b_OMEGA_K = 0.1

# Not sure how represent with MDL this situation. I will skip that for the moment 
#		POP_V = 1
#		OMEGA_V = 0.01
#		POP_T = 1
#		OMEGA_T = 0.01

   } # end PRIOR_PARAMETERS

   PRIOR_VARIABLE_DEFINITION{

	  # prior on "THETA"     
      lMU_POP_K = ln(MU_POP_K)
	  lPOP_K ~ Normal(mean=lMU_POP_K, var=VAR_POP_K)
	  POP_K = exp(lPOP_K)

	  # prior on "OMEGA"	
      TAU_K ~ Gamma2(shape=a_OMEGA_K, rate=b_OMEGA_K)
      OMEGA_K=1/TAU_K       
    } # end PRIOR_VARIABLE_DEFINITION
    
} # end of prior object

example331_mdl = mdlObj{
	IDV{ T }
	
	VARIABILITY_LEVELS{
		ID:{level=2, type is parameter}
		DV:{level=1, type is observation}
	}

# <<<< Relevant model parameters are selected from the PRIOR_VARIABLE_DEFINITION block  
# <<<< and listed in the STRUCTURAL_PARAMETERS and VARIABILITY_PARAMETERS blocks as usual >>>
   STRUCTURAL_PARAMETERS{
      POP_K
      POP_V
      POP_T
   }# end STRUCTURAL_PARAMETERS

   VARIABILITY_PARAMETERS{
      OMEGA_K
      OMEGA_V
	  OMEGA_T
   }# end VARIABILITY_PARAMETERS


   RANDOM_VARIABLE_DEFINITION(level=ID){
      eta_K ~ Normal( mean=0, var=OMEGA_K)
      eta_V ~ Normal( mean=0, var=OMEGA_V)
      eta_T ~ Normal( mean=0, var=OMEGA_T)
   }# end RANDOM_VARIABLE_DEFINITION

   INDIVIDUAL_VARIABLES{
      K : {type is linear, trans is ln, pop = POP_K, ranEff=eta_K}
      V : {type is linear, trans is ln, pop = POP_V, ranEff=eta_V}
      TAU : {type is linear, trans is ln, pop = POP_T, ranEff=eta_T}
      SD_ADD = 1/sqrt(TAU)
   }# end INDIVIDUAL_VARIABLES

   MODEL_PREDICTION{   
      DOSE :: dosingVar
      C=DOSE/V*exp(-K*T)
   }# end MODEL_PREDICTION   
   
   RANDOM_VARIABLE_DEFINITION(level=DV){
   		eps ~ Normal( mean=0, var=1)
   	}
   
   OBSERVATION{
      Y : {type is additiveError, additive=SD_ADD, eps=eps, prediction= C}
   }# end OBSERVATION  

} # end of model object

example3312_task = taskObj{   
    ESTIMATE { 
        set algo is mcmc
		TARGET_SETTINGS(target="BUGS"){
		set 
		nchains = 1,
	    burnin = 1,
		niter = 1,
		parameters = "POP_K, POP_V, OMEGA_K, OMEGA_V, SD_ADD, V,K",
		winbugsgui ="true"
		}
		}
} # end task

example331 = mogObj{
	OBJECTS{
		example331_dat : { type is dataObj }
		example331_mdl : { type is mdlObj }
		example3312_pri : { type is priorObj }
		example3312_task : { type is taskObj }
	}
}