# Help with fitting population

**URL:** <https://discourse.pumas.ai/t/help-with-fitting-population/111>\
**Category:** Estimation\
**Created:** [February 6, 2020, 7:04pm UTC](https://discourse.pumas.ai/t/help-with-fitting-population/111 "2020-02-06T19:04:55Z")\
**Posts on this page:** 2\
**Page:** 2

<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:** [February 11, 2020, 1:26pm UTC](https://discourse.pumas.ai/t/help-with-fitting-population/111/21 "2020-02-11T13:26:44Z")

</div>

I’ve looked a bit into this. I’ll go through some steps that might be generally useful whenever the model fitting fails. Hopefully it’s then easier for other people to benefit from them.

As mentioned above, it’s always a good idea to check the deviance value in the starting values. I think we’ll add a check for that to the `fit` method at some point and we’ll generally try to make the debugging process simpler. For now, the starting value should be checked manually with

```auto
julia> deviance(model3,dfP,param, Pumas.FOCEI())
┌ Warning: Interrupted. Larger maxiters is needed.
└ @ DiffEqBase ~/.julia/packages/DiffEqBase/avuk1/src/integrator_interface.jl:319
┌ Warning: Interrupted. Larger maxiters is needed.
└ @ DiffEqBase ~/.julia/packages/DiffEqBase/avuk1/src/integrator_interface.jl:319
┌ Warning: Interrupted. Larger maxiters is needed.
└ @ DiffEqBase ~/.julia/packages/DiffEqBase/avuk1/src/integrator_interface.jl:319
┌ Warning: Interrupted. Larger maxiters is needed.
└ @ DiffEqBase ~/.julia/packages/DiffEqBase/avuk1/src/integrator_interface.jl:319
┌ Warning: Interrupted. Larger maxiters is needed.
└ @ DiffEqBase ~/.julia/packages/DiffEqBase/avuk1/src/integrator_interface.jl:319
┌ Warning: Interrupted. Larger maxiters is needed.
└ @ DiffEqBase ~/.julia/packages/DiffEqBase/avuk1/src/integrator_interface.jl:319
NaN

```

Whenever, the starting value is either `Inf` of `NaN` it won’t be possible to fit the model. To figure out which of the subjects that are causing problems, we can use the (unexported) `findinfluential` function, i.e.

```auto
julia> Pumas.findinfluential(model3, dfP, param, Pumas.FOCEI())
┌ Warning: Interrupted. Larger maxiters is needed.
└ @ DiffEqBase ~/.julia/packages/DiffEqBase/avuk1/src/integrator_interface.jl:319
┌ Warning: Interrupted. Larger maxiters is needed.
└ @ DiffEqBase ~/.julia/packages/DiffEqBase/avuk1/src/integrator_interface.jl:319
┌ Warning: Interrupted. Larger maxiters is needed.
└ @ DiffEqBase ~/.julia/packages/DiffEqBase/avuk1/src/integrator_interface.jl:319
┌ Warning: Interrupted. Larger maxiters is needed.
└ @ DiffEqBase ~/.julia/packages/DiffEqBase/avuk1/src/integrator_interface.jl:319
┌ Warning: Interrupted. Larger maxiters is needed.
└ @ DiffEqBase ~/.julia/packages/DiffEqBase/avuk1/src/integrator_interface.jl:319
┌ Warning: Interrupted. Larger maxiters is needed.
└ @ DiffEqBase ~/.julia/packages/DiffEqBase/avuk1/src/integrator_interface.jl:319
5-element Array{Tuple{String,Float64},1}:
 ("12", NaN)
 ("14", Inf)
 ("15", Inf)
 ("9", 118.83800947482402)
 ("4", 91.37243210337728)

```

So it looks like there are some problems with subjects `12`, `14`, and `15`. The problems can happen as part of the evaluation of the conditional (on the random effects) likelihood function or when computing the derivatives with respect to the random effects in order to compute the Laplace approximation of the marginal likelihood.

To investigate this, we can evaluate just the conditional likelihood for the three subjects at zero random effects, i.e.

```auto
julia> map(["12", "14", "15"]) do id
         (id, Pumas.conditional_nll(model3, filter(i -> i.id == id, dfP)[1], param, (η=zeros(4),)))
       end
3-element Array{Tuple{String,Float64},1}:
 ("12", 1.0186482312664653e35)
 ("14", 43.07465583007613)
 ("15", 43.07348006826408)

```

The values of the conditional (log) likelihood function look reasonable for subjects `14`, and `15` but is extreme for subject `12`.

In such a case, it’s useful to inspect data for possible problems. Indeed, in this case it looks like there might be a problem with the timings. For subject 12, there is a positive concentration at `t = 0.0` but the first dose happens only at `t=0.0625`. This is the reason for the very large likelihood value and is most likely an error in the data.

For subjects `14`, and `15` things are more involved and I think we might have an issue in Pumas. By default, we use a hybrid ODE solver that will automatically switch to a stiff solver when necessary. For subjects `14` and `15` it looks like a stiff solver is indeed needed. We can try to evaluate the conditional likelihood with a non-stiff solver

```auto
julia> Pumas.conditional_nll(model3, filter(i -> i.id=="14", dfP)[1], param, (η=zeros(4),), alg=Vern7())
┌ Warning: Interrupted. Larger maxiters is needed.
└ @ DiffEqBase ~/.julia/packages/DiffEqBase/avuk1/src/integrator_interface.jl:319
Inf

julia> Pumas.conditional_nll(model3, filter(i -> i.id=="15", dfP)[1], param, (η=zeros(4),), alg=Vern7())
┌ Warning: Interrupted. Larger maxiters is needed.
└ @ DiffEqBase ~/.julia/packages/DiffEqBase/avuk1/src/integrator_interface.jl:319
Inf

```

It looks like we are correctly swtiching to the stiff solver when computing just the conditional likelihood but when we compute the deviance, the swiching to the stiff solver seems to fail. We can instead try to evaluate the marginal likelihood (same as the `deviance`) but forcing a stiff solver

```auto
julia> deviance(model3, filter(i -> i.id=="14", dfP)[1], param, Pumas.FOCEI(), alg=Rodas5())
65.93266393946297

julia> deviance(model3, filter(i -> i.id=="15", dfP)[1], param, Pumas.FOCEI(), alg=Rodas5())
65.93031242137423

```

and indeed it worked just fine.

---

<div class="post-metadata">

**Author:** ![alewolf](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pumas.ai/alewolf/32/24_2.png) [@alewolf](https://discourse.pumas.ai/u/alewolf)\
**Post date:** [February 11, 2020, 5:31pm UTC](https://discourse.pumas.ai/t/help-with-fitting-population/111/22 "2020-02-11T17:31:17Z")

</div>

This is awesome!

I need to double check the data for subject 12, but thanks for finding the error.  
Also this is a great tutorial to evaluate failing fits, find the errors and fix them -  
I think this would go great in a fit troubleshooting section in the documentation.  
I can’t make the data public (sorry), but maybe a similar dataset can be simulated?  
Thanks so much and please let me know if I can help in anyway

Ale

[Previous page](https://discourse.pumas.ai/t/help-with-fitting-population/111.md?page=1)
