warfarin_PK_PRED_design = designObj{
  DECLARED_VARIABLES{
    D::dosingTarget
    Y::observation
  }

  INTERVENTION{
          admin1 : {type is bolus, input=D, amount=100, doseTime=0 }
  }

  SAMPLING{
          window1 : {type is simple, sampleTime = [0.0001, 24, 36, 48, 72, 96, 120], outcome=Y }  }

  STUDY_DESIGN{
         arm1 : {
              armSize=33,
              interventionSequence={admin=admin1,start=0},
              samplingSequence={sample=window1,start=0}}
     }
}

warfarin_PK_ODE_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 } 
		} # 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 }
		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_ANALYTIC_mdl = mdlObj {
	IDV { T }

	## logtWT calculation in COVARIATES block supercedes data column of same name
	COVARIATES{
		WT
		logtWT = ln(WT/70)
	}
	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_V_WT
	} # 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)
		ETA_KA ~ Normal(mean = 0, sd = PPV_KA)
		:: {type is correlation, rv1=ETA_CL, rv2=ETA_V, value=CORR_CL_V}
		ETA_TLAG ~ Normal(mean = 0, sd = PPV_TLAG)
	} # end RANDOM_VARIABLE_DEFINITION 
	
	INDIVIDUAL_VARIABLES {
	    CL : {type is linear, trans is ln, pop = POP_CL, fixEff = {coeff=BETA_CL_WT, cov=logtWT} , 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 {
	    D::dosingVar # dosing variable
	    k = CL/V
	    CC = if ( T < TLAG) then 0
	         else (D/V) * KA/(KA-k) * (exp(-k * (T - TLAG))- exp(-KA*(T-TLAG)) )
	} # end MODEL_PREDICTION

	RANDOM_VARIABLE_DEFINITION(level=DV){
		EPS_Y ~ Normal(mean = 0, var = 1) 
	}

	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_PRED_taskEval = taskObj {
	EVALUATE{ 
	}
} # 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
			}
		}
}


#warfarin_PK_PRED_taskEval_Explicit = taskObj {
#	EVALUATE{ 
## Suggested names of common options (options that would make sense to different software)
#        set FIM = "P", # FIMtype ?
#		previous.FIM = "",
#        graphOnly = FALSE,
#        typeIerror = 0.05,
#        powerComparison = FALSE,
#        nsubjectsComparison = F,
#        equivalenceRange = c(log(0.8),log(1.25)),
#        powerEquivalence = FALSE,
#        nsubjectsEquivalence = FALSE,
#        typeIIerror = 0.9
#        
## Graphical options (can be moved to SETTINGS if they only make sense to PFIM)       
#        graph.logical = TRUE,
#        log.logical = FALSE, 
# 		names.datax = c("Time"),
# 		names.datay = c("Concentration"),
#        graph.infA = c(0),
#        graph.supA = c(360),
#        y.rangeA = NULL
#        
#		# Define tool specific settings in block
#		SETTINGS(target = "PFIM"){ # here we explicit all elements of the task
#			set project = "warfarin_PK_PRED",
#			output = "Stdout_warfarin_PK_PRED_eval.R",
#			outputFIM = "",			
#			option = 1,
#			subjects.input = 1,
#			covariate.model = FALSE,
#			covariate_occ.model = FALSE
#			}
#		}
#	}
#} # end of task object

# EVALUATE MOG
warfarin_PK_PRED_mog = mogObj {
	OBJECTS{
		warfarin_PK_PRED_design : { type is designObj }
		warfarin_PK_ANALYTIC_mdl : { type is mdlObj }
		warfarin_PK_ODE_par : { type is parObj }
		warfarin_PK_PRED_taskEval : { type is taskObj }
	}
}
