metrumresearchgroup / mrgsolve

Simulate from ODE-based population PK/PD and QSP models in R
https://mrgsolve.org
129 stars 36 forks source link

event 'until' usage bug/misunderstanding? #1144

Closed dpastoor closed 7 months ago

dpastoor commented 9 months ago

I am trying to leverage until to design dosing regimens up til a fixed point in time where we're done observing. To use this example, given monthly dosing until 16 weeks I used the following - however my expectation was dosing would stop at 84 (or maybe 112 aka 16 weeks), instead I see that it continues out to 140 which is until + ii but that doesn't seem to align with the definition of

until | the expected maximum observation time for this regimen

> realize_addl(ev(amt = 300, time = 4*7, ii = 4*7, until = 16*7))
Events:
  time amt ii addl cmt evid
1   28 300  0    0   1    1
2   56 300  0    0   1    1
3   84 300  0    0   1    1
4  112 300  0    0   1    1
5  140 300  0    0   1    1
> 16*7
[1] 112
> 16*7+28
[1] 140

# what about a q2w dose:
> realize_addl(ev(amt = 300, time = 4*7, ii = 2*7, until = 16*7))
Events:
   time amt ii addl cmt evid
1    28 300  0    0   1    1
2    42 300  0    0   1    1
3    56 300  0    0   1    1
4    70 300  0    0   1    1
5    84 300  0    0   1    1
6    98 300  0    0   1    1
7   112 300  0    0   1    1
8   126 300  0    0   1    1
9   140 300  0    0   1    1
10  154 300  0    0   1    1

I'm not following how for the q4w regimen there is 1 extra dose past 112, but then if q2w there is 3 extra doses?

Would appreciate some clarity on if i'm misunderstanding until's behavior?

kylebaron commented 8 months ago

Sorry for late reply on this. Feels like a bug. I'm guessing there is some neglect of the fact that you are starting at time = 4*7 or time = 2*7 in the event. The results look ok if you start this at time = 0

library(mrgsolve)
#> 
#> Attaching package: 'mrgsolve'
#> The following object is masked from 'package:stats':
#> 
#>     filter

realize_addl(ev(amt = 300, time = 4*7, ii = 4*7, until = 16*7))
#> Events:
#>   time amt ii addl cmt evid
#> 1   28 300  0    0   1    1
#> 2   56 300  0    0   1    1
#> 3   84 300  0    0   1    1
#> 4  112 300  0    0   1    1
#> 5  140 300  0    0   1    1
realize_addl(ev(amt = 300, time = 0, ii = 4*7, until = 16*7))
#> Events:
#>   time amt ii addl cmt evid
#> 1    0 300  0    0   1    1
#> 2   28 300  0    0   1    1
#> 3   56 300  0    0   1    1
#> 4   84 300  0    0   1    1
realize_addl(ev(amt = 300, time = 0, ii = 4*7, until = 16*7 + 0.001))
#> Events:
#>   time amt ii addl cmt evid
#> 1    0 300  0    0   1    1
#> 2   28 300  0    0   1    1
#> 3   56 300  0    0   1    1
#> 4   84 300  0    0   1    1
#> 5  112 300  0    0   1    1

realize_addl(ev(amt = 300, time = 4*7, ii = 2*7, until = 16*7))
#> Events:
#>    time amt ii addl cmt evid
#> 1    28 300  0    0   1    1
#> 2    42 300  0    0   1    1
#> 3    56 300  0    0   1    1
#> 4    70 300  0    0   1    1
#> 5    84 300  0    0   1    1
#> 6    98 300  0    0   1    1
#> 7   112 300  0    0   1    1
#> 8   126 300  0    0   1    1
#> 9   140 300  0    0   1    1
#> 10  154 300  0    0   1    1
realize_addl(ev(amt = 300, time = 0, ii = 2*7, until = 16*7))
#> Events:
#>   time amt ii addl cmt evid
#> 1    0 300  0    0   1    1
#> 2   14 300  0    0   1    1
#> 3   28 300  0    0   1    1
#> 4   42 300  0    0   1    1
#> 5   56 300  0    0   1    1
#> 6   70 300  0    0   1    1
#> 7   84 300  0    0   1    1
#> 8   98 300  0    0   1    1
realize_addl(ev(amt = 300, time = 0, ii = 2*7, until = 16*7+0.001))
#> Events:
#>   time amt ii addl cmt evid
#> 1    0 300  0    0   1    1
#> 2   14 300  0    0   1    1
#> 3   28 300  0    0   1    1
#> 4   42 300  0    0   1    1
#> 5   56 300  0    0   1    1
#> 6   70 300  0    0   1    1
#> 7   84 300  0    0   1    1
#> 8   98 300  0    0   1    1
#> 9  112 300  0    0   1    1

Created on 2024-01-10 with reprex v2.0.2

So workaround would be to start it at time = 0 and then offset the times?

realize_addl(ev(amt = 300, time = 0, ii = 2*7, until = 16*7)) %>% 
  mutate(time = time + 2*7)
#> Events:
#>   time amt ii addl cmt evid
#> 1   14 300  0    0   1    1
#> 2   28 300  0    0   1    1
#> 3   42 300  0    0   1    1
#> 4   56 300  0    0   1    1
#> 5   70 300  0    0   1    1
#> 6   84 300  0    0   1    1
#> 7   98 300  0    0   1    1
#> 8  112 300  0    0   1    1

Created on 2024-01-10 with reprex v2.0.2