-
Notifications
You must be signed in to change notification settings - Fork 6
/
events-et.Rmdh
475 lines (356 loc) · 12.8 KB
/
events-et.Rmdh
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
An event table in rxode2 is a specialized data frame that acts as a
container for all of rxode2's events and observation times.
To create an rxode2 event table you may use the code `eventTable()`,
`et()`, or even create your own data frame with the right event
information contained in it. This is closely related to the [types of
events that rxode2 supports](rxode2-event-types.html).
```{r}
library(rxode2)
library(units)
(ev <- et())
```
or
```{r}
(ev <- et())
```
With this event table you can add sampling/observations or doses by
piping or direct access.
This is a short table of the two main functions to create dosing
| add.dosing() | et() | Description |
|-----------------|------|-------------------------------------|
| dose | amt | Dose/Rate/Duration amount |
| nbr.doses | addl | Additional doses or number of doses |
| dosing.interval | ii | Dosing Interval |
| dosing.to | cmt | Dosing Compartment |
| rate | rate | Infusion rate |
| start.time | time | Dosing start time |
| | dur | Infusion Duration |
Sampling times can be added with `add.sampling(` sampling times `)` or
`et(` sampling times `)`. [Dosing intervals and sampling
windows](#add-doses-and-samples-within-a-sampling-window) are also
supported.
With most of the documentation we will be using the `et()` syntax,
though both are supported.
For these models, we can illustrate by using the model shared in the
rxode2 tutorial:
```{r}
## Model from rxode2 tutorial
m1 <-function() {
ini({
KA <- 2.94E-01
CL <- 1.86E+01
V2 <- 4.02E+01
Q <- 1.05E+01
V3 <- 2.97E+02
Kin <- 1
Kout <- 1
EC50 <- 200
## Added modeled bioavaiblity, duration and rate
fdepot <- 1
durDepot <- 8
rateDepot <- 1250
})
model({
C2 <- centr/V2
C3 <- peri/V3
d/dt(depot) <- -KA*depot
f(depot) <- fdepot
dur(depot) <- durDepot
rate(depot) <- rateDepot
d/dt(centr) <- KA*depot - CL*C2 - Q*C2 + Q*C3
d/dt(peri) <- Q*C2 - Q*C3
d/dt(eff) <- Kin - Kout*(1-C2/(EC50+C2))*eff;
eff(0) <- 1
})
}
```
## Adding doses to the event table
Once created you can add dosing to the event table by the
`add.dosing()`, and `et()` functions.
Using the `add.dosing()` function you have:
| argument | meaning |
|-----------------|-----------------------------------------|
| dose | dose amount |
| nbr.doses | Number of doses; Should be at least 1. |
| dosing.interval | Dosing interval; By default this is 24. |
| dosing.to | Compartment where dose is administered. |
| rate | Infusion rate |
| start.time | The start time of the dose |
```{r}
ev <- eventTable(amount.units="mg", time.units="hr")
## The methods ar attached to the event table, so you can use
## them directly
ev$add.dosing(dose=10000, nbr.doses = 3)# loading doses
## Starts at time 0; Default dosing interval is 24
## You can also pipe the event tables to these methods.
ev <- ev %>%
add.dosing(dose=5000, nbr.doses=14,
dosing.interval=12)# maintenance
ev
```
Notice that the units were specified in the table. When specified, the
units use the `units` package to keep track of the units and convert
them if needed. Additionally, `ggforce` uses them to label the
`ggplot` axes. The `set_units` and `drop_units` are useful to set and
drop the rxode2 event table units.
In this example, you can see the time axes is labeled:
```{r}
rxSolve(m1, ev) %>% plot(C2)
```
If you are more familiar with the NONMEM/rxode2 event records, you can
also specify dosing using `et` with the dose elements directly:
```{r}
ev <- et(timeUnits="hr") %>%
et(amt=10000, until = set_units(3, days),
ii=12) # loading doses
ev
```
Which gives:
```{r}
rxSolve(m1, ev) %>% plot(C2)
```
This shows how easy creating event tables can be.
## Adding sampling to an event table
If you notice in the above examples, rxode2 generated some default
sampling times since there was not any sampling times. If you wish
more control over the sampling time, you should add the samples to the
rxode2 event table by `add.sampling` or `et`
```{r}
ev <- eventTable(amount.units="mg", time.units="hr")
## The methods ar attached to the event table, so you can use them
## directly
ev$add.dosing(dose=10000, nbr.doses = 3)# loading doses
ev$add.sampling(seq(0,24,by=4))
ev
```
Which gives:
```{r}
solve(m1, ev) %>% plot(C2)
```
Or if you use `et` you can simply add them in a similar way to `add.sampling`:
```{r}
ev <- et(timeUnits="hr") %>%
et(amt=10000, until = set_units(3, days),
ii=12) %>% # loading doses
et(seq(0,24,by=4))
ev
```
which gives the following rxode2 solve:
```{r}
solve(m1, ev) %>% plot(C2)
```
Note the jagged nature of these plots since there was only a few
sample times.
## Expand the event table to a multi-subject event table.
The only thing that is needed to expand an event table is a list of
IDs that you want to expand;
```{r}
ev <- et(timeUnits="hr") %>%
et(amt=10000, until = set_units(3, days),
ii=12) %>% # loading doses
et(seq(0,48,length.out=200)) %>%
et(id=1:4)
ev
```
You can see in the following simulation there are 4 individuals that are solved for:
```{r}
set.seed(42)
rxSetSeed(42)
solve(m1, ev,
params=data.frame(KA=0.294*exp(rnorm(4)),
CL=18.6*exp(rnorm(4)))) %>%
plot(C2)
```
## Add doses and samples within a sampling window
In addition to adding fixed doses and fixed sampling times, you can
have windows where you sample and draw doses from. For dosing windows
you specify the time as an ordered numerical vector with the lowest
dosing time and the highest dosing time inside a list.
In this example, you start with a dosing time with a 6 hour dosing window:
```{r}
set.seed(42)
rxSetSeed(42)
ev <- et(timeUnits="hr") %>%
et(time=list(c(0,6)), amt=10000, until = set_units(2, days),
ii=12) %>% # loading doses
et(id=1:4)
ev
```
You can clearly see different dosing times in the following simulation:
```{r}
ev <- ev %>% et(seq(0,48,length.out=200))
solve(m1, ev,
params=data.frame(KA=0.294*exp(rnorm(4)),
CL=18.6*exp(rnorm(4)))) %>%
plot(C2)
```
Of course in reality the dosing interval may only be 2 hours:
```{r}
set.seed(42)
rxSetSeed(42)
ev <- et(timeUnits="hr") %>%
et(time=list(c(0,2)), amt=10000, until = set_units(2, days),
ii=12) %>% # loading doses
et(id=1:4) %>%
et(seq(0,48,length.out=200))
solve(m1, ev,
params=data.frame(KA=0.294*exp(rnorm(4)),
CL=18.6*exp(rnorm(4)))) %>%
plot(C2)
```
The same sort of thing can be specified with sampling times. To
specify the sampling times in terms of a sampling window, you can
create a list of the sampling times. Each sampling time will be a two
element ordered numeric vector.
```{r}
rxSetSeed(42)
set.seed(42)
ev <- et(timeUnits="hr") %>%
et(time=list(c(0,2)), amt=10000, until = set_units(2, days),
ii=12) %>% # loading doses
et(id=1:4)
## Create 20 samples in the first 24 hours and 20 samples in the
## second 24 hours
samples <- c(lapply(1:20, function(...){c(0,24)}),
lapply(1:20, function(...){c(20,48)}))
## Add the random collection to the event table
ev <- ev %>% et(samples)
library(ggplot2)
solve(m1, ev, params=data.frame(KA=0.294*exp(rnorm(4)),
CL=18.6*exp(rnorm(4)))) %>%
plot(C2) + geom_point()
```
This shows the flexibility in dosing and sampling that the rxode2 event tables allow.
## Combining event tables
Since you can create dosing records and sampling records, you can
create any complex dosing regimen you wish. In addition, rxode2 allows
you to combine event tables by `c`, `seq`, `rep`, and `rbind`.
## Sequencing event tables
One way to combine event table is to sequence them by `c`, `seq` or
`etSeq`. This takes the two dosing groups and adds at least one
inter-dose interval between them:
```{r}
## bid for 5 days
bid <- et(timeUnits="hr") %>%
et(amt=10000,ii=12,until=set_units(5, "days"))
## qd for 5 days
qd <- et(timeUnits="hr") %>%
et(amt=20000,ii=24,until=set_units(5, "days"))
## bid for 5 days followed by qd for 5 days
et <- seq(bid,qd) %>% et(seq(0,11*24,length.out=100));
rxSolve(m1, et) %>% plot(C2)
```
When sequencing events, you can also separate this sequence by a
period of time; For example if you wanted to separate this by a week,
you could easily do that with the following sequence of event tables:
```{r}
## bid for 5 days followed by qd for 5 days
et <- seq(bid,set_units(1, "week"), qd) %>%
et(seq(0,18*24,length.out=100));
rxSolve(m1, et) %>% plot(C2)
```
Note that in this example the time between the bid and the qd event
tables is exactly one week, not 1 week plus 24 hours because of the
inter-dose interval. If you want that behavior, you can sequence it
using the `wait="+ii"`.
```{r}
## bid for 5 days followed by qd for 5 days
et <- seq(bid,set_units(1, "week"), qd,wait="+ii") %>%
et(seq(0,18*24,length.out=100));
rxSolve(m1, et) %>% plot(C2)
```
Also note, that rxode2 assumes that the dosing is what you want to
space the event tables by, and clears out any sampling records when
you combine the event tables. If that is not true, you can also use
the option `samples="use"`
## Repeating event tables
You can have an event table that you can repeat with `etRep` or `rep`.
For example 4 rounds of 2 weeks on QD therapy and 1 week off of
therapy can be simply specified:
```{r}
qd <-et(timeUnits = "hr") %>%
et(amt=10000, ii=24, until=set_units(2, "weeks"), cmt="depot")
et <- rep(qd, times=4, wait=set_units(1,"weeks")) %>%
add.sampling(set_units(seq(0, 12.5,by=0.005),weeks))
rxSolve(m1, et) %>% plot(C2)
```
This is a simplified way to use a sequence of event tables.
Therefore, many of the same options still apply; That is `samples` are
cleared unless you use `samples="use"`, and the time between event
tables is at least the inter-dose interval. You can adjust the timing
by the `wait` option.
## Combining event tables with rbind
You may combine event tables with `rbind`. This does not consider the
event times when combining the event tables, but keeps them the same
times. If you space the event tables by a waiting period, it also does
not consider the inter-dose interval.
Using the previous `seq` you can clearly see the difference. Here was the sequence:
```{r}
## bid for 5 days
bid <- et(timeUnits="hr") %>%
et(amt=10000,ii=12,until=set_units(5, "days"))
## qd for 5 days
qd <- et(timeUnits="hr") %>%
et(amt=20000,ii=24,until=set_units(5, "days"))
et <- seq(bid,qd) %>%
et(seq(0,18*24,length.out=500));
rxSolve(m1, et) %>% plot(C2)
```
But if you bind them together with `rbind`
```{r}
## bid for 5 days
et <- rbind(bid,qd) %>%
et(seq(0,18*24,length.out=500));
rxSolve(m1, et) %>% plot(C2)
```
Still the waiting period applies (but does not consider the inter-dose interval)
```{r}
et <- rbind(bid,wait=set_units(10,days),qd) %>%
et(seq(0,18*24,length.out=500));
rxSolve(m1, et) %>% plot(C2)
```
You can also bind the tables together and make each ID in the event
table unique; This can be good to combine cohorts with different
expected dosing and sampling times. This requires the `id="unique"`
option; Using the first example shows how this is different in this case:
```{r}
## bid for 5 days
et <- etRbind(bid,qd, id="unique") %>%
et(seq(0,150,length.out=500));
library(ggplot2)
rxSolve(m1, et) %>% plot(C2) + facet_wrap( ~ id)
```
## Event tables in Rstudio Notebooks
In addition to the output in the console which has been shown in the above examples, Rstudio notebook output is different and can be seen in the following screenshots;
The first screenshot shows how the event table looks after evaluating
it in the Rstduio notebook
![](eventTable-output-notebook-02.png)
This is a simple dataframe that allows you to page through the
contents. If you click on the first box in the Rstudio notebook
output, it will have the notes about the event table:
![](eventTable-output-notebook-01.png)
## Expanding events
Event tables can be expanded so they contain an `addl` data item, like
the following example:
```{r}
ev <- et() %>%
et(dose=50, ii=8, until=48)
ev
```
You can expand the events so they do not have the `addl` items by
`$expand()` or `etExpand(ev)`:
The first, `etExpand(ev)` expands the event table without modifying the original data frame:
```{r}
etExpand(ev)
```
You can see the `addl` events were expanded, however the original data
frame remained intact:
```{r}
print(ev)
```
If you use `ev$expand()` it will modify the `ev` object. This is
similar to an object-oriented method:
```{r}
ev$expand()
ev
```