# Time after dose (TAD)

time after dose

In this post, we’ll look at different ways you can bring time after dose into your simulated outputs.

Author

Kyle Baron

Published

06/01/2019

# 1 TAD calculated as runtime argument

For a while, you’ve been able to get time after dose in your simulated output

``````library(mrgsolve)
library(tidyverse)

mod <- modlib("pk1", req = "")

mod %>%
ev(amt = 100, ii = 4, addl = 3, time = 2) %>%
mrgsim(tad = TRUE, add = c(3.888,5.91)) %>%
as_tibble() %>%
``````# A tibble: 10 × 4
ID  time   tad    CP
<dbl> <dbl> <dbl> <dbl>
1     1  0    -2     0
2     1  1    -1     0
3     1  2     0     0
4     1  2     0     0
5     1  3     1     3.07
6     1  3.89  1.89  3.99
7     1  4     2     4.05
8     1  5     3     4.27
9     1  5.91  3.91  4.22
10     1  6     0     4.21``````

This is convenient because you can choose the output at run time, we give you the negative numbers prior to the first dose etc. This sort of calculation is possible because we let mrgsolve know ahead of time that we want this calculation done and mrgsolve makes an extra pass through the records to find when is the first dose for each individual.

# 2 TAD calculated in the model

Sometimes you would like to work with time after dose in your model. This isn’t super-complicated to do but does require some programming and setup and all of that. As of mrgsolve 0.9.1, there is a special function to do these calculations for you.

``mod <- mread("time_after_dose.txt",req = "")``

Looking at the `[MAIN]` block: ::: {.cell}

:::

When you call `self.tad()` you will get the time after dose. It’s important that this gets called on every record … specifically every dosing record. It will not work properly if it is not called every record and there is no check at this time to make sure you follow that rule. So please follow the rule.

To see an example:

``````[ param ] CL = 1, V = 20, KA = 1

[ pkmodel ] cmt = "GUT,CENT", depot = TRUE

[ main ]

[ table]
capture CP  = CENT/V;``````
``````mod %>%
ev(amt = 100, ii = 4, addl = 3, time = 2) %>%
mrgsim(tad = TRUE, add = c(3.888,5.91)) %>%
as_tibble() %>%
``````# A tibble: 10 × 5
<dbl> <dbl> <dbl>  <dbl> <dbl>
1     1  0    -2     -1     0
2     1  1    -1     -1     0
3     1  2     0     -1     0
4     1  2     0      0     0
5     1  3     1      1     3.07
6     1  3.89  1.89   1.89  3.99
7     1  4     2      2     4.05
8     1  5     3      3     4.27
9     1  5.91  3.91   3.91  4.22
10     1  6     0      0     4.21``````

You will notice two differences between `tad` (output requested at run time) and `tadose` (values calculated in the model itself) in the output listing above:

1. `tadose` is -1 before the first dose
2. specifically, `tadose` is -1 at the 2 hour observation record that occurs that the same time as the dose, but happens before the dose in record order

The main point if this is that you can easily obtain time after dose in the problem (model) itself to use as you program the model and also output the number into the simulated output.