# Installation Test
# One compartment Pop PK model (IV bolus, V, K)
#
# Dataset:
# Simulated dataset
#
# Evaluation method for this model:
# Compare results to simulation values:
# V=8, K=0.1, OMEGA_V=0.025, OMEGA_K=0.01, SD_ADD=0.5
#
# Original by Mike K Smith
# Pfizer
# Copyright 2016

Test_dat = dataObj {
   DECLARED_VARIABLES{ INPUT_CENTRAL::dosingTarget Y::observation}
   DATA_INPUT_VARIABLES{
      ID:{use is id}
      AMT:{use is amt, variable=INPUT_CENTRAL}
      TIME:{use is idv}
      DV:{use is dv, variable=Y}
      MDV:{use is mdv}
   }# end DATA_INPUT_VARIABLES

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

Test_design = designObj{
  DECLARED_VARIABLES{
    DOSE::dosingTarget
    Y::observation
  }

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

  SAMPLING{
          window1 : {type is simple, sampleTime = [0.0001, 1, 4, 8, 12, 24, 36], outcome=Y }  }

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

Test_par = parObj{
	STRUCTURAL{
		POP_K : {value=0.1}
		POP_V : {value=8}
	}
	VARIABILITY{
		OMEGA_K : {value=0.1}
		OMEGA_V : {value=0.1}
		SD_ADD  : {value=0.1}
	}
}

Test_prior = priorObj{
  PRIOR_PARAMETERS{
        # prior on "THETA"
		MU_POP_K = 0.1
		MU_POP_V = 8
        VAR_POP_K = 0.3
        VAR_POP_V = 0.3

        # prior on "OMEGA"
		a_OMEGA_K = 0.1
		b_OMEGA_K = 0.1
		a_OMEGA_V = 0.1
		b_OMEGA_V = 0.1
		
		# prior on "SIGMA"
        a_POP_T = 0.1
		b_POP_T = 0.1

   } # end PRIOR_PARAMETERS

   PRIOR_VARIABLE_DEFINITION{
	  # prior on "THETA"     
      lMU_POP_K = ln(MU_POP_K)
      lMU_POP_V = ln(MU_POP_V)		
	  lPOP_K ~ Normal(mean=lMU_POP_K, var=VAR_POP_K)
	  lPOP_V ~ Normal(mean=lMU_POP_V, var=VAR_POP_V)
	  POP_K = exp(lPOP_K)
	  POP_V = exp(lPOP_V)	

	  # prior on "OMEGA"	
      TAU_K ~ Gamma2(shape=a_OMEGA_K, rate=b_OMEGA_K)
      OMEGA_K=1/TAU_K
      TAU_V ~ Gamma2(shape=a_OMEGA_V, rate=b_OMEGA_V)
      OMEGA_V=1/TAU_V 

      # prior on "SIGMA"  
      TAU ~ Gamma2(shape=a_POP_T, rate=b_POP_T)
      SD_ADD = 1/sqrt(TAU)            
    } # end PRIOR_VARIABLE_DEFINITION

} # end of parameter object

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

   STRUCTURAL_PARAMETERS{
      POP_K
      POP_V
   }# end STRUCTURAL_PARAMETERS

   VARIABILITY_PARAMETERS{
      OMEGA_K
      OMEGA_V
      SD_ADD
   }# end VARIABILITY_PARAMETERS


   RANDOM_VARIABLE_DEFINITION(level=ID){
      eta_K ~ Normal( mean=0, var=OMEGA_K)
      eta_V ~ Normal( mean=0, var=OMEGA_V)    
   }# 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}
   }# end INDIVIDUAL_VARIABLES

   MODEL_PREDICTION{   
		COMPARTMENT {
			INPUT_CENTRAL : {type is direct, to = CENTRAL}
			CENTRAL       : {type is compartment}
                     	 :: {type is elimination, from=CENTRAL, v=V, k=K}
   		}# end COMPARTMENT

      CONC=CENTRAL/V
   }# 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= CONC}
   }# end OBSERVATION  

} # end of model object

SAEM_task = taskObj{   
    ESTIMATE { 
    	set algo is saem  
	} 
} # end task

NONMEM_task = taskObj{   
    ESTIMATE { 
    	set algo is focei  
	} 
} # end task

BUGS_task = taskObj{
	ESTIMATE{
		set algo is mcmc
		TARGET_SETTINGS(target="BUGS"){
			set nchains = 1,
			burnin = 4000,
			niter = 10000,
			parameters = "POP_V,POP_K,OMEGA_V,OMEGA_K,SD_ADD",
			winbugsgui="true"
		}
	}
}

EVALUATE_task = taskObj{
	EVALUATE{
	}
	}
	
Test = mogObj{
	OBJECTS{
		Test_dat : { type is dataObj }
		Test_mdl : { type is mdlObj }
		Test_par : { type is parObj }
		SAEM_task : { type is taskObj }
	}
}

