forked from kylebaron/learn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeck_01.Rmd
609 lines (396 loc) · 10.3 KB
/
deck_01.Rmd
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
---
title: "Introduction to <grn>mrgsolve</grn><BR>Models and Events"
author: Kyle T. Baron
date: Metrum Research Group, LLC
output:
slidy_presentation:
number_sections: true
css: [styles/slidystyles.css,styles/colors.css]
html_document:
theme: united
number_sections: true
toc: true
css: [styles/htmlstyles.css,styles/colors.css]
---
```{r, include = FALSE}
library(mrgsolve)
library(dplyr)
library(knitr)
library(lattice)
opts_chunk$set(comment = '.', fig.height = 5, fig.width = 9)
tryit_file <- "workbook.Rmd"
```
```{r setup, echo = FALSE, message = FALSE}
mod <- mread_cache("pk1", modlib()) %>%
update(end = 192, delta = 0.2) %>% Req(CP)
data(exidata)
data <- filter(exidata, ID <=10)
set.seed(1222)
```
# A basic simulation with mrgsolve
```{r}
mod %>% ev(amt = 100, ii = 24, addl = 3) %>% mrgsim() %>% plot()
```
# A basic simulation with mrgsolve
```{r, eval = FALSE}
mod %>% ev(amt = 100, ii = 24, addl = 3) %>% mrgsim() %>% plot()
```
- <red>mod</red>: the model object
- Ok ... where did `that` come from?
- <blu>ev(amt = 100, ...)</blu> : the intervention
- An `event` in this example
- <grn>mrgsim()</grn>: actually do the simulation
- <orng>plot()</orng>: do something with the simulation
- `plot`, `mutate`, `as_data_frame` etc ...
> - <red>model</red> `%>%` <blu>intervention</blu> `%>%` <grn>Go!</grn> `%>%` <orng>take-a-look</orng>
# What's coming ...
> - `model %>% intervention %>%` <alrt>options</alrt> `%>% Go! %>% ...`
> - `model %>% intervention %>%` <orng>population</orng> `%>% Go! %>% ...`
> - `model %>%` <purp>data-set</purp> `%>% Go! %>% ...`
> - where <purp>data-set</purp> = `intervention + population`
> - For now, let's get this part down
> - <red>model</red> `%>%` <blu>intervention</blu> `%>%` <grn>Go!</grn> `%>%` <orng>take-a-look<orng>
# Why do we use `%>%` ?
What happens first in this operation?
```{r, eval = FALSE}
mean(sqrt(seq(4)))
```
# Pipelines
```{r, eval = FALSE}
mean(sqrt(seq(4)))
```
```{r, eval = FALSE}
4 %>% seq() %>% sqrt() %>% mean()
```
Better.
```{r, eval = FALSE}
4 %>%
seq(.) %>%
sqrt(.) %>%
mean(., na.rm = TRUE)
```
```{r,eval = FALSE}
mod %>% some_intervention() %>% simulate() %>% post_process()
```
# The model object
<red>mod</red><blk>el</blk> `%>% ... `
> - I (almost) always call the model object <red>mod</red> in the documention / examples
> - All the information about the model we need to know to run the simulation
> - <grn>Distinct</grn> from the intervention, the population, the summary
> - But the model <orng>does</orng> know about output time, random effects
# Take a look: overview
```{r, comment = ""}
mod
```
# Take a look: parameters (<red>really important</red>)
```{r, comment = ""}
param(mod)
```
> - Parameters get a name
> - Names and number of parameters gets fixed at compile time
> - All parameters have a value
> - Value can be modified after compile time
# Take a look: compartments
```{r, comment = ""}
init(mod)
```
> - Every compartment gets a name
> - Every compartment gets an initial condition
# Where did <red>mod</red> come from?
```{r,eval = FALSE}
mod <- mread("simple", "model")
```
```{r, eval = FALSE}
mod <- mread("<model-name>", "<project-directory>")
```
- By default mrgsolve looks for the code in the file
- <grn>model-name.cpp</grn> in
- <blu>project-directory</blu>
- <red>mread demo</red>
```{r, echo = FALSE}
Sys.sleep(2)
```
# Read in a model object with caching
First time to read
```{r}
mod <- mread_cache("simple", "model")
```
Next time to read
```{r}
mod <- mread_cache("simple", "model")
```
# Oops
```{r}
mod <- try(mread("simple"))
mod
```
# Internal model library
Quiz:
```{r, eval = FALSE}
mod <- mread("<first-argument>", "<second-argument>")
```
# Internal model library
Quiz:
```{r, eval = FALSE}
mod <- mread("<first-argument>", "<second-argument>")
```
```{r, eval = FALSE}
mod <- mread("<first-argument>", modlib())
```
```{r}
modlib()
```
# Internal model library
```{r}
mod <- mread("effect", modlib())
```
```{r}
mod
```
# Inline model specification
We <red>haven't covered</red> the specifics of coding a model yet
```{r}
code <- '
$PARAM CL = 1, V = 20
$PKMODEL cmt = "CENT"
'
```
```{r, eval = FALSE}
mod <- mcode("dont_do_this", code)
```
```{r, eval = FALSE}
mod <- mcode_cache("seriously_dont", code)
```
> - <font class = "red">Question:</font> Why is this a bad idea?
# <red>Your turn</red>
- File name: <grn>`r tryit_file`</grn>
- Section name: <blu>Warm Up</blu>
# Event objects
- `model %>%` <red>intervention</red> `%>% Go! %>% take-a-look`
- Event object = quick / easy way to implement dose or other intervention
```{r}
e <- ev(amt = 100)
e
```
> - Defaults: `time`, `evid`, `cmt`
# Three ways to invoke
<blk>Inline</blk>
```{r, eval = FALSE}
mod %>% ev(amt = 100) %>% mrgsim()
```
<blk>Object via pipeline</blk>
```{r, eval = FALSE}
e <- ev(amt = 100)
mod %>% ev(e) %>% mrgsim()
```
<blk>As argument</blk>
```{r, eval = FALSE}
mod %>% mrgsim(events = e)
```
# What to include in `ev(...)`
- <grn>time</grn> event time
- <grn>cmt</grn> Event compartment
- <grn>amt</grn> Dose amount
- <grn>ii</grn> Inter-dose interval
- <grn>addl</grn> Additional doses to administer
- <grn>rate</grn> Infusion rate
- <grn>ss</grn> Set to 1 to advance to steady state
- <grn>evid</grn> Event id
- <grn>ID</grn> Subject ID (use multiple ids - ID=1:10)
# Interventions and corresponding `evid`
- <grn>Bolus</grn> dosing (`evid` 1, with `rate`==0)
- Zero order <grn>infusion</grn> (`evid` 1, with `rate` > 0)
- <grn>Other</grn> type event (`evid` 2)
- This also forces solver reset
- Compartment <grn>reset</grn> (`evid` 3)
- <grn>Reset</grn> and dose (`evid` 4)
- <grn>Replace</grn> the amount in a specific compartment (`evid` 8)
# Create complex events - 1
What's going to happen?
```{r, eval = FALSE}
e1 <- ev(amt = 200)
e2 <- ev(amt = 100, time = 24, ii = 24, addl = 4)
c(e1, e2)
```
# Create complex events - 1
What's going to happen?
```{r, echo = FALSE}
e1 <- ev(amt = 200)
e2 <- ev(amt = 100, time = 24, ii = 24, addl = 4)
e <- c(e1,e2)
mod %>%
ev(e) %>%
mrgsim(end = 96) %>% plot(CP~.)
```
# Create complex events - 1
Combine two events
```{r}
e1 <- ev(amt = 200)
e2 <- ev(amt = 100, time = 24, ii = 24, addl = 4)
c(e1, e2)
```
# Create complex events - 2
What's going to happen?
```{r, eval = FALSE}
e1 <- ev(amt = 200, ii = 12, addl = 2)
e2 <- ev(amt = 100, ii = 24, addl = 4)
seq(e1, e2)
```
# Create complex events - 2
What's going to happen?
```{r, echo = FALSE}
e1 <- ev(amt = 200, ii = 12, addl = 2)
e2 <- ev(amt = 100, ii = 24, addl = 4)
e <- seq(e1, e2)
mrgsim(mod, events = e, end = 180) %>% plot(CP ~ .)
```
# Create complex events - 2
Put two events in a sequence
```{r}
e1 <- ev(amt = 200, ii = 12, addl = 2)
e2 <- ev(amt = 100, ii = 24, addl = 4)
seq(e1, e2)
```
# Create complex events - 3
What is going to happen?
```{r, eval = FALSE}
e1 <- ev(amt = 200)
e2 <- ev(amt = 100, ii = 24, addl = 4)
seq(e1, wait = 36, e2)
```
# Create complex events - 3
What is going to happen?
```{r, echo = FALSE}
e1 <- ev(amt = 200)
e2 <- ev(amt = 100, ii = 24, addl = 4)
e <- seq(e1, wait = 36, e2)
mrgsim(mod, events = e, end = 180) %>% plot(CP ~ .)
```
# Create complex events - 3
Wait before starting the next part of the regimen
```{r}
e1 <- ev(amt = 200)
e2 <- ev(amt = 100, ii = 24, addl = 4)
seq(e1, wait = 36, e2)
```
# <red>Your turn</red>
- File name: <grn>`r tryit_file`</grn>
- Section name: <blu>Z-Pak</blu>
# Event objects are just data frames
```{r}
as.data.frame(e1)
```
- We will use a <blk>data_set</blk> later on for populations
- Event objects are convenient
- Constructor
- Operations
# We're stil working on this setup
<hr>
<red>model</red> `%>%` <blu>intervention</blu> `%>%` <grn>Go!</grn> `%>%` <orng>take-a-look</orng>
<hr>
<red>model</red>:
- Load a model with `mread` or `mread_cache`
- Use the internal library with `mread("<model-name>", modlib())`
- Check model parameters with `param(mod)`
- Check model initial conditions with `init(mod)`
- View model code with `see(mod)`
<blu>intervention</blu>:
- `ev(...`): `amt`, `cmt`, `time`, `ii`, `addl`, `rate`
- Different ways to combine event objects
# Simulate
<hr>
`model %>% intervention %>%` <grn>Go!</grn>
<hr>
```{r}
mod %>% ev(e) %>% mrgsim()
```
```{r, eval = FALSE}
mrgsim(mod, events = e)
```
# Deal with output
<hr>
`model %>% intervention %>% Go! %>%` <orng>take-a-look</orng>
<hr>
# Plot
```{r}
mod %>% ev(amt = 100) %>% mrgsim() %>% plot()
```
# Plot
```{r}
mod %>% ev(amt = 100) %>% mrgsim() %>%
plot(CP + EFFECT~., col = "firebrick")
```
# Pipeline to dplyr functions
```{r}
mod %>% mrgsim() %>% mutate(arm = 1)
```
# What would you like to "fix" in this plot?
```{r,echo= FALSE}
mod <- update(mod, end = 18, delta = 6)
```
```{r}
mod %>% ev(amt = 100) %>% mrgsim() %>% plot(CP~time)
```
# Simulation end time
```{r}
mod %>% ev(amt = 100) %>% mrgsim(end = 48) %>% plot(CP~time)
```
# Simulation time step
```{r}
mod %>% ev(amt = 100) %>% mrgsim(end = 48, delta = 0.1) %>% plot(CP~time)
```
# Simulation time grid
- To form a sequence of times
- <grn>start</grn> usually `0`
- <grn>end</grn> time of last observation
- <grn>delta</grn> <blk>output</blk> time step
- Additional times to simulate
- <purp>add</purp> ad-hoc numeric vector
```{r}
stime(mod)
```
# Update
On the fly
```{r, eval = FALSE}
mod %>% update(end = 72) %>% mrgsim()
```
Persistent
```{r, eval = FALSE}
mod <- update(mod, end = 72)
```
mrgsim will call `update` for you (on the fly)
```{r, eval = FALSE}
mod %>% mrgsim(end = 72)
```
# What else can I update?
* Time
- `start`, `end`, `delta`, `add`
* Parameters and compartment initial values
* Solver settings
* `atol`, `rtol`
* `hmax`, `maxsteps`, `mxhnil`, `ixpr`
* `$OMEGA`, `$SIGMA`
* `tscale` (rescale the output time)
* `digits`
```{r, eval = FALSE}
mod %>% update(rtol = 1E-12) %>% ...
```
```{r, eval = FALSE}
mod %>% mrgsim(rtol = 1E-12) %>% ...
```
# Controlling output - <grn>request</grn>
```{r}
mod %>% mrgsim() %>% head(n = 2)
```
```{r}
mod %>% Req(CP) %>% mrgsim() %>% head(n = 2)
```
# Controlling output - <grn>obsonly</grn>
```{r}
mod %>% ev(amt=1) %>% mrgsim() %>% head(n = 2)
```
```{r}
mod %>% ev(amt=1) %>% obsonly() %>% mrgsim() %>% head(n = 2)
```
# Next slide deck
[Data Sets](deck_02.html)