# DosageRegimen varies during infusion time

**URL:** <https://discourse.pumas.ai/t/dosageregimen-varies-during-infusion-time/499>\
**Category:** How-to\
**Created:** [August 24, 2021, 4:56pm UTC](https://discourse.pumas.ai/t/dosageregimen-varies-during-infusion-time/499 "2021-08-24T16:56:41Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![NamTienNguyen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pumas.ai/namtiennguyen/32/143_2.png) [@NamTienNguyen](https://discourse.pumas.ai/u/NamTienNguyen)\
**Post date:** [August 24, 2021, 4:56pm UTC](https://discourse.pumas.ai/t/dosageregimen-varies-during-infusion-time/499/1 "2021-08-24T16:56:41Z")

</div>

Hi all,

I have a question related to **creating `DosageRegimen` in which Dose varies during infusion time**.

Let’s assume that due to some reason that the drug loses during infusing, and the concentration decreases with time. And I want to create the ‘dynamical dose’. Is any way for me to do this?

For instance, I have 1000 mg at the onset of therapy, after 30 min, the `amt` is 950 mg until fulfilling the infusion, assume 2h, the `amt` is 800 mg. If I traditionally create `DosageRegimen`, the dose (and actually the concentration) is seen to be incremental.

Thank you for your help,  
Tien Nguyen.

---

<div class="post-metadata">

**Author:** ![jogagobburu](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@jogagobburu](https://discourse.pumas.ai/u/jogagobburu)\
**Post date:** [August 24, 2021, 5:15pm UTC](https://discourse.pumas.ai/t/dosageregimen-varies-during-infusion-time/499/2 "2021-08-24T17:15:09Z")

</div>

You can create 3 different infusion doses with your specifications. For eg, first infusion is 50mg for 30min, followed by 150mg for 2h…etc. Can you try that?  
J

---

<div class="post-metadata">

**Author:** ![NamTienNguyen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pumas.ai/namtiennguyen/32/143_2.png) [@NamTienNguyen](https://discourse.pumas.ai/u/NamTienNguyen)\
**Post date:** [August 24, 2021, 9:49pm UTC](https://discourse.pumas.ai/t/dosageregimen-varies-during-infusion-time/499/3 "2021-08-24T21:49:40Z")

</div>

Thank you for your response.

I tried this before asking my question, but it didn’t work. The `amt` is counter for new `events`, but in this example, it must still be in 1 `events`.

I also tried to modify in `@model` like that, `Cp = @. (Central.X%)/Vc`, where X% is `array[100, 95, 90, 85, 80]`. But it when I executed my simulation, I had an error `DimensionMismatch("arrays could not be broadcast to a common size; got a dimension with lengths 129 and 7")`

Thank you for your help,  
Tien Nguyen.

---

<div class="post-metadata">

**Author:** ![andreasnoack](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pumas.ai/andreasnoack/32/22_2.png) [@andreasnoack](https://discourse.pumas.ai/u/andreasnoack)\
**Post date:** [August 25, 2021, 6:22am UTC](https://discourse.pumas.ai/t/dosageregimen-varies-during-infusion-time/499/4 "2021-08-25T06:22:18Z")

</div>

> [@NamTienNguyen](#):
>
> The `amt` is counter for new `events` , but in this example, it must still be in 1 `events` .

Please elaborate. Why can’t it be three different back-to-back infusion doses?

---

<div class="post-metadata">

**Author:** ![NamTienNguyen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pumas.ai/namtiennguyen/32/143_2.png) [@NamTienNguyen](https://discourse.pumas.ai/u/NamTienNguyen)\
**Post date:** [August 25, 2021, 6:47am UTC](https://discourse.pumas.ai/t/dosageregimen-varies-during-infusion-time/499/5 "2021-08-25T06:47:18Z")

</div>

Thank you for your response,

The problem is due to some reason (especially instability drug), the drug loses during infusing, and the concentration (and `amt`) decreases with time in the infusing solution.

So lets me assume that the drug is given in extended infusion (for example with 3h), then the `amt` naturally decreases in infusion solution bags. So basically, it is still in 1 `events`.

I don’t know whether what I explained above matching with your question.

Thank you for your help,  
Tien Nguyen.

---

<div class="post-metadata">

**Author:** ![andreasnoack](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pumas.ai/andreasnoack/32/22_2.png) [@andreasnoack](https://discourse.pumas.ai/u/andreasnoack)\
**Post date:** [August 25, 2021, 7:12am UTC](https://discourse.pumas.ai/t/dosageregimen-varies-during-infusion-time/499/6 "2021-08-25T07:12:27Z")

</div>

My question was mostly why it’s necessary to treat this as a single dose in the dataset/software. It’s not clear to me why it would matter. Splitting it into three doses in the dataset would just be a convenient way of handling the effect that you are describing. The number of events in the software seems like it could be considered an internal detail as long as the total dose matches what was administered.

An alternative solution that might be closer to the real world would be to model the infusion rate as a smoothly decaying function over time. That is possible but then you’d have to carry the dose information as a covariate instead of using the event system since the latter only handles the three standard administration types. That can be done in the `pre` block. See the Weibull example in [Absorption models](https://tutorials.pumas.ai/html/pkpd/absorption_models.html) for how this can be done.

---

<div class="post-metadata">

**Author:** ![jogagobburu](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@jogagobburu](https://discourse.pumas.ai/u/jogagobburu)\
**Post date:** [August 25, 2021, 11:54am UTC](https://discourse.pumas.ai/t/dosageregimen-varies-during-infusion-time/499/7 "2021-08-25T11:54:26Z")

</div>

Dear Tien - Interesting discussion. You may want to consider the following if you want to treat the dose as one event:

1. Setup a diff eq based model with a depot and central compartments. Use a first-order transfer from depot to central; you can fix the ka to a high number 5/hr for eg. This will not be your rate-limiting step.
2. Add a zero-order kloss from the depot compartment. If you know the ‘degradation’ rate from stability experiments you can fix to that rate here.
3. Perform a simple simulation to begin with, to make sure this is what you wanted to achieve.
4. If happy so far, then you can add a step-wise change in the kloss based on your requirements.  
J

---

<div class="post-metadata">

**Author:** ![NamTienNguyen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pumas.ai/namtiennguyen/32/143_2.png) [@NamTienNguyen](https://discourse.pumas.ai/u/NamTienNguyen)\
**Post date:** [August 25, 2021, 2:25pm UTC](https://discourse.pumas.ai/t/dosageregimen-varies-during-infusion-time/499/8 "2021-08-25T14:25:41Z")

</div>

Thank you, Joga and Andreas Noack for your recommendation.

1. First, I have the same idea as you, Joga. But the `depot` here corresponding to the infusion solution, so I think instead of using 1-order transfer from `depot` to `central`, we should use zero-order with rate = Depot/duration. And `depot` here has 2 zero-order rates.

2. Why this zero-order model has an error when simulation, I just copy this in [tutorial](https://tutorials.pumas.ai/html/PKPDDataAnalysisBook/pk03.html). I’m using Pumas 2.0.

```auto
pk_03 = @model begin
  @param begin
    tvcl ∈ RealDomain(lower=0)
    tvvc ∈ RealDomain(lower=0)
    tvTabs ∈ RealDomain(lower=0)
    Ω ∈ PDiagDomain(3)
    σ²_prop ∈ RealDomain(lower=0)
  end

  @random begin
    η ~ MvNormal(Ω)
  end

  @pre begin
    Cl = tvcl * exp(η[1])
    Vc = tvvc * exp(η[2])
    duration = (Central = tvTabs * exp(η[3]),)
  end

  @dynamics begin
    Central' = - (Cl/Vc)*Central
  end

  @derived begin
    cp = @. 1000*(Central/Vc)
    dv ~ @. Normal(cp, sqrt(cp^2*σ²_prop))
  end
end

param = ( tvcl = 45.12,
          tvvc = 96,
          tvTabs = 4.54,
          Ω = Diagonal([0.0,0.0,0.0]),
          σ²_prop = 0.015
)

ev1 = DosageRegimen(20,rate=-2)
sub1 = Subject(id=1,events=ev1)

Random.seed!(123)
sim_sub1 = simobs(pk_03, sub1, param, obstimes=0:0.1:10)

```

```auto
ERROR: ArgumentError: either rate or duration should be set in the @pre block when the Event rate is -2

```

---

<div class="post-metadata">

**Author:** ![vijay](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pumas.ai/vijay/32/96_2.png) [@vijay](https://discourse.pumas.ai/u/vijay)\
**Post date:** [August 25, 2021, 2:53pm UTC](https://discourse.pumas.ai/t/dosageregimen-varies-during-infusion-time/499/9 "2021-08-25T14:53:55Z")

</div>

Tien -

The tutorials in that page are being updated to the new syntax, but essentially what you are hitting is an error due to a syntax change in 2.0. All parameters that control the dose are now in a new block called the `dosecontrol` block. You can see the documentation [here](https://docs.pumas.ai/stable/model_components/dosing_control/#Dose-Control-Parameters-(DCP)).

here is the complete code that will help you run the example in Pumas 2.0

```auto
using Random, Pumas, PumasUtilities
pk_03 = @model begin
  @param begin
    tvcl ∈ RealDomain(lower=0)
    tvvc ∈ RealDomain(lower=0)
    tvTabs ∈ RealDomain(lower=0)
    Ω ∈ PDiagDomain(3)
    σ²_prop ∈ RealDomain(lower=0)
  end

  @random begin
    η ~ MvNormal(Ω)
  end

  @pre begin
    Cl = tvcl * exp(η[1])
    Vc = tvvc * exp(η[2])
  end

  @dosecontrol begin
    duration = (Central = tvTabs * exp(η[3]),)
  end

  @dynamics begin
    Central' = - (Cl/Vc)*Central
  end

  @derived begin
    cp = @. 1000*(Central/Vc)
    dv ~ @. Normal(cp, sqrt(cp^2*σ²_prop))
  end
end

param = ( tvcl = 45.12,
          tvvc = 96,
          tvTabs = 4.54,
          Ω = Diagonal([0.0,0.0,0.0]),
          σ²_prop = 0.001
)

ev1 = DosageRegimen(20,rate=-2)
sub1 = Subject(id=1,events=ev1, observations = (cp = nothing, dv=nothing))

Random.seed!(123)
sim_sub1 = simobs(pk_03, sub1, param, obstimes=0.1:0.1:10)

sim_plot(pk_03, [sim_sub1])

```

---

<div class="post-metadata">

**Author:** ![NamTienNguyen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pumas.ai/namtiennguyen/32/143_2.png) [@NamTienNguyen](https://discourse.pumas.ai/u/NamTienNguyen)\
**Post date:** [August 25, 2021, 3:06pm UTC](https://discourse.pumas.ai/t/dosageregimen-varies-during-infusion-time/499/10 "2021-08-25T15:06:45Z")

</div>

Thank you Profesor for your answer.

---

<div class="post-metadata">

**Author:** ![NamTienNguyen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pumas.ai/namtiennguyen/32/143_2.png) [@NamTienNguyen](https://discourse.pumas.ai/u/NamTienNguyen)\
**Post date:** [August 26, 2021, 2:11am UTC](https://discourse.pumas.ai/t/dosageregimen-varies-during-infusion-time/499/11 "2021-08-26T02:11:21Z")

</div>

Dear all, Can I use `if - else control follow` in `@pre` block? I tried to use it but had an error

Thank you for your help.

```auto
using Pumas, DataFrames, Plots, Pkg, CSV, Random, PumasUtilities

model = @model begin
    @param begin
        θCl ∈ RealDomain(lower = 0.0, init = 5.27)
        θVc ∈ RealDomain(lower = 0.0, init = 17.2)
        θQ ∈ RealDomain(lower = 0.0, init = 9.92)
        θVp ∈ RealDomain(lower = 0.0, init = 10.6)
        θX ∈ RealDomain(lower = 0.0, init = 0.2)
        #θduration ∈ RealDomain(lower = 0.0, init = 3)
        Ω_IIV ∈ PDiagDomain(init = [log(0.265^2 + 1), log(0.307^2 + 1)]) # IIV for Cl and Vc
    end

    @random begin
        η_IIV ~ MvNormal(Ω_IIV)
    end

    @covariates begin
        Clcr
        Wt
    end

    @pre begin
        Cl = θCl*(1 + 0.014*(Clcr - 39.0)*(60/1000))*(1 + 0.007*(Wt - 75))*exp(η_IIV[1])
        Vc = θVc*(1 + 0.0225*(Wt - 75))*exp(η_IIV[2])
        Vp = θVp
        Q = θQ
        if t <= 3
            μ = (θX/100)*t
        else μ = 0
        end 
    end

    #=@dosecontrol begin
        duration = (Central = θduration, )
    end=#

    @dynamics begin
        Depot' = -Depot*μ
        Central' = Depot/3 -Central*(Cl/Vc + Q/Vc) + Peri*Q/Vp
        Peri' = Central*Q/Vc - Peri*Q/Vp
    end

    @derived begin
        #Cdepot = Depot/1
        Cp = @. Central/Vc
        # dv ~ @. Normal(Cp, sqrt(...))
    end
end

```

```auto
ERROR: Variable μ already defined

```

---

<div class="post-metadata">

**Author:** ![andreasnoack](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pumas.ai/andreasnoack/32/22_2.png) [@andreasnoack](https://discourse.pumas.ai/u/andreasnoack)\
**Post date:** [August 26, 2021, 7:38am UTC](https://discourse.pumas.ai/t/dosageregimen-varies-during-infusion-time/499/12 "2021-08-26T07:38:09Z")

</div>

> [@NamTienNguyen](#):
>
> Dear all, Can I use `if - else control follow` in `@pre` block?

Yes that is allowed but you can only assign to a variable once, so you’d have to write it like

```julia
μ = if t <= 3
    (θX/100)*t
else
    zero((θX/100)*t)
end

```

Using `zero` instead of `0` isn’t strictly require but it ensures that both branches in the control flow statement produce floating point values. This will give better performance.

An alternative formulation without control flow is

```auto
μ = (t <= 3)*(θX/100)*t

```

---

<div class="post-metadata">

**Author:** ![NamTienNguyen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pumas.ai/namtiennguyen/32/143_2.png) [@NamTienNguyen](https://discourse.pumas.ai/u/NamTienNguyen)\
**Post date:** [August 26, 2021, 8:44am UTC](https://discourse.pumas.ai/t/dosageregimen-varies-during-infusion-time/499/13 "2021-08-26T08:44:33Z")

</div>

It’s interesting! Thank you for your help.
