SAEM_EXTRA_REQUEST and SAEM_EXTRA Reserved Variables (nm760)

Occasionally one wishes to obtain a distribution of any individual parameter defined in the control stream code, after an SAEM analysis is completed, and not just the PHIs/ETAs. As of nm760, a file can be generated during an SAEM ETASAMPLES=1 individual parameter collection process, that collects the posterior distribution of desired parameters, such as individual weighted residuals, as shown here (see example "shapiro_iwr2.ctl"):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
$SIZES ISAMPLEMAX=1100
;#Model Desc: Two compartment Model, Using ADVAN3, TRANS4
;#Project Name: example1
;#Project ID: gm00-001

$PROB RUN# shapiro_iwr2 (from samp5l)
$ABBR DECLARE INTEGER FIRST_WRITE
$INPUT C SET ID JID TIME  DV=CONC AMT=DOSE RATE EVID MDV CMT
       CLX V1X QX V2X SDIX SDSX
$DATA shapiro_iwr2.csv IGNORE=C
$SUBROUTINES ADVAN3 TRANS4

$PK
include nonmem_reserved_general
SAEM_EXTRA_REQUEST=1 ;# Request NONMEM to trigger SAEM-EXTRA events
MU_1=THETA(1)
MU_2=THETA(2)
MU_3=THETA(3)
MU_4=THETA(4)
CL=DEXP(MU_1+ETA(1))
V1=DEXP(MU_2+ETA(2))
Q=DEXP(MU_3+ETA(3))
V2=DEXP(MU_4+ETA(4))
S1=V1

$ERROR
include nonmem_reserved_general
SAEM_EXTRA_REQUEST=1
IPRE=F
IWRES=(DV-IPRE)/IPRE/SQRT(SIGMA(1,1))
Y = F + F*EPS(1)
;# When SAEM_extra=1, then this particular set of individual
;# parameters were "accepted" So you may record them if you wish
IF(SAEM_EXTRA==1 .AND. EVID==0 .AND. FIRST_WRITE==0) THEN
IF(PNM_RUN_MODE==PNM_SINGLE) THEN ;# best if a pnm file with -awnf is used.

" OPEN(UNIT=52,FILE='shapiro_iwr2.iwr')
ELSE ;#If parallel, need to have different file name for each node

" OPEN(UNIT=52,FILE='shapiro_iwr2_'//trim(tfi(pnm_node_number))//'.iwr')
ENDIF
IF(PNM_NODE_NUMBER==1) THEN ;# Only one file should have header, for easy merging later

" WRITE(52,96) 'SAMPLE      ','ID          ','TIME        ','IWRES       '

" 96 FORMAT(A12,1X,A14,2(1X,A12))
ENDIF
FIRST_WRITE=1
ENDIF


IF(SAEM_EXTRA==1 .AND. EVID==0 ) THEN
" WRITE(52,97) SAEM_SAMPLE,ID,TIME,IWRES
" 97 FORMAT(I12,1X,F14.0,2(1X,1PG12.5))
ENDIF

;# Initial values of THETA
$THETA
(2.0) ;#[LN(CL)]
(2.0) ;#[LN(V1)]
(2.0) ;#[LN(Q)]
(2.0) ;#[LN(V2)]

;#INITIAL values of OMEGA
$OMEGA BLOCK(4)
0.15	;#[P]
0.01	;#[F]
0.15	;#[P]
0.01	;#[F]
0.01	;#[F]
0.15	;#[P]
0.01	;#[F]
0.01	;#[F]
0.01	;#[F]
0.15	;#[P]

;#Initial value of SIGMA
$SIGMA
(0.6 )   ;#[P]

$EST METHOD=SAEM NBURN=500 NITER=300 MASSRESET=1 PRINT=20 CTYPE=3
$EST METHOD=SAEM NBURN=0 NITER=0 MASSRESET=0 ETASAMPLES=1 ISAMPLE=1000 EONLY=1

Notice that SAEM_EXTRA and SAEM_EXTRA REQUEST parameters are available. NONMEM will set SAEM_EXTRA to 1 only when ETASAMPLES=1 is set, so you will only get samples outputted during the special period when you want to collect individual parameters with population parameters fixed, so it is not necessary to test for ETASAMPLES=1 in the control stream code.