-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPAWS_3.R
233 lines (195 loc) · 8.99 KB
/
PAWS_3.R
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#################################################################################
## ##
## Solutions to hands-on assignments: nlmixr model development ##
## Examine the GOF plots and implement models with ##
## one or transit compartments ##
## ##
## Compare vpcs of alternatives and compare OFVs: ##
## ##
## fitPK001$OBJF-fitPK002$OBJF ##
## ##
#################################################################################
## load the required libraries
library(xpose.nlmixr)
library(nlmixr)
library(RxODE)
library(lattice)
library(data.table)
## read in the Warfarin PK-only data set using data.table syntax (fast and efficient!)
PKdata <- fread("warfarin_PK.csv")
#################################################################################
## ##
## Update the model with a single effect compartment ##
## ##
#################################################################################
## One compartment transit model
One.comp.transit <- function() {
ini({
# Where initial conditions/variables are specified
lktr <- log(1.15) #log k transit (/h)
lcl <- log(0.135) #log Cl (L/h)
lv <- log(8) #log V (L)
prop.err <- 0.15 #proportional error (SD/mean)
add.err <- 0.6 #additive error (mg/L)
eta.ktr ~ 0.5 #IIV ktr
eta.cl ~ 0.1 #IIV Cl
eta.v ~ 0.1 #IIV V
})
model({
cl <- exp(lcl + eta.cl)
v <- exp(lv + eta.v)
ktr <- exp(lktr + eta.ktr)
# RxODE-style differential equations are supported
d/dt(depot) = -ktr * depot
d/dt(central) = ktr * trans - (cl/v) * central
d/dt(trans) = ktr * depot - ktr * trans
## Concentration is calculated
cp = central/v
# And is assumed to follow proportional and additive error
cp ~ prop(prop.err) + add(add.err)
})
}
#################################################################################
## ##
## Run using SAEM ##
## ##
#################################################################################
fitOne.comp.transit_S <-
nlmixr(One.comp.transit,
PKdata,
est = "saem",
saemControl(print = 100),
tableControl(cwres = TRUE))
save(fitOne.comp.transit_S,file="fitOne.comp.transit_S.Rdata")
fitOne.comp.transit_S
vpc_ui(
fitOne.comp.transit_S, #the nlmixr object
n = 500, #number of replicates simulated using estimated parameters and study sampling structure
show = list(obs_dv = TRUE), #additional items to show, like the observations
xlab = "Time (h)", #x-axis label
ylab = "Concentration (mg/L)", #y-axis label
title= "One transit compartment SAEM"
)
#################################################################################
## ##
## Run using FOCEI ##
## ##
#################################################################################
fitOne.comp.transit_F <-
nlmixr(One.comp.transit,
PKdata,
est = "focei",
foceiControl(print = 5))
fitOne.comp.transit_F
save(fitOne.comp.transit_F, file = "fitOne.comp.transit_F.Rdata")
vpc_ui(
fitOne.comp.transit_F, #the nlmixr object
n = 500, #number of replicates simulated using estimated parameters and study sampling structure
show = list(obs_dv = TRUE), #additional items to show, like the observations
xlab = "Time (h)", #x-axis label
ylab = "Concentration (mg/L)", #y-axis label
title = "One transit compartment FOCEI"
)
xpdb.1f <- xpose_data_nlmixr(fitOne.comp.transit_F)
#Absolute values of individual weighted residual vs time
IWRES1<-absval_res_vs_idv(xpdb.1f, #the xpose object
res = "IWRES", #examine absolute values (absval) of individual weighted residuals
idv = "TIME", #as a function of time
caption = NULL) #if not NULL provides the directory where this was run
IWRES1
#################################################################################
## ##
## Update the model with five effect compartments ##
## ##
#################################################################################
## 5 transit compartments
KA1tr5ode <- function() {
ini({
# Where initial conditions/variables are specified
lktr <- log(1.15) #log transit rate constant (/h)
lcl <- log(0.135) #log Cl (L/h)
lv <- log(8) #log V (L)
prop.err <- 0.15 #proportional error (SD/mean)
add.err <- 0.6 #additive error (mg)
eta.ktr ~ 0.5 #IIV ktr
eta.cl ~ 0.1 #IIV cl
eta.v ~ 0.1 #IIV v
})
model({
# Where the model is specified
ktr <- exp(lktr + eta.ktr)
cl <- exp(lcl + eta.cl)
v <- exp(lv + eta.v)
## ODE example
cc=central/v
d/dt(depot)= - ktr*depot
d/dt(central) = ktr*transit5 - cl*cc
d/dt(transit1)= ktr*(depot - transit1)
d/dt(transit2)= ktr*(transit1 - transit2)
d/dt(transit3)= ktr*(transit2 - transit3)
d/dt(transit4)= ktr*(transit3 - transit4)
d/dt(transit5)= ktr*(transit4 - transit5)
## where residual error is assumed to follow proportional and additive error
cc ~ prop(prop.err) + add(add.err)
})
}
nlmixr(KA1tr5ode)
#################################################################################
## ##
## Run using SAEM ##
## ##
#################################################################################
fitKA1tr5ode_S <-
nlmixr(KA1tr5ode,
PKdata,
est = "saem",
saemControl(print = 100),
tableControl(cwres = TRUE))
fitKA1tr5ode_S
save(fitKA1tr5ode_S,file="fitKA1tr5ode_S.Rdata")
vpc_ui(
fitKA1tr5ode_S, #the nlmixr object
n = 500, #number of replicates simulated using estimated parameters and study sampling structure
show = list(obs_dv = TRUE), #additional items to show, like the observations
xlab = "Time (h)", #x-axis label
ylab = "Concentration (mg/L)", #y-axis label
title= "VPC with 5 transit compartments SAEM"
)
fitKA1tr5ode_S$OBJF
#[1] 270.5943
fitOne.comp.transit_F$OBJF
#[1] 321.1425
fitKA1tr5ode_S$OBJF - fitOne.comp.transit_F$OBJF
#[1] -50.5482
#################################################################################
## ##
## Run using FOCEI ##
## ##
#################################################################################
fitKA1tr5ode_F <-
nlmixr(KA1tr5ode,
PKdata,
est = "focei",
foceiControl(print = 20),
tableControl(cwres = TRUE))
fitKA1tr5ode_F
save(fitKA1tr5ode_F,file="fitKA1tr5ode_F.Rdata")
#load(file="fitKA1tr5ode_F.Rdata")
vpc_ui(
fitKA1tr5ode_F, #the nlmixr object
n = 500, #number of replicates simulated using estimated parameters and study sampling structure
show = list(obs_dv = TRUE), #additional items to show, like the observations
xlab = "Time (h)", #x-axis label
ylab = "Concentration (mg/L)", #y-axis label
title= "VPC with 5 transit compartments FOCEI"
)
xpdb.3f <- xpose_data_nlmixr(fitKA1tr5ode_F)
#Absolute values of individual weighted residual vs time
IWRES3<-absval_res_vs_idv(xpdb.3f, #the xpose object
res = "IWRES", #examine absolute values (absval) of individual weighted residuals
idv = "TIME", #as a function of time
caption = NULL) #if not NULL provides the directory where this was run
IWRES3
#One transit compartment vs 5 transit compartments:
fitKA1tr5ode_F$OBJF - fitOne.comp.transit_F$OBJF
#[1] -90.81914