I am working on creating a model that uses an equation to estimate transdermal flux over time which is then fed into the dynamics block to get plasma concentrations. I want to estimate the parameters that go into the flux equation. I need the absorption to be zero up to some time point (lag) then follow a simple equation. My model runs, but it is not finding a good lag time for some subjects.

When I look at inspect(fit), I can see that the flux being used in @dynamics is negative. Why is my equation in @pre not making flux zero before the lag time? Is there a better way to do this?

See here how the lag time is estimated well for all subjects except subject 2-NOOCC.

Here is my current model:

```
model_fabs_noocc = @model begin
@param begin
tv_lag ∈ RealDomain(lower=0.1, upper=6)
tv_R0 ∈ RealDomain(lower=30,upper=100)
tv_kout ∈ RealDomain(lower=0.01,upper=10)
tv_vc ∈ RealDomain(lower=0)
tv_cl ∈ RealDomain(lower=0)
tv_vp1 ∈ RealDomain(lower=0)
tv_qp1 ∈ RealDomain(lower=0)
tv_vp2 ∈ RealDomain(lower=0)
tv_qp2 ∈ RealDomain(lower=0)
Ω²_lag ∈ RealDomain(lower=0.001, upper=0.9)
Ω²_R0 ∈ RealDomain(lower=0.001)
Ω²_kout ∈ RealDomain(lower=0.001)
σ²_add ∈ RealDomain(lower=0.001)
σ²_prop ∈ RealDomain(lower=0.001)
end
@random begin
η_R0 ~ Normal(0,sqrt(Ω²_R0))
η_kout ~ Normal(0,sqrt(Ω²_kout))
η_lag ~ Normal(0,sqrt(Ω²_lag))
end
@covariates CATEGORY
@pre begin
lag = tv_lag * exp(η_lag[1])
R = tv_R0 * exp(η_R0[1])
k = tv_kout * exp(η_kout[1])
flux = R - R*exp(-k*(t-lag))
flux .= ifelse.(flux .<= 0, 0, flux) #limits time > 0
Vc = tv_vc
CL = tv_cl
Vp1 = tv_vp1
Qp1 = tv_qp1
Vp2 = tv_vp2
Qp2 = tv_qp2
end
@dynamics begin
Central' = flux - (CL/Vc)*Central - (Qp1/Vc)*Central + (Qp1/Vp1)*Peripheral1 - (Qp2/Vc)*Central + (Qp2/Vp2)*Peripheral2
Peripheral1' = (Qp1/Vc)*Central - (Qp1/Vp1)*Peripheral1
Peripheral2' = (Qp2/Vc)*Central - (Qp2/Vp2)*Peripheral2
end
@derived begin
Cp = @. Central/Vc
CONC ~ @. Normal(Cp, sqrt(((abs(Cp)^2)*σ²_prop) + σ²_add))
flux_dv = @. flux
end
end
```