-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.qmd
More file actions
613 lines (389 loc) · 11 KB
/
index.qmd
File metadata and controls
613 lines (389 loc) · 11 KB
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
---
title: "Get Started with mrgsolve"
author: "Metrum Research Group"
date: 2022-08-31
format:
html:
toc: true
---
```{r, include = FALSE}
source("src/global.R")
```
# Scope
This document is a very brief introduction to mrgsolve basics. Please see our
main portal at [mrgsolve.org](https://mrgsolve.org) for more resources.
# About `mrgsolve`
- `R` package for simulation from ODE-based models
- Free, OpenSource, GitHub, CRAN
- Language
- Models written in `C++` inside model specification format
- General purpose solver: `ODEPACK` / `DLSODA` (`FORTRAN`)
- Automatically detect and switch between non-stiff (Adams) and stiff (BDF)
methods for solving the differential equations
- Simulation workflow in `R`
- Hierarchical (population) simulation
- `ID`, $\eta$, $\varepsilon$
- Integrated PK functionaility
- Bolus, infusion, `F`, `ALAG`, `SS` etc, handled under the hood
- 1- and 2-cmt PK models in closed-form
- Extensible using `R`, `C++`, `Rcpp`, `boost`, `RcppArmadillo`
- `R` is it's natural habitat
## Background
- Motivation: large bone/mineral homeostatsis model (CaBone)
- History using
- Berkeley Madonna
- WinBUGS
- NONMEM (attempted)
- 2010: write `R` front end to `deSolve`
- 2012: write `C++` interface to `DLSODA`
- Develop dosing / event capability
- More recently, expose functionality provided by
- `Rcpp` - vectors, matrices, functions, environments, random numbers
- `boost` - numerical tools in `C++`
- users' own `C++` code (functions, data structures, classes)
- Translator from `SBML` to `mrgsolve` using `R` bindings to `libSBML`
## Orientation
- https://CRAN.R-project.org/package=mrgsolve
- GitHub site: https://github.com/metrumresearchgroup/mrgsolve
- Issues and questions: https://github.com/metrumresearchgroup/mrgsolve/issues
- mrgsolve website: https://mrgsolve.github.io
- User Guide: https://mrgsolve.github.io/user_guide
- Blog: https://mrgsolve.github.io/blog
- Vignettes: https://mrgsolve.github.io/vignettes
- Compare against NONMEM: https://github.com/mrgsolve/nmtests
## What we will cover today
1. Three basic workflows
1. Loading the model into R
1. Event objects
1. Data sets
Emphasis is on getting you running your own simulations today.
# Three (basic) simulation workflows
```{r setup}
library(tidyverse)
library(mrgsolve)
```
- Single profile
- Batch
- Population
These aren't entirely different, but I like to organize this way. When I'm
planning an simulation, I first think "what type of output do I want?" and the
answer to that question directs me on what to do next.
## Single profile
This is how we load a simulation model into mrgsolve.
Load a two-compartment model from the internal library
```{r}
mod <- modlib("pk2")
```
We now have a 2-compartment PK model with which we can simulate. It is important
to know how this works and we will talk in depth about this. But for now, let's
simulate some stuff.
First, we'll just simulate from this model object (`mrgsim()`)
```{r}
mrgsim(mod)
```
In the output
- Essentially a data frame of simulated data
- First column `ID`
- Second column: `time`
- Next columns: compartments
- Last columns: derived quantities
___Investigate the model object a bit___
- overview
```{r}
```
- parameters
```{r}
```
- compartments
```{r}
```
- outputs
```{r}
```
### Event object
Now, we'll create an "event object" to simulate from. This is just a concise
statement of some intervention. Like a one-liner ... easy to make.
Let's do 100 mg x1 to the first compartment, then simulate:
```{r}
mod %>% ev(amt = 100) %>% mrgsim()
```
We use `ev()` to create a set of intervention(s) for the simulation. Here, it
is just a single 100 mg dose into the first compartment. The event object
looks like this:
```{r}
ev(amt = 100)
```
We have the following columns
1. `time` - whatever is your model time
1. `amt` - whatever is the mass unit for your compartments
1. `cmt` could be number or name
1. `evid` just like nonmem - mostly using 1
You can also use:
- `rate` - infusion
- `ss` - steady state (1 or 2)
- `ii` - interdose interval
- `addl` - additional doses
- `tinf` - infusion time (rather than `rate`)
- `total` - total number of doses (rather than `addl`)
See `?ev`
### Plot
Simulate 100 mg x1 again and now we pipe it to `plot()`
```{r}
mod %>% ev(amt = 100) %>% mrgsim() %>% plot()
```
### Control time span of the simulation
I would like this to look a little nicer.
- 100 mg x1
- Run the end of the simulation out to 72 hours with delta 0.1
- Make the line smoother
- Plot the result
```{r}
mod %>% ev(amt = 100) %>% mrgsim(end = 72, delta = 0.1) %>% plot()
```
We can make this change permanent
- end: 72 hours
- delta: 0.1 hours
```{r}
mod2 <- update(mod, end = 72, delta = 0.1)
```
### More-complicated events
We said that the `event` objects were simple. But we can combine them to make
more complicated sequences.
Let's load a PK model for azithromycin (`azithro-single`):
```{r}
mod <- mread("azithro-single", project = "model")
```
__Check out the model__
```{r}
mod
```
Create an event object to implement the z-pak dose:
- 500 mg po on day 1 (`load`)
- 250 mg po daily on days 2 through 5 (`continue`)
```{r}
load <- ev(amt = 500)
continue <- ev(amt = 250, ii = 24, addl = 3, time = 24)
zpak <- c(load, continue)
```
Look at the zpak dosing object
```{r}
zpak
```
We can also accompilsh this just with 250 mg tablets
```{r}
zpak <- c(ev(amt = 250), ev(amt = 250, ii = 24, addl = 4))
zpak
```
Now, simulate and plot from the zpak event object
```{r}
mrgsim(mod, zpak) %>% plot()
```
### Event sequence
- 100 mg daily x 7 __then__
- 50 mg BID x7
```{r}
```
## Batch
Let's use our fixed-effects azithromycin model to look at how weight affects
PK. We'll use that `zpak` object that we created in the previous section.
### Create an idata set
Now, let's make a data frame that contains the weights that we want to
investigate (from 40 kg to 140 kg by 10 kg).
```{r}
wt <- tibble(WT = seq(40, 140, 10))
head(wt)
```
__IMPORTANT__: the key here is that we have `WT` as a column in the data set
and we have `WT` as a parameter in the model (look at the parameters)
```{r}
param(mod)
```
When we make the names agree, mrgsolve will update the `WT` parameter as the
simulation advances across individuals.
### Simulate with event object
Now we can pass this set of weights into the problem as "idata". We will use
just the first record from the zpak dosing object.
- Load the `azithro-single` model
- Create a dosing event with 500 mg x1
- Simulate with `idata`
- End the simulation at 96 hours
```{r}
mod <- mread("azithro-single", project = "model")
load <- ev(amt = 500)
out <- mrgsim(mod, events = load, idata = wt, end = 96)
```
Take a quick look at the output (`head`)
```{r}
out
```
Plot the ouptut, looking only at `CP` and on log scale
```{r}
plot(out, CP ~ time, logy = TRUE)
```
This idata set functionality is typically used with an event object
(as we have here) but isn't required.
## Population
The last workflow I'm calling "population". Here, population just refers to
the input data set.
We can have a data frame that contains many individuals with all different
types of dosing interventions. This is just like the data set that you use
to do your NONMEM run.
### Read a NMTRAN like data set
Meropenem PopPK
http://repository.ddmore.foundation/model/DDMODEL00000213
For example, read in `data/meropenem.csv`
```{r}
data <- readr::read_csv("data/meropenem.csv", na = '.')
```
- glimpse the data (`head`)
- count `EVID`, `DUR`, `AMT`
- number of IDs
```{r}
head(data)
count(data, EVID, DUR, AMT)
length(unique(data$ID))
```
Now, load the meropenem model (`meropenem_pk.cpp`, in the `model` directory)
```{r}
mod <- mread("meropenem_pk", project = "model")
```
And simulate with `mod` and `data`
```{r}
out <- mrgsim(mod, data)
```
Then plot using `Y` output
```{r}
plot(out, Y ~ TIME)
```
Resimulate and plot by duration
```{r}
out <- mrgsim(mod, data, carry_out = "DUR")
```
Plot `Y` versus `TIME` by `DUR`
```{r}
plot(out, Y ~ TIME | DUR)
```
Recall that we have both observations and doses in the data set (as usual
for your NONMEM data set)
Count `EVID` in `data`
```{r}
count(data, EVID)
```
When mrgsolve finds records with `EVID=0` in the data set, it will assume that
you have specified every time that you want a simulated value in the data set.
In other words the design of the simulated output will match the design of the
input data:
Check `dim()` in `data` and `out`:
```{r}
dim(data)
dim(out)
```
Let's look to see what happens when we don't include any observation records
in the input data:
Filter into `doses`:
```{r}
doses <- filter(data, EVID==1)
```
Now we still have the same number of people, with different doses and infusion
times.
Check unique `ID` and count `EVID`, `AMT`, and `DUR`
```{r}
length(unique(doses$ID))
count(doses, EVID, AMT, DUR)
```
- Simulate from this sparse data set; end = 8 hours
- Get `DUR` in to the output
- Plot `log(Y)` versus `time` by `DUR`
```{r}
mod %>%
mrgsim(doses, carry_out = "DUR", end = 8) %>%
plot(Y~TIME|factor(DUR), logy=TRUE)
```
The principle is: when mrgsolve does NOT find any observation records, it will
fill them in for you according to the time grid that we looked at previously.
This can be very helpful in reducing the data assembly burden when running your
simulations.
### Some other ways to create population inputs
- `expand.ev()` makes all combinations of your inputs
- Sensible defaults are provided
- `time`, `cmt`, `evid`
```{r}
data <- expand.ev(amt = c(100, 300, 1000), ii = c(12, 24))
data
```
- `as_data_set()` takes event objects and creates a data frame / set
- 100 mg and 300 mg doses daily x7
- 20 individuals each
```{r}
data <- as_data_set(
ev(amt = 100, ii = 24, total = 7, ID = 1:20),
ev(amt = 300, ii = 24, total = 7, ID = 1:20)
)
```
# Get work done
## Work with output
- `names()`
- `summary()`
- `head()`
- `$`
```{r}
mod <- modlib("pk1")
out <- mrgsim(mod)
names(out)
summary(out)
head(out)
out$time[1:5]
```
## Coerce output
- data.frame
- tibble
- matrix
```{r}
out <- mrgsim(mod)
class(out)
as_tibble(out)
```
## Corerce via dplyr verbs
- Simulate and pipe the output to `mutate()`
```{r}
out <- mrgsim(mod) %>% mutate(name = "kyle")
class(out)
head(out)
```
## Return data frame
- Use `output` argument
```{r}
out <- mrgsim(mod, output = "df")
class(out)
```
- Use `mrgsim_df()`
```{r}
out <- mrgsim_df(mod)
class(out)
```
## Carry-Out
- 100 mg x1
- need `dose` in the output
- contrast that with getting `amt` in the output
First create the event
```{r}
e <- ev(amt = 100, dose = amt)
e
```
Then recover `dose`
```{r}
mrgsim(mod, events = e, carry_out = "dose")
```
## Recover
- `dose`: 100 mg
- `trt`: 100 mg x1
- need `dose` and `trt` in the output
First, create the event
```{r}
e <- ev(amt = 100, trt = "100 mg x1", dose = amt)
e
```
Then simulate and recover `trt` and `amt`
```{r}
mrgsim(mod, events = e, recover = "trt,amt")
```