warfarin_PK_SEX_design = designObj{
  DECLARED_VARIABLES{
  	INPUT_KA :: dosingTarget
  	Y ::observation
  	SEX withCategories{female, male}
  	WT
  }

  INTERVENTION{
 	admin1 : {type is bolus, input=INPUT_KA, amount=100, doseTime=[0] }
  }
  SAMPLING{
	window1 : {type is simple, sampleTime = [0.0001, 24, 36, 48, 72, 96, 120], outcome=Y }
  }
  DESIGN_SPACES{
	DS1 : { objRef=[window1], element is sampleTime, discrete=[0.0001, 24, 36, 48,72,96, 120] } 
	DS2 : { objRef=[window1], element is numberTimes, discrete=[4,5] } # define a design space for nb of sampling points
	# not sure what is meant to go here. Perhaps compare to the original example
#	DS3 : { objRef=[admin1], element is sampleTime, discrete=[100, 200, 300] } 
	DS4 : { objRef=[arm1, arm2], element is armSize, discrete=seq(0,40,1) } #the size of an arm can be any (discrete) number between 0 & 40
#	DS5 : { objRef=[armSize], element is armSize, discrete=seq(30,40,1) }  # in total, we want between 30 & 40 subjects
  }
  
  POPULATION{
  	default : { type is template, covariate=[{ catCov=SEX, discreteRv ~ Bernoulli1(probability = 0.5) },
  						{ cov = WT, rv ~ Normal1(mean=piecewise{{ 70 when SEX==SEX.male; otherwise 60 }}, stdev=10) }] }
  	arm2Pop : { type is template, covariate=[{ catCovValue=SEX.female }, { cov = WT, rv ~ Normal1(mean=60, stdev=10) }] }
  }
  
  STUDY_DESIGN{
#  	SEX withCategories{female, male} ~ Bernoulli(probability = 0.5) # Default distribution for SEX (will apply to arm1
#	WTmale ~ Normal(mean=70, variance=10)  # Distribution for WT in males (conditional to value of SEX)
#	WTfemale ~ LogNormal(mean=60, variance=10) # Distribution for WT in females
#	WT : if(SEX==SEX.male) then WTmale
#		 else WTfemale
	arm1 : {
		armSize=16,
		interventionSequence=[{
			admin=admin1,
			start=0
		}],
		samplingSequence=[{
			sample=window1,
			start=0
		}],
		population=default
	}
	arm2 : {
		armSize=17,
		interventionSequence=[{
			admin=admin1,
			start=0
		}],
		samplingSequence=[{
			sample=window1,
			start=0
		}],
		population=arm2Pop
	}
  }
}

warfarin_PK_SEX_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_SEX_mdl = mdlObj {
	IDV { T }
	
	COVARIATES{
		WT
		AGE
		SEX withCategories {female, male} 
		logtWT = ln(WT/70)
		tAGE = AGE - 40
	}

	VARIABILITY_LEVELS(reference=ID){
		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 
	
	INDIVIDUAL_VARIABLES { # This maps to the "Type 3" individual parameter definition in PharmML
	    CL : {type is linear, trans is ln, pop = POP_CL, fixEff = [
	    	  {coeff = BETA_CL_WT, cov = logtWT},
	    	  {coeff = POP_FCL_FEM, catCov = SEX.female },
	    	  {coeff = BETA_CL_AGE, cov = tAGE}
	    	  ], 
			  ranEff = ETA_CL }
	    V : {type is linear, trans is ln, pop = POP_V, fixEff = {coeff=BETA_V_WT, cov=logtWT} , ranEff = ETA_V }
	    KA : {type is linear, trans is ln, pop = POP_KA, ranEff = ETA_KA }
	    TLAG : {type is linear, trans is ln, pop = POP_TLAG, ranEff = ETA_TLAG }
	} # end INDIVIDUAL_VARIABLES

	
	MODEL_PREDICTION {
		COMPARTMENT {
			INPUT_KA      : {type is depot, modelCmt=1, to=CENTRAL, ka=KA, tlag=TLAG}
#			INPUT_CENTRAL : {type is direct, to = CENTRAL}
			CENTRAL       : {type is compartment, modelCmt=2}
                     	 :: {type is elimination, modelCmt=2, 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

# EVALUATE task - implicit version
warfarin_PK_SEX_taskOPT = taskObj {
	OPTIMISE{ 
	}
} # end of task object

# EVALUATE task - explicit version
warfarin_PK_ODE_taskOpt_Explicit = taskObj{ # Here we define explicitly all options for optimisation with PFIM & FW algorithm
        OPTIMISE {
			set optAlgo is simplex,
			fim = "P",
			previousFim = "",
			
# Suggested names of common options (options that would make sense to different software) - Options for methods (what we want the software to compute, 
# and the settings/options for these computations)
        graphOnly = false,
        typeIError = 0.05,
        powerComparison = false,
        nSubjectsComparison = false,
        equivalenceRange = [ln(0.8),ln(1.25)],
        powerEquivalence = false,
        nSubjectsEquivalence = false,
        typeIIError = 0.9,
        
# Graphical options (can be moved to SETTINGS if they only make sense to PFIM)     - Options for graphs   
        graphLogical = true,
        logLogical = false, 
 		namesDataX = ["Time"],
 		namesDataY = ["Concentration"],
		graphInfA=[0],
		graphSupA=[360] /#,
        y.rangeA = NULL #/
        
#			FEATURES{
#				sampleTime,
#				numberTimes=4
#			}
        
		# Define tool specific settings in block
		TARGET_SETTINGS(target = "PFIM"){ # here we explicit all elements of the task
			set project = "warfarin_PK_ODE_optimise",
			output = "Stdout_warfarin_PK_ODE_optimise.R",
			outputFIM = "",			
			option = 1,
			subjectsInput = 1,
			covariateModel = false,
			covariate_occModel = false
			}
		}
}

# EVALUATE MOG
warfarin_PK_SEX_mog = mogObj {
	OBJECTS{
		warfarin_PK_SEX_design : { type is designObj }
		warfarin_PK_SEX_mdl : { type is mdlObj }
		warfarin_PK_SEX_par : { type is parObj }
		warfarin_PK_SEX_taskOPT : { type is taskObj }
	}
}
