# Nathan (Method 2 Moller 2014)
# Linear Regression Model 
# for each subject HBA1C at steady state is estimate using MPG 
# MPG = 28.7*HBA1C - 46,7
# HBA1C = (1/28.7)*MPG + (46.7/28.7) 
# HBA1C = 0.035*MPG + 1.63 

# model used for estimation
# compatibility for NONMEM e MONOLIX

# Limitation:
# EVID encoded as a covariate in the data object

# Dataset:
# simulated dataset (according to the original one) was used 
#
# Evaluation method for this model:
# run NONMEM estimation via TEL
# run MONLIX estimation via TEL
#
# By Elisa Borella, Letizia Carrara, Silvia Lavezzi, Paolo Magni, Giulia Muzio, Lorenzo Pasotti, Elena Tosca
# BMS lab - Univ Pavia
# Copyright 2015
#
# THIS IS A BAYESIAN VERSION OF THE MODEL


Method_2_Nathan_data = dataObj{

DECLARED_VARIABLES{Y::observation}

   DATA_INPUT_VARIABLES{
       
      ID:{use is id}
      TIME:{use is idv}
      DV:{use is dv, variable=Y}
      MPG : {use is covariate}
	  EVID : {use is  evid}
   }
   
   SOURCE{
       srcfile : { 
       file="Simulated_Nathan_data.csv",  
       inputFormat is nonmemFormat
       }
   }
}

Method_2_Nathan_pri = priorObj{
	
	PRIOR_PARAMETERS{
	  MU_BETA0_POP = 1.5
      MU_BETA1_POP = 0.05
      
      # standard deviations
      SIGMA_BETA0_POP = 1.5
      SIGMA_BETA1_POP = 0.05

	  #so that IC95% for RES: 0.025-0.15; mean_gamma=a/b, var_gamma=a/(b^2); to change if needed
      a_RES2 = 1.2
      b_RES2 = 0.0021
	}
	
	PRIOR_VARIABLE_DEFINITION{
	  BETA0_POP ~ Normal(mean=MU_BETA0_POP, sd=SIGMA_BETA0_POP)
	  BETA1_POP ~ Normal(mean=MU_BETA1_POP, sd=SIGMA_BETA1_POP)
	  invRES2 ~ Gamma1(shape=a_RES2, scale=b_RES2)
	  RES = sqrt(1/invRES2)
	}
}


Method_2_Nathan_par = parObj{
   STRUCTURAL{   
   BETA0_POP : {value=1.63} 
   BETA1_POP : {value= 0.035} 
   RES : {value = 1, lo=0}
   	}
}


Method_2_Nathan_mdl = mdlObj{

 	IDV {T}
 
	STRUCTURAL_PARAMETERS{
 		BETA0_POP
 		BETA1_POP
 		RES
	}
	
	INDIVIDUAL_VARIABLES{
	 	BETA0 = BETA0_POP
 		BETA1 = BETA1_POP
	}
	
	COVARIATES{
		MPG
	}
 
    VARIABILITY_LEVELS{
   		DV : { level=1, type is observation }
   }
  
  	RANDOM_VARIABLE_DEFINITION(level=DV){
  		EPS ~ Normal(mean=0, var=1)
  	}

  
	MODEL_PREDICTION {
    	HBA1C = BETA0 + BETA1*MPG
	}	
   
   
   OBSERVATION {	
		Y : { type is additiveError, additive = RES, eps = EPS, prediction = HBA1C }
   }
 }
 
Method_2_Nathan_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
        }
   }
}
	
Method_2_Nathan_mog =mogObj {
	OBJECTS{
	Method_2_Nathan_data : {type is dataObj}
	Method_2_Nathan_pri : {type is priorObj}
	Method_2_Nathan_mdl : {type is mdlObj}
	Method_2_Nathan_task : {type is taskObj}
	}
}
