# BSV Gradient Zero - piecewise function

Hello,
I have a simple piecewise equation, but when I try to add BSV, it says that the gradient is exactly zero in Ω²_R0 indicating that Ω²_R0 is not identified. I clearly have the BSV identified, but I am thinking that something about the piecewise equations restricts the model fitting. Is there a better way to do this sort of thing?
To explain the data better, I have 16 IDs that are labeled CATEGORY=“NOOCC” and 16 IDs that are labeled CATEGORY=“OCC”. The NOOCC data should be fit with the regular equation: R*(1-exp(-k*(t))) while the OCC data has some effect on that equation.

Here is my model:

``````model_vitro = @model begin
@param begin
tv_R0       ∈   RealDomain(lower = 0.0000000001) # ug/cm2-hr
tv_kout     ∈   RealDomain(lower = 0.0000000001) # 1/hr
tv_EFF_OCC     ∈   RealDomain(lower = 0.0000000001)
tv_EFF_RMOCC   ∈   RealDomain(lower = 0.0000000001)
Ω²_R0       ∈   RealDomain(lower=0.0001)
Ω²_kout     ∈   RealDomain(lower=0.0001)
Ω²_EFF_OCC     ∈   RealDomain(lower = 0.0000000001)
Ω²_EFF_RMOCC   ∈   RealDomain(lower = 0.0000000001)
end

@random begin
η_R0    ~ Normal(0,sqrt(Ω²_R0))
η_kout  ~ Normal(0,sqrt(Ω²_kout))
η_EFF_OCC  ~ Normal(0,sqrt(Ω²_EFF_OCC))
η_EFF_RMOCC ~ Normal(0,sqrt(Ω²_EFF_RMOCC))
end

@covariates CATEGORY

@pre begin
R = tv_R0    * exp(η_R0)
k = tv_kout  * exp(η_kout)
EFF_OCC = tv_EFF_OCC * exp(η_EFF_OCC)
EFF_RMOCC = tv_EFF_RMOCC * exp(η_EFF_RMOCC)

slope = CATEGORY == "OCC" && t >= 7  && t<10 ? R*(1-exp(-k*(7))) + EFF_OCC*(t-7) :
CATEGORY == "OCC" && t >= 10         ? R*(1-exp(-k*(7))) + EFF_OCC*(10-7) + EFF_RMOCC*(t-10) :
R*(1-exp(-k*(t)))
end

@derived begin
flux_vitro = @. slope  # amount in the reciever per unit area
end
end

param_vitro =  (
tv_R0      = 0.15,
tv_kout    = 0.1,
tv_EFF_OCC    = 0.1,
tv_EFF_RMOCC  = 0.7,
Ω²_R0      = 0.1,
Ω²_kout    = 0.1,
Ω²_EFF_OCC    = 0.1,
Ω²_EFF_RMOCC    = 0.1,
)
``````

Do you get the same error if you just set

``````slope = R*(1-exp(-k*(t)))
``````

Also, how dose `[sub.time for sub in pop]` look like?

It works using the piecewise equation without BSV, but it must not be able to initialize when I add BSV because it isn’t used until after 7 hr?
If I use the equation slope = R*(1-exp(-k*(t))) it still says gradient is exactly zero in Ω²_R0.

sub.time outputs: 0.0, 2.0, 5.5, 8.5, 11.0

I am not able to reproduce the issue with a simulated population.

``````pop = Subject.(
simobs(
model_vitro,
[Subject(id = i, time = 0.0:1.0:20.0, covariates = (; CATEGORY = "OCC")) for i in 1:10],
param_vitro,
),
)

fpm = fit(model_vitro, pop, param_vitro, LaplaceI())
``````