-
Notifications
You must be signed in to change notification settings - Fork 2
/
day1-04-iterative.jl
415 lines (337 loc) · 10.7 KB
/
day1-04-iterative.jl
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# load libraries
using CSV
using DataFramesMeta
using Dates
using NCA
using NCAUtilities
using NCA.Unitful
using PumasUtilities
using CairoMakie
using PharmaDatasets
using Serialization # used to save and call model fits
## load data
pk_md_data_csv = dataset("nlme_sample.csv", String) # load dataset from PharmaDatasets
pk_data = CSV.read(pk_md_data_csv, DataFrame)
first(pk_data, 6) # display first 6 rows
## map population
pop = read_pumas(
pk_data,
id = :ID,
time = :TIME,
amt = :AMT,
covariates = [:WT, :AGE, :SEX, :CRCL, :GROUP],
observations = [:DV],
cmt = :CMT,
evid = :EVID,
rate = :RATE,
)
########################### 1 COMPARTMENT MODEL ##################
pk_1cmt = @model begin
@metadata begin
desc = "base model: 1comp"
timeu = u"hr"
end
@param begin
"Clearance (L/hr)"
tvcl ∈ RealDomain(lower = 0)
"Volume (L)"
tvvc ∈ RealDomain(lower = 0)
"""
- ΩCL
- ΩVc
"""
Ω ∈ PDiagDomain(2)
"Proportional RUV (variance )"
σ²_prop ∈ RealDomain(lower = 0.0001)
"Additive RUV (variance) "
σ²_add ∈ RealDomain(lower = 0.0001)
end
@random begin
η ~ MvNormal(Ω)
end
@covariates begin
"Dose (mg)"
GROUP
"Sex"
SEX
"Age (years)"
AGE
"Weight (kg)"
WT
"Creatine Clearance"
CRCL
end
@pre begin
CL = tvcl * exp(η[1])
Vc = tvvc * exp(η[2])
end
@dynamics Central1
#Central' = -CL/Vc*Central
@derived begin
cp := @. 1000 * (Central / Vc) #cp is suppressed from the output because of ":="
"""
Drug Concentration (ng/mL)
"""
# additive error model
#DV ~ @. Normal(CONC, sqrt(σ²_add))
# proportional error model
#DV ~ @. Normal(CONC, sqrt(CONC^2*σ²_prop))
# combination error model
DV ~ @. Normal(cp, √(abs(cp)^2 * σ²_prop + σ²_add))
end
end
params_1cmt_comb =
(tvvc = 5, tvcl = 0.2, Ω = Diagonal([0.09, 0.09]), σ²_add = 0.01, σ²_prop = 0.01)
pkfit_1cmt_comb = fit(pk_1cmt, pop, params_1cmt_comb, FOCE())
## Show Model diagnistic criteria (more on day 2)
metrics_pkfit_1cmt_comb = metrics_table(pkfit_1cmt_comb)
inspect_1cmt_comb = inspect(pkfit_1cmt_comb) # get predictions and residuals
inspect_df_1cmt_comb = DataFrame(inspect_1cmt_comb) # convert to a DataFrame
goodness_of_fit(inspect_1cmt_comb) # plot will show you 4 basic goodness of fit plots (more day 2)
infer_1cmt_comb = infer(pkfit_1cmt_comb) # computes variance-covariance matrix CI calculated as the (1-level)/2 and (1+level)/2
coeftable(infer_1cmt_comb) # returns a table with parameter, se, ci lower and ci upper
## look at the influenntial individuals
pk_influential = findinfluential(pk_1cmt, pop, params_1cmt_comb, FOCE())
## Save your fitted model
serialize("pkfit_1cmt_comb.jls", pkfit_1cmt_comb)
## Call your saved fitted model
pkfit_1cmt_comb = deserialize("pkfit_1cmt_comb.jls")
########################### 2 COMPARTMENT MODEL ##################
pk_2cmt = @model begin
@param begin
"Clearance (L/hr)"
tvcl ∈ RealDomain(lower = 0.0001)
"Volume Central Compartment (L)"
tvvc ∈ RealDomain(lower = 0.0001)
"Intercompartmental Clearance (L/h)"
tvq ∈ RealDomain(lower = 0.0001)
"Volume Peripheral Compartment (L)"
tvvp ∈ RealDomain(lower = 0.0001)
"""
- ΩCL
- ΩVc
- ΩQ
- ΩVp
"""
Ω ∈ PDiagDomain(2)
"Additive RUV (variance )"
σ²_add ∈ RealDomain(lower = 0.0001)
"Proportional RUV (variance )"
σ²_prop ∈ RealDomain(lower = 0.0001)
end
@random begin
η ~ MvNormal(Ω)
end
@covariates begin
"Dose (mg)"
GROUP
"Sex"
SEX
"Age (years)"
AGE
"Weight (kg)"
WT
"Creatine Clearance"
CRCL
end
@pre begin
CL = tvcl * exp(η[1])
Vc = tvvc * exp(η[2])
Q = tvq
Vp = tvvp
end
@dynamics Central1Periph1
# Central' = -(CL+Q)/Vc*Central + Q/Vp*Peripheral
# Peripheral' = Q/Vc*Central - Q/Vp*Peripheral
@derived begin
cp := @. Central / Vc
"""
Drug Concentration (ng/mL)
"""
DV ~ @. Normal(cp, √(abs(cp)^2 * σ²_prop + σ²_add)) # using variance
end
end
params_2cmt_comb = (
tvcl = 0.02,
tvvc = 5,
tvq = 0.01,
tvvp = 10,
Ω = Diagonal([0.09, 0.09]),
σ²_add = 0.01,
σ²_prop = 0.01,
)
#
## Maximum likelihood estimation
pkfit_2cmt_comb = fit(pk_2cmt, pop, params_2cmt_comb, FOCE())
## you can quickly check proportional and additive error as well
pkfit_2cmt_add = fit(
pk_2cmt,
pop,
params_2cmt_comb,
constantcoef = (σ²_prop = 0,), # sets a parameter to a fixed value
FOCE(),
)
pkfit_2cmt_prop =
fit(pk_2cmt, pop, params_2cmt_comb, constantcoef = (σ²_add = 0,), FOCE())
# Serialize fits
serialize("pk_fit_2cmt_comb.jls", pkfit_2cmt_comb)
pkfit_2cmt_comb = deserialize("pk_fit_2cmt_comb.jls")
## Show Model diagnstic criteria (more day 2)
metrics_pkfit_2cmt_comb = metrics_table(pkfit_2cmt_comb) # best model
metrics_pkfit_2cmt_add = metrics_table(pkfit_2cmt_add)
metrics_pkfit_2cmt_prop = metrics_table(pkfit_2cmt_prop)
## Compare the estimates with 1-cmt model
compare_estimates(; pkfit_2cmt_comb, pkfit_1cmt_comb)
## compare model metrics of 1 cmt to 2 cmt (more day 2)
df_compartment_comp = innerjoin(
metrics_pkfit_2cmt_comb,
metrics_pkfit_1cmt_comb,
on = :Metric,
makeunique = true,
)
inspect_2cmt_comb = inspect(pkfit_2cmt_comb) # get predictions and residuals
inspect_df_2cmt_comb = DataFrame(inspect_2cmt_comb) # convert to a DataFrame
goodness_of_fit(inspect_2cmt_comb) # plot will show you 4 basic goodness of fit plots (more day 2)
infer_2cmt_comb = infer(pkfit_2cmt_comb) # computes variance-covariance matrix.. CI calculated as the (1-level)/2 and (1+level)/2
coeftable(infer_2cmt_comb) #returns a table with parameter, se, ci lower and ci upper
## Select the two compartment model as our final base model
#################### COVARIATE MODEL BUILDING #######################################
## some basic plots (more day 2)
covariates_check(pop)
covariates_dist(pop)
###################### WEIGHT ##########################################
pk_base_wt = @model begin
@param begin
tvcl ∈ RealDomain(lower = 0.0001)
tvvc ∈ RealDomain(lower = 0.0001)
tvq ∈ RealDomain(lower = 0.0001)
tvvp ∈ RealDomain(lower = 0.0001)
Ω ∈ PDiagDomain(2)
σ²_add ∈ RealDomain(lower = 0.0001)
σ²_prop ∈ RealDomain(lower = 0.0001)
end
@random begin
η ~ MvNormal(Ω)
end
@covariates WT
@pre begin
# wtCL = (WT/70)^0.75
# wtV = (WT/70)
CL = tvcl * (WT / 70)^0.75 * exp(η[1])
Vc = tvvc * (WT / 70) * exp(η[2])
Q = tvq * (WT / 70)^0.75
Vp = tvvp * (WT / 70)
end
@dynamics Central1Periph1
@derived begin
cp := @. Central / Vc
DV ~ @. Normal(cp, √(abs(cp)^2 * σ²_prop + σ²_add))
end
end
params_base_wt = (
tvvc = 5,
tvcl = 0.02,
tvq = 0.01,
tvvp = 10,
Ω = Diagonal([0.09, 0.09]),
σ²_add = 0.01,
σ²_prop = 0.01,
)
#
## Maximum likelihood estimation
pkfit_base_wt = fit(pk_base_wt, pop, params_base_wt, FOCE())
serialize("pkfit_base_wt.jls", pkfit_base_wt)
pkfit_base_wt = deserialize("pkfit_base_wt.jls")
## we again could do all of the post model processing - metrics, plots and compare
###################### CRCL ##########################################
pk_base_wt_crcl = @model begin
@param begin
"Clearance (L/hr)"
tvcl ∈ RealDomain(lower = 0.0001)
"Volume Central Compartment (L)"
tvvc ∈ RealDomain(lower = 0.0001)
"Intercompartmental Clearance (L/h)"
tvq ∈ RealDomain(lower = 0.0001)
"Volume Peripheral Compartment (L)"
tvvp ∈ RealDomain(lower = 0.0001)
"""
- ΩCL
- ΩVc
- ΩQ
- ΩVp
"""
Ω ∈ PDiagDomain(2)
"Additive RUV (variance )"
σ²_add ∈ RealDomain(lower = 0.0001)
"Proportional RUV (variance )"
σ²_prop ∈ RealDomain(lower = 0.0001)
end
@random begin
η ~ MvNormal(Ω)
end
@covariates begin
"Dose (mg)"
GROUP
"Sex"
SEX
"Age (years)"
AGE
"Weight (kg)"
WT
"Creatine Clearance"
CRCL
end
@pre begin
wtCL = (WT / 70)^0.75 # allometric scaling
wtV = (WT / 70)
crcl_eff = (CRCL / 95)^0.75 #posible creatine clearance effect
CL = tvcl * crcl_eff * wtCL * exp(η[1])
Vc = tvvc * wtV * exp(η[2])
Q = tvq * wtCL
Vp = tvvp * wtV
end
@dynamics Central1Periph1
@derived begin
cp := @. Central / Vc
"""
Drug Concentration (ng/mL)
"""
DV ~ @. Normal(cp, √(abs(cp)^2 * σ²_prop + σ²_add))
end
end
params_base_wt_crcl = (
tvvc = 5,
tvcl = 0.02,
tvq = 0.01,
tvvp = 10,
Ω = Diagonal([0.09, 0.09]),
σ²_add = 0.01,
σ²_prop = 0.01,
)
## Maximum likelihood estimation
pkfit_base_wt_crcl = fit(pk_base_wt_crcl, pop, params_base_wt_crcl, FOCE())
serialize("pkfit_base_wt_crcl.jls", pkfit_base_wt_crcl)
pkfit_base_wt_crcl = deserialize("pkfit_base_wt_crcl.jls")
inspect_base_wt_crcl = inspect(pkfit_base_wt_crcl) # get predictions and residuals
inspect_df_base_wt_crclb = DataFrame(inspect_base_wt_crcl) # convert to a DataFrame
goodness_of_fit(inspect_base_wt_crcl) # plot will show you 4 basic goodness of fit plots (more day 2)
infer_base_wt_crcl = infer(pkfit_base_wt_crcl) # computes variance-covariance matrix.. CI calculated as the (1-level)/2 and (1+level)/2
coeftable(infer_base_wt_crcl) #returns a table with parameter, se, ci lower and ci upper
##show all models we have done so far
list_models()
## from here you could select a final model based on metrics and qualify your model
## write a final report
report(
(; final_model = (pkfit_base_wt_crcl, infer_base_wt_crcl, inspect_base_wt_crcl)),
categorical = [:SEX, :GROUP],
date = Dates.now(),
output = "drug_x_report",
clean = false,
title = "Population Pharmacokinetic Analysis for Drug",
author = "Brooke",
version = "v0.1",
header = "Pumas Report",
footer = "ACOP NLME WS",
)
## from here you could further qualify your model then move to nexxxt step of the workflow
## simulate and/or move on to pk-pd etc