z
Home PK intro R NONMEM Calculators Data checker

NM Model

‹ Back
Templates Code snippets

NONMEM model templates and Codes


There are several ways to build a model depending on how you want to test the PK (ADVAN TRANS). Often closed form models available in PREDPP without having to specify all the kinetic parameters (rates between compartents, where what goes, defining compartments etc.) But even the closed formats have some requirements which have to be set by the user, like where is the data, is it log transformed, how are dates and times defined, how is it administered (oral, iv), what error should be used (additive, proportional, both), etc.

Click on lines to open the code

The NONMEM model file

Description and explanation of the basic NONMEM database items.
General info:
The ';' indicates a comment. everything after will be seen as comment.

;; 1. Based on: 000
;; 2. Description: Haloperidol
;; x1. Author: user

Problem description (required):

$PROBLEM PK model


Columns of the dataset
The input section refers to the dataset in what order the columns appear. It does not look at names it will count columns and therefore have to be specified in the right order.
If a column is not to be read by NONMEM it should be explicitely mentioned and followed by '= DROP'. Note that NONMEM will look for the required items in the input.

$INPUT CENSOR AORTA=DROP ID DATE=DROP TIME AMT DV EVID	MDV 


Datafile
The data section defines the name of the dataset (including .csv), the file should be present in the same folder. Ignore refers to the symbol set in front of a row in the dataset that NONMEM should ingore. It this case 'C'. Make sure that if the data contains a header it is preceded by the ignore symbol.

$DATA DATABASE_1.CSV IGNORE=C


PK method
The subroutine determines what model will be used through the choice of ADVAN and TRANS subroutines. These subroutines have their own requirements concerning PK parameters. For an overview of available closed form subroutines see hier.

$SUBROUTINE ADVAN1 TRANS2


The PK parameters
THETAs are the population parameters. ETAs are the individual deviations from the population estimates. Covariates, scaling and other calculations can be defined here as well.

$PK
 CL = THETA(3) * EXP(ETA(1))
 V = THETA(4) * EXP(ETA(2))
 S1 = V/1000


The initial estimates of THETAs and boundries
The first number is the lower boundry, the second the initial estimate and the third the upper boundry.
If only one number is given it refers to the initial estimate, with two the lower bound and initial estimate.

$THETA  
 (0,0.5) 	; 1 prop
 (0,0.005) 	; 2 add
 (0,24)		; 3 CL
 (0,2500) 	; 4 V


The initial estimates of OMEGA (combination of the ETAs) and collinearity

$OMEGA BLOCK(2)
 0.3	   ;; IIV-CL
 0.1 0.03  ;; IIV-V


Residual error
Remaining error not described in the ETAs (the model misspecification).

$SIGMA 
 1  FIX


Error block
In this area the calculations are defined for prediction of the concentration, errors IIV, res var, etc.

$ERROR
 IPRED = F
 IRES = DV-IPRED
 W = IPRED * THETA(1) + THETA(2)
 IF (W.EQ.0) W = 1
 IWRES = IRES/W
 Y= IPRED + W * ERR(1)


Define which estimation method is used
Choose the method for example FOCE, LAPLACIAN, SIMULATION, etc. and additional options.

$ESTIMATION 
 METHOD=1 MAXEVAL=99999 SIG=3 PRINT=5 NOABORT POSTHOC INTERACTION


Covariance step
How and when the covariance step is performed (omega)

$COVARIANCE 	
 PRINT=E UNCONDITIONAL


Table files (output)
Set the output tables, file name and items. Items should be as specified in the NONMEM analysis.

$TABLE     ID IPRED IWRES CWRES NOPRINT ONEHEADER FILE=SDTAB001
$TABLE     ID CL V ETA1 ETA2 NOPRINT ONEHEADER FILE=PATAB001
$TABLE     ID NOPRINT ONEHEADER FILE=COTAB001
$TABLE     ID NOPRINT ONEHEADER FILE=CATAB001



Model templates

1-compartment iv [ADVAN1 TRANS2]

;; 1. Based on: 001
;; 2. Description: drug
;; x1. Author: www.pkpd-info.com

$PROBLEM PK model

$INPUT 	;; Change to dataset
  CENSOR AORTA=DROP ID DATE=DROP TIME AMT EVID MDV TAD DV		 

$DATA datafile.CSV IGNORE=C  

$SUBROUTINES 
  ADVAN1 TRANS2 	;; 1-compartment iv data

$PK
  CL = THETA(3) * EXP(ETA(1))
  V = THETA(4) * EXP(ETA(2))
  
  S1 = V   ;; scaling of compartment check units of dose and observations

$THETA  	;; set realistic initial estimates (also for proportional and additive error)
  (0, 0.1) 	;1 prop
  (0, 0.5)  	;2 add
  (0, 30)    	;3 CL
  (0, 200)   	;4 V

$OMEGA BLOCK(2)
  0.09      		;; IIV-CL
  0.01 0.09  		;; IIV-V

$SIGMA
  1 FIX  ;residual variability

$ERROR  	;; For calculation based on linear (non log-transformed) data
  IPRED = F
  IRES = DV-IPRED
  W = IPRED*THETA(1)+THETA(2)
  IF (W.EQ.0) W = 1
  IWRES = IRES/W
  Y= IPRED+W*ERR(1)

$EST METHOD=1 MAXEVAL=99999 SIG=3 PRINT=5 NOABORT POSTHOC INTERACTION  	;; Estimation method FOCE+interaction

$COV PRINT=E UNCONDITIONAL

$TABLE ID TAD IPRED IWRES CWRES EVID MDV TIME NOPRINT ONEHEADER FILE=SDTAB001
$TABLE ID CL V ETA1 ETA2 NOPRINT ONEHEADER FILE=PATAB001
$TABLE ID NOPRINT ONEHEADER FILE=COTAB001
$TABLE ID NOPRINT ONEHEADER FILE=CATAB001
2-compartiments iv [ADVAN3 TRANS4]

;; 1. Based on: 001
;; 2. Description: drug
;; x1. Author: www.pkpd-info.com

$PROBLEM PK model

$INPUT 	;; Pas aan naar dataset
  CENSOR AORTA=DROP ID DATE=DROP TIME AMT EVID MDV TAD DV		 

$DATA datafile.CSV IGNORE=C 

$SUBROUTINES 
  ADVAN3 TRANS4 		;; data 2-comp (iv)

$PK 				
  LTVCL = LOG(THETA(3))
  MU_1 = LTVCL 			;; MU_1 referencing 
  CL =  EXP(MU_1 + ETA(1))
  
  LTVV1 = LOG(THETA(4))
  MU_2 = LTVV1
  V1 = EXP(MU_2 + ETA(2))
  
  Q = THETA(5)
  V2 = THETA(6)
  
  S1 = V1

$THETA  			;; set realistic initial estimates
  (0, 0.5) 	;1 prop
  (0, 0.1) 	;2 add
  (0, 30)   	;3 CL
  (0, 200)  	;4 V1
  (0, 30)   	;5 Q
  (0, 200)  	;6 V2

$OMEGA BLOCK(2)
  0.09		; IIV-CL
  0.01 0.09	; IIV-V

$SIGMA
  1 FIX  ;residual variability

$ERROR  			;; Based on linear data and proportional and additive error
  IPRED = F
  IRES = DV-IPRED
  W = IPRED*THETA(1)+THETA(2)
  IF (W.EQ.0) W = 1
  IWRES = IRES/W
  Y= IPRED+W*ERR(1)

$EST METHOD=1 MAXEVAL=99999 SIG=3 PRINT=5 NOABORT POSTHOC INTERACTION  	;; Estimation method FOCE+interaction

$COV PRINT=E UNCONDITIONAL

$TABLE ID TAD IPRED IWRES CWRES EVID MDV TIME NOPRINT ONEHEADER FILE=SDTAB001
$TABLE ID CL V1 Q V2 ETA1 ETA2 NOPRINT ONEHEADER FILE=PATAB001
$TABLE ID NOPRINT ONEHEADER FILE=COTAB001
$TABLE ID NOPRINT ONEHEADER FILE=CATAB001

1-compartiment with 1st order absorption [ADVAN2 TRANS2]

;; 1. Based on: 001
;; 2. Description: medicament
;; x1. Author: www.pkpd-info.com

$PROBLEM PK model

$INPUT 	;; Change to dataset
  CENSOR AORTA=DROP ID DATE=DROP TIME AMT EVID MDV TAD DV		 

$DATA datafile.CSV IGNORE=C 

$SUBROUTINES 
  ADVAN2 TRANS2 	;; data 1-comp with first order absorption

$PK
  TVKA = THETA(3)
  KA = TVKA

  TVCL = THETA(4)
  CL = TVCL*EXP(ETA(1))
  MU_1 = LOG(TVCL)

  TVV  = THETA(5)
  V = TVV * EXP(ETA(2))
  MU_2 = LOG(TVV)

  S2 = V  ;; Scaling compartment (check units of dose and observations)

$THETA  	;; set realistic initial estimates
  (0, 0.5) 	;1 prop
  (0, 0.1)  	;2 add
  (0, 4)     	;3 Ka
  (0, 30)    	;4 CL
  (0, 200)   	;5 V

$OMEGA BLOCK(2)
  0.09      		; IIV-CL
  0.01 0.09  		; IIV-V

$SIGMA
  1 FIX  ;residual variability

$ERROR  	;; Calculation based on linear (non log-transformed) data
  IPRED = F
  IRES = DV-IPRED
  W = IPRED*THETA(1)+THETA(2)
  IF (W.EQ.0) W = 1
  IWRES = IRES/W
  Y= IPRED+W*ERR(1)

$EST METHOD=1 MAXEVAL=99999 SIG=3 PRINT=5 NOABORT POSTHOC INTERACTION  	;; Estimation methode FOCE+interaction

$COV PRINT=E UNCONDITIONAL

$TABLE ID TAD IPRED IWRES CWRES  EVID MDV TIME NOPRINT ONEHEADER FILE=SDTAB001
$TABLE ID CL V KA ETA1 ETA2 NOPRINT ONEHEADER FILE=PATAB001
$TABLE ID NOPRINT ONEHEADER FILE=COTAB001
$TABLE ID NOPRINT ONEHEADER FILE=CATAB001
2-compartiments with 1st order absorption [ADVAN4 TRANS4]

;; 1. Based on: 001
;; 2. Description: Drug
;; x1. Author: www.pkpd-info.com


$PROBLEM PK model

$INPUT
  CENSOR AORTA=DROP ID DATE=DROP TIME AMT EVID MDV TAD DV
	
$DATA Dataset.CSV IGNORE=C

$SUBROUTINES 
  ADVAN4 TRANS4 	;; 2-comp with first order absorption

$PK
  TVKA = THETA(3)
  KA = TVKA*EXP(ETA(3))
  MU_3 = LOG(TVKA)

  TVCL = THETA(4)
  CL = TVCL*EXP(ETA(1))
  MU_1 = LOG(TVCL)

  TVV2  = THETA(5)
  V2 = TVV2 * EXP(ETA(2))
  MU_2 = LOG(TVV2)
	
  TVQ = THETA(6)
  Q = TVQ

  TVV3  = THETA(7)
  V3 = TVV3
  
  S2 = V2

$THETA
  (0, 0.5) 	;1 prop
  (0 0.1)  	;2 add
  (0, 4)	;3 Ka
  (0, 30)    	;4 CL
  (0, 50)   	;5 Vc
  (0, 20)	;6 Q
  (0, 200)	;7 Vp
	

$OMEGA BLOCK(3)
  0.09      		; IIV-CL
  0.01 0.09  		; IIV-Vc
  0.01 0.01 0.09	; IIV-Ka

$SIGMA
 1 FIX  ;residual variability

$ERROR
  IPRED = F
  IRES = DV-IPRED
  W = IPRED*THETA(1)+THETA(2)
  IF (W.EQ.0) W = 1
  IWRES = IRES/W
  Y= IPRED+W*ERR(1)

$EST METHOD=1 MAXEVAL=99999 SIG=3 PRINT=5 NOABORT POSTHOC INTERACTION

$COV PRINT=E UNCONDITIONAL

$TABLE ID TAD IPRED IWRES CWRES EVID MDV TIME NOPRINT ONEHEADER FILE=SDTAB003
$TABLE ID CL V2 Q V3 KA ETA1 ETA2 ETA3 NOPRINT ONEHEADER FILE=PATAB003
$TABLE ID WT NOPRINT ONEHEADER FILE=COTAB003
$TABLE ID NOPRINT ONEHEADER FILE=CATAB003
3-compartiments with 1st order absorption [ADVAN12 TRANS4]

;; 1. Based on: 001
;; 2. Description: drug
;; x1. Author: www.pkpd-info.com

$PROBLEM PK model

$INPUT
 CENSOR AORTA=DROP ID DATE=DROP TIME AMT EVID MDV TAD DV

$DATA Dataset.CSV IGNORE=C

$SUBROUTINES 
 ADVAN12 TRANS4 		;; 3-comp with first-order absorption

$PK
  TVKA = THETA(3)
  MU_3 = LOG(TVKA)
  KA = EXP(MU_3+ETA(3))
	
  TVCL = THETA(4)
  MU_1 = LOG(TVCL)
  CL = EXP(MU_1+ETA(1))

  TVV2 = THETA(5)
  MU_2 = LOG(TVV2)
  V2 = EXP(MU_2+ETA(2))

  TVQ3 = THETA(6)
  Q3 = TVQ3

  TVV3  = THETA(7)
  V3 = TVV3
	
  TVQ4 = THETA(8)
  Q4 = TVQ4

  TVV4  = THETA(9)
  V4 = TVV4
  
  SC = V2

$THETA
  (0, 0.5) 	;1 prop
  (0 FIX)  	;2 add
  (0, 4)	;3 Ka
  (0, 30)    	;4 CL
  (0, 50)   	;5 Vc
  (0, 20)	;6 Q
  (0, 100)	;7 Vp
  (0, 10) 	;8 Q4
  (0, 150) 	;9 V4

$OMEGA BLOCK(3)
  0.09      		; IIV-CL
  0.01 0.09  		; IIV-Vc
  0.01 0.01 0.09	; IIV-Ka

$SIGMA
  1 FIX  ;residual variability 

$ERROR
  IPRED = F
  IRES = DV-IPRED
  W = IPRED*THETA(1)+THETA(2)
  IF (W.EQ.0) W = 1
  IWRES = IRES/W
  Y= IPRED+W*ERR(1)

$EST METHOD=1 MAXEVAL=99999 SIG=3 PRINT=5 NOABORT POSTHOC INTERACTION

$COV PRINT=E UNCONDITIONAL

$TABLE ID TAD IPRED IWRES CWRES EVID MDV TIME NOPRINT ONEHEADER FILE=SDTAB001
$TABLE ID CL V2 Q3 V3 Q4 KA ETA1 ETA2 ETA3 NOPRINT ONEHEADER FILE=PATAB001
$TABLE ID WT NOPRINT ONEHEADER FILE=COTAB001
$TABLE ID NOPRINT ONEHEADER FILE=CATAB001


1-compartiment with 1st order absorption [User defined]

;; 1. Based on: 001
;; 2. Description: drug
;; x1. Author: www.pkpd-info.com

$PROBLEM PK model

$INPUT
  CENSOR AORTA=DROP ID DATE=DROP TIME AMT EVID MDV TAD DV CMT

$DATA Dataset.CSV IGNORE=C

$SUBROUTINES
  ADVAN13 TOL=12

$MODEL
  COMP (ABSORB,DEFDOSE)
  COMP (CENTRAL)
  
$PK
  TVKA = THETA(3)
  MU_3 = LOG(TVKA)
  KA = EXP(MU_3+ETA(3))
	
  TVCL = THETA(4)
  MU_1 = LOG(TVCL)
  CL = EXP(MU_1+ETA(1))

  TVV = THETA(5)
  MU_2 = LOG(TVV)
  V = EXP(MU_2+ETA(2))
  
  S2=V
  K20=CL/V

$THETA
  (0, 0.5) 	;1 prop
  (0 FIX)  	;2 add
  (0, 4)	;3 Ka
  (0, 30)    	;4 CL
  (0, 50)   	;5 V

$OMEGA BLOCK(3)
  0.09      		; IIV-CL
  0.01 0.09  		; IIV-Vc
  0.01 0.01 0.09	; IIV-Ka

$DES
  DADT(1) = -KA*A(1)
  DADT(2) = A(1)*KA-A(2)*K20

$SIGMA
  1 FIX  ; residual variability 

$ERROR
  IPRED = F
  IRES = DV-IPRED
  W = IPRED*THETA(1)+THETA(2)
  IF (W.EQ.0) W = 1
  IWRES = IRES/W
  Y= IPRED+W*ERR(1)

$EST METHOD=1 MAXEVAL=99999 SIG=3 PRINT=5 NOABORT POSTHOC INTERACTION

$COV PRINT=E UNCONDITIONAL

$TABLE ID TAD IPRED IWRES CWRES EVID MDV TIME NOPRINT ONEHEADER FILE=SDTAB001
$TABLE ID KA CL V ETA1 ETA2 ETA3 NOPRINT ONEHEADER FILE=PATAB001
$TABLE ID WT NOPRINT ONEHEADER FILE=COTAB001
$TABLE ID NOPRINT ONEHEADER FILE=CATAB001

2-compartiments with 1st order absorption [User defined]

;; 1. Based on: 001
;; 2. Description: drug
;; x1. Author: www.pkpd-info.com

$PROBLEM PK model

$INPUT
  CENSOR AORTA=DROP ID DATE=DROP TIME AMT EVID MDV TAD DV CMT

$DATA Dataset.CSV IGNORE=C

$SUBROUTINES
  ADVAN13 TOL=12

$MODEL
  COMP (ABSORB,DEFDOSE)
  COMP (CENTRAL)
  COMP (PERIPH)
  
$PK
  TVKA = THETA(3)
  MU_3 = LOG(TVKA)
  KA = EXP(MU_3+ETA(3))
	
  TVCL = THETA(4)
  MU_1 = LOG(TVCL)
  CL = EXP(MU_1+ETA(1))

  TVV2 = THETA(5)
  MU_2 = LOG(TVV2)
  V2 = EXP(MU_2+ETA(2))
  
  TVQ = THETA(6)
  Q = TVQ
	
  TVV3 = THETA(7)
  V3 = TVV3
  
  S2=V2
  K20 = CL/V2
  K23 = Q/V2
  K32 = Q/V3
  
$THETA
  (0, 0.5) 	;1 prop
  (0 FIX)  	;2 add
  (0, 4)	;3 Ka
  (0, 30)    	;4 CL
  (0, 250)   	;5 Vc
  (0, 6)	;6 Q
  (0, 600)	;7 Vp

$OMEGA BLOCK(3)
  0.09      		; IIV-CL
  0.01 0.09  		; IIV-Vc
  0.01 0.01 0.09	; IIV-Ka

$DES
  DADT(1)=-KA*A(1)
  DADT(2)=A(1)*KA-A(2)*K20-A(2)*K23 + A(3)*K32
  DADT(3)=A(2)*K23 - A(3)*K32

$SIGMA
  1 FIX  ; residual variability 

$ERROR
  IPRED = F
  IRES = DV-IPRED
  W = IPRED*THETA(1)+THETA(2)
  IF (W.EQ.0) W = 1
  IWRES = IRES/W
  Y= IPRED+W*ERR(1)

$EST METHOD=1 MAXEVAL=99999 SIG=3 PRINT=5 NOABORT POSTHOC INTERACTION

$COV PRINT=E UNCONDITIONAL

$TABLE ID TAD IPRED IWRES CWRES EVID MDV TIME NOPRINT ONEHEADER FILE=SDTAB001
$TABLE ID KA CL V2 Q V3 ETA1 ETA2 ETA3 NOPRINT ONEHEADER FILE=PATAB001
$TABLE ID WT NOPRINT ONEHEADER FILE=COTAB001
$TABLE ID NOPRINT ONEHEADER FILE=CATAB001


2-compartiments with Michaelis Menten absorption [User defined]

;; 1. Based on: 001
;; 2. Description: drug
;; x1. Author: www.pkpd-info.com

$PROBLEM PK model

$INPUT
  CENSOR AORTA=DROP ID DATE=DROP TIME AMT EVID MDV TAD DV CMT

$DATA Dataset.CSV IGNORE=C

$SUBROUTINES
  ADVAN13 TOL=12

$MODEL
  NCOMP = 3
  COMP=(DEPOT,DEFDOSE)
  COMP (CENTRAL)
  COMP (PERIPH)
  
$PK
  TVCL = THETA(3)
  MU_1 = LOG(TVCL)
  CL = EXP(MU_1+ETA(1))

  TVV1 = THETA(4)
  MU_2 = LOG(TVV1)
  V1 = EXP(MU_2+ETA(2))
  
  TVQ = THETA(5)
  Q = TVQ
  
  TVV2 = THETA(6)
  V2 = TVV2
  
  EMAX = THETA(7)
  TMAX = THETA(8)
  
  K20 = CL/V1
  K23 = Q/V1
  K32 = Q/V2
  S2=V1
  
$THETA
  (0, 0.5) 	;1 prop
  (0 FIX)  	;2 add
  (0, 30)    	;3 CL
  (0, 250)   	;4 Vc
  (0, 6)	;5 Q
  (0, 600)	;6 Vp
  (0.1, 10, 100);7 EMAX
  (0.1, 5, 100)	;8 TMAX

$OMEGA BLOCK(2)
  0.09      		; IIV-CL
  0.01 0.09  		; IIV-Vc

$DES
  DADT(1)=-EMAX*A(1)/(TMAX+A(1))
  DADT(2)=EMAX*A(1)/(TMAX+A(1))-K20*A(2)-K23*A(2)+K32*A(3)
  DADT(3)=K23*A(2)-K32*A(3)

$SIGMA
  1 FIX  ; residual variability 

$ERROR
  IPRED = F
  IRES = DV-IPRED
  W = IPRED*THETA(1)+THETA(2)
  IF (W.EQ.0) W = 1
  IWRES = IRES/W
  Y= IPRED+W*ERR(1)

$EST METHOD=1 MAXEVAL=99999 SIG=3 PRINT=5 NOABORT POSTHOC INTERACTION

$COV PRINT=E UNCONDITIONAL

$TABLE ID TAD IPRED IWRES CWRES EVID MDV TIME NOPRINT ONEHEADER FILE=SDTAB001
$TABLE ID CL V1 Q V2 ETA1 ETA2 NOPRINT ONEHEADER FILE=PATAB001
$TABLE ID WT NOPRINT ONEHEADER FILE=COTAB001
$TABLE ID NOPRINT ONEHEADER FILE=CATAB001



Code snippets


Click on lines to open the code


Allometric scaling
Allometric scaling using the standard exponents of 3/4 and 1. These exponents can also be estimated using theta's. The weight is set to 70kg however using the median weight is also common.

$PK
  SIZE = (WT/70)

  TVCL2 =THETA(3)*SIZE**0.75
  CL=TVCL2 * EXP(ETA(1))
  
  TVV2= THETA(4)*SIZE**1
  V2= TVV2
  S2 = V2/1000

  TVCL3 =THETA(5)*SIZE**0.75
  Q=TVCL3

  TVV3= THETA(6)*SIZE**1
  V3= TVV3

Cmax and Tmax
Calculate the Cmax and Tmax. (NOTE: works over the whole time range per ID not just after one dose. Make sure this is what you want! See '$PK' part)

$ABB
  COMRES = 2
  
$PK
  IF (NEWIND.LE.1) THEN
    COM(1) = -1
    COM(2) = -2
  ENDIF	

$DES 
  CONT = A(2)/S2  	;; Set to the correct compartment and scaling
  IF(CONT.GT.COM(1)) THEN
    COM(1) = CONT
    COM(2) = T  
  ENDIF 
 
 $ERROR
  CMAX  = COM(1)
  TMAX  = COM(2) 



Covariates:
Categorical variable using FLAG to switch the covariate Theta on and off. In this example covariate CYP1 in the database is defined as 0 or 1. If CYP1 is anything else than 1 Theta(5) will be 1. If a categorical covariate has just two levels in the dataset (0 and 1) then FLAG can also be replaced by the covariate itself. The effect of the covariate CYP1 is in the example tested on CL.
$PK
  ; Set theta's to match the correct number
  FLAG1 = 0
  IF(CYP1.EQ.1) FLAG1=1   ; CYP1 can be any database defined categorical variate. Check levels
  COV1 = (THETA(n)**FLAG1)  
  
  ;or
  IF(COV.EQ.0) COV1 = 1
  IF(COV.EQ.1) COV1 = (1+THETA(n))
 
  
  ; Add covariate to PK parameter
  TVCL = THETA(3)*COV1
  CL = TVCL*EXP(ETA(1))
Continuous covariate with a linear relationship. The covariate is often centered around the median (for example a BSA of 1.2).
$PK
  ; Set theta's to match the correct number
  ; If you want to use multiple --> COVn+1
  
  COV1 = (COV/median)**THETA(n)	    	; Power function
  COV1 = (1+THETA(n)*(COV-median)     	; Linear function
  COV1 = EXP(THETA(n)*(COV-median)) 	; Exponential function
  
  IF(COV.LE.median) COV1 = (1+THETA(n)*(COV-median)) 		; Hockey stick function
  IF(COV.GT.median) COV1 = (1+THETA(n+1)*(COV-median)) 		; Note that it has two lines and THETAs
  
  
  ; add covariate to your PK parameter
  TVCL = THETA(3)*COV1 
  CL = TVCL*EXP(ETA(1))
  
  
To replace missing data of a covariate by for example the mean an if else code is possible. In the example below '99999' is the value for a missing datapoint and 620 the replacement value.
$PK
  IF(ALB.EQ.99999) THEN
    ALBX = 620
  ELSE
    ALBX = ALB/0.066430
  ENDIF

Error blocks:
For log data with additive error (note: additive for the log-transformed data)
$THETA  
  (0, 0.5)  ; 1 Error defined as a theta (check number)
  
$SIGMA
  1 FIX  ; Residual variability 
   
$ERROR	
  IPRED=LOG(0.0001)
  IF(F.GT.0)IPRED=LOG(F)
  W=1
  IF(F.GT.0)W = SQRT(THETA(1)**2)
  IRES = DV-IPRED
  IWRES = IRES/W
  Y = IPRED+W*EPS(1)
For linear data with proportional and additive error
$THETA  
  (0, 0.1)  ; 1 proportional error as a theta (check number)
  (0, 0.5)  ; 2 additive error as a theta (check number)

$SIGMA
  1 FIX  ;residual variability 
 
$ERROR	
  IPRED = F
  IRES = DV-IPRED
  W = IPRED*THETA(1)+THETA(2)
  IF (W.EQ.0) W = 1
  IWRES = IRES/W
  Y= IPRED+W*ERR(1)
Alternative for linear data with proportional and additive error
$SIGMA  
  0.2   ; prop
  0.5   ; add
 	
$ERROR
  IPRED = F
  W = SQRT(SIGMA(1)*F*F+SIGMA(2))
  IRES = DV-IPRED
  IWRES = IRES/W
  Y = F+F*EPS(1)+EPS(2)

IF ELSE statements
IF ELSE statements in nonmem code (FORTRAN)
; Short statement
IF(AMT.GT.0)FLAG0=1

; Longer statement
IF(AMT.GT.0)THEN
  FLAG0=1
ENDIF

; Multiple arguments require both to be true
; e.g. set flag0 to 1 if time after dose (TAD) > 0 and matrix equals 1
FLAG0=0
IF(TAD.GT.0.AND.MATRIX.EQ.1)FLAG0=1

; Multiple arguments require only one to be true
; e.g. use THETA(2) only for age <1 or >18, else use THETA(2)*THETA(3)
IF(AGE.LT.1.OR.AGE.GT.18)THEN
  CL = THETA(2)
ELSE
  CL - THETA(2)*THETA(3)
ENDIF

; Nested statement
; e.g. go through multiple options
FLAG0=0
IF(TAD.LT.1) THEN
  FLAG0=1
ELSEIF(TAD.LT.5)THEN
  FLAG0=2
ELSE
  FLAG0=3
ENDIF
	


Interoccasion variability (IOV)
Calculate interoccasion variability. The occasions should be defined in the dataset or in the code (e.g. IF (TIME.LT.24) FLAG0 = 1
$PK  ; Define the occassions and turn occasions on for example by using 'FLAGs'
  FLAG0 = 0
  FLAG1 = 0
  FLAG2 = 0
  FLAG3 = 0
  FLAG4 = 0
  FLAG5 = 0

  IF(OCC.EQ.0) FLAG0 = 1
  IF(OCC.EQ.1) FLAG1 = 1
  IF(OCC.EQ.2) FLAG2 = 1
  IF(OCC.EQ.3) FLAG3 = 1
  IF(OCC.EQ.4) FLAG4 = 1
  IF(OCC.EQ.5) FLAG5 = 1

  IOV = FLAG0*ETA(2)+FLAG1*ETA(3)+FLAG2*ETA(4)+FLAG3*ETA(5)+FLAG4*ETA(6)+FLAG5*ETA(7)

  CL = TVCL *EXP(ETA(1)+ IOV)	; E.g test the IOV on CL

$OMEGA 0.127 			; IIV for CL
$OMEGA BLOCK(1) 0.01	; IOV for CL
$OMEGA BLOCK(1) SAME	;
$OMEGA BLOCK(1) SAME	;
$OMEGA BLOCK(1) SAME	;
$OMEGA BLOCK(1) SAME	;
$OMEGA BLOCK(1) SAME	;



M3 method:
M3 method for data LLoQ with proportional and additive error. TYPE should be defined in dataset. In this case TYPE=2 is below LLoQ. For data above LLOQ (using F_FLAG=0) the prediction of the observation is calculated. For data below LLOQ (using F_FLAG=1) the likelihood of the observation is returned.
$THETA 
  (0, 0.1)  ; 1 proportional error as a theta (check number)
  (0, 0.5)  ; 2 additive error as a theta (check number)

$SIGMA
  1 FIX  ;residual variability 
 
$ERROR
  LLOQ = 0.999
  IPRED=F

  W=1
  IF(F.NE.0) W = SQRT((THETA(1)*IPRED)**2+THETA(2)**2)

  IRES = DV-IPRED
  IWRES = IRES/W

  IF(TYPE.EQ.1) THEN
    F_FLAG = 0		
    Y = IPRED+W*EPS(1)
  ENDIF

  IF(TYPE.EQ.2) THEN	
    F_FLAG = 1
    DEL=0.000001							
    DUM = (LLOQ - IPRED)/(W+DEL)
    CUMD = PHI(DUM)
    Y = CUMD
    MDVRES = 1
  ENDIF

$EST 
  METHOD=COND INTER NOABORT LAPLACIAN
  

Time above MIC
Calculate the time above the given MIC. Note that it requires a dataset (e.g. simulation) with small steps in time as it determines the conc>MIC at those time points and uses the interval between current time and previous time.
$ABB COMRES = 4

$PK
  IF (NEWIND.LE.1) THEN					; Resets for new individual
    COM(1) = 0
    COM(2) = 0
    COM(3) = 0
    COM(4) = 0
  ENDIF
  
  MIC = 200 					; Set critical value
	
$DES
  DADT(1)= -ka*A(1)
  DADT(2)= ka*A(1) - A(2)*k20
  CP = A(2)/S2
  DADT(3)= CP
	
  IF(CP.GT.COM(1)) THEN
    COM(1) = CP
    COM(2) = T  
  ENDIF 
  

  IF(CP.GE.MIC)THEN					
    COM(3) = T - COM(4)				; Time above fixed conc
  ENDIF
	
  IF(CP.LT.MIC)THEN					
    COM(4) = T - COM(3)				; Time under fixed conc	
  ENDIF
 
$ERROR 
  AUC = A(3)
  CMAX  = COM(1)
  TMAX  = COM(2)
  CONC = A(2)/S2
	
  TA = COM(3) 				; Cumulative time above MIC
  TB = COM(4) 				; Cumulative time below MIC
  

AUC above MIC
Calculate the AUC when concentration is above and below the given MIC. In this case for a one compartment model with absorption. Also calculates the overall Cmax and Tmax.
$ABB COMRES = 4

$PK
  IF (NEWIND.LE.1) THEN					; Resets for new individual
    COM(1) = 0
    COM(2) = 0
    COM(3) = 0
    COM(4) = 0
  ENDIF
  
  MIC = 200 						; Set critical value
	
$DES
  DADT(1)= -ka*A(1)
  DADT(2)= ka*A(1) - A(2)*k20
  CP = A(2)/S2
  DADT(3)= CP
	
  IF(CP.GT.COM(1)) THEN
    COM(1) = CP
    COM(2) = T  
  ENDIF 
  

  IF(CP.GE.MIC)THEN					
    COM(3) = A(3) - COM(4)		
  ENDIF
	
  IF(CP.LT.MIC)THEN					
    COM(4) = A(3) - COM(3)		
  ENDIF
 
$ERROR 
  AUC = A(3)
  CMAX  = COM(1)
  TMAX  = COM(2)
  CONC = A(2)/S2
	
  AUCMIC = COM(3)			; This is the cum AUC above MIC
  AUCBMIC = COM(4)			; This is the cum AUC below MIC


Transit compartments:
Transit compartments for absorption fixed number of compartments specify different comparments for the drug to transition through. Below an example of seven transit compartments from dose compartment to central (observation) compartment.
$SUBROUTINES 
  ADVAN5 	

$MODEL
  COMP=(TRNS1,DEFDOS)           ;TRANSIT COMP1
  COMP=(TRNS2)                  ;TRANSIT COMP2
  COMP=(TRNS3)                  ;TRANSIT COMP3
  COMP=(TRNS4)                  ;TRANSIT COMP4
  COMP=(TRNS5)                  ;TRANSIT COMP5
  COMP=(TRNS6)                  ;TRANSIT COMP6	
  COMP=(TRNS7)                  ;TRANSIT COMP7	
  COMP=(CIP,DEFOBS)             ;CENTR. COMP. CIP

$PK
  SIZE = (WT/70)
	
  MTT = THETA(2) * EXP(ETA(3))

  TVCL = THETA(3)*(SIZE**0.75)
  CL = TVCL * EXP(ETA(1))

  TVV  = THETA(4)*(SIZE)
  V = TVV * EXP(ETA(2))

  S8 = V  ; make sure to scale the right compartment (observations)

  KTR = 7 / MTT

  K12 = KTR   ; specify the path of the compartments: from 1 to 2, from 2 to 3 etc. 
  K23 = KTR   ; when exceeding 9 compartments use 'T' to separate the numbers of the compartments K20T21 = KTR	
  K34 = KTR
  K45 = KTR
  K56 = KTR
  K67 = KTR
  K78 = KTR
  K80 = CL / V
	
Transit compartments absorption calculate the number of compartments. Requires dummy rows in your dataset (additional rows for the compartment e.g. 1 minute before dose a row for cmt 2 in this case). The code below shows the items for the transit compartments (not full model).
$SUBROUTINES 
  ADVAN6 TOL=6 	;; transit model

$MODEL
  COMP=(ABS) ; transit comp
  COMP=(CENT) ; cent comp

$PK
  IF(AMT.GT.0.AND.CMT.EQ.1)PODO=AMT ; oral dose
  IF(AMT.GT.0)TDOS = TIME  ; if dose is given, use time 
  TAD = TIME - TDOS ; set TAD
 
 ;absorption
  F1 = 0
  F2 = 1
  BIO = THETA(4)
  
  MTT = THETA(6) * EXP(ETA(2))
  N = THETA(7) 
  KTR = (N+1) / MTT
 
  LNFAC = LOG(2.5066)+(N+0.5)*LOG(N)-N    ;logarithmic transformation

$DES
  IF(T.GE.TDOS) THEN
  DADT(1) = EXP(LOG(BIO*PODO+.00001)+LOG(KTR)+N*LOG(KTR*(T-TDOS)+.00001)-KTR*(T-TDOS)-LNFAC)-KA*A(1)
 
  ELSE
  DADT(1) = EXP(LOG(BIO*PODO+.00001)+LOG(KTR)+N*LOG(KTR*T+.00001)-KTR*T-LNFAC)-KA*A(1)  ; dose given
  ENDIF

  DADT(2) = KA*A(1) - K*A(2) ; central comp

  ABSOR = A(1)
  

Random sampling:
Random sampling Sample from distributions during simulations. Call random numbers from pseudo-uniform or pseudo-normal distributions.
$PK
IF (ICALL.EQ.4) 		; Only during simulations ICALL=4
	  CALL RANDOM(2,R)	; Random number between 0-1
	  UNI = R
	  CALL RANDOM(3,R)	; An additional one from different seed
	  UNI2 = R
 ENDIF

; first seed is for eta and eps from var-covar set in $OMEGA and $SIGMA. 
; The second is for the first random call the third seed for the second random call.
; UNIFORM creates a pseudo-uniform distribution (between 0-1). You can also choose NORMAL for pseudo-normal distr (mean 0 var 1)
$SIM (12345) (5678 UNIFORM) (64561 UNIFORM) ONLYSIM SUBPROBLEMS=1000
  
Random sampling per ID Use this code if you want the random number to remain the same per individual per iteration of simulation. In the previous code the random number is different for each row (also within an individual). Here it only changes for a new iteration or individual.

IF (ICALL.EQ.4.AND.NEWIND.NE.2) THEN
   CALL RANDOM(2,R)
   UNI = R						
ENDIF

$SIM (12345) (5678 UNIFORM) (64561 UNIFORM) ONLYSIM SUBPROBLEMS=1000
  
Categorical Random classes Sample categorical random numbers with x number of levels. In this example 5 levels.
$PK
   IF (ICALL.EQ.4) THEN
	CALL RANDOM(2,R)
	BIN=INT(R*5)+1
   ENDIF

 $SIM (12345) (5678 UNIFORM) ONLYSIM SUBPROBLEMS=1000