Calculating Parameter Uncertainty

Author

Patrick Kofod Mogensen

1 Introduction

A typical workflow for fitting a Pumas model and deriving parameter precision typically involves:

  1. Preparing the data and the model.
  2. Checking model-data compatibility.
  3. Obtaining initial parameter estimates.
  4. Fitting the model via a chosen estimation method.
  5. Interpreting the fit results.
  6. Computing parameter uncertainty based on the asymptotic variance-covariance formulas (robust or not).
  7. (Optionally) proceeding with more advanced techniques like bootstrapping or SIR for robust uncertainty quantification.

In previous tutorials, we already set up the data and performed a fit. We also obtained some parameter uncertainty estimates. In this tutorial, we will go more into depth with parameter uncertainty calculations using different methods. Exploratory data analysis (EDA), although extremely important, is out of scope here. Readers interested in EDA are encouraged to consult other tutorials.

2 Model and Data

2.1 Model Definition

Below is the PK model, named warfarin_pk_model, defined in Pumas. This model contains:

  • Fixed effects (population parameters): pop_CL, pop_Vc, pop_tabs, pop_lag
  • Inter-individual variability (IIV) components: pk_Ω
  • Residual error model parameters: σ_prop,σ_add
  • Covariates for scaling: FSZCL and FSZV
  • Differential equations describing the PK behavior in the compartments.
using Pumas
using PharmaDatasets
using DataFramesMeta
using PumasUtilities
warfarin_pk_model = @model begin
    @metadata begin
        desc = "Warfarin 1-compartment PK model (PD removed)"
        timeu = u"hr"
    end
    @param begin
        # PK parameters
        """
        Clearance (L/hr)
        """
        pop_CL  RealDomain(lower = 0.0, init = 0.134)
        """
        Central volume (L)
        """
        pop_Vc  RealDomain(lower = 0.0, init = 8.11)
        """
        Absorption lag time (hr)
        """
        pop_tabs  RealDomain(lower = 0.0, init = 0.523)
        """
        Lag time (hr)
        """
        pop_lag  RealDomain(lower = 0.0, init = 0.1)
        # Inter-individual variability
        """
          - ΩCL: Clearance
          - ΩVc: Central volume
          - Ωtabs: Absorption lag time
        """
        pk_Ω  PDiagDomain([0.01, 0.01, 0.01])
        # Residual variability
        """
        σ_prop: Proportional error
        """
        σ_prop  RealDomain(lower = 0.0, init = 0.00752)
        """
        σ_add: Additive error
        """
        σ_add  RealDomain(lower = 0.0, init = 0.0661)
    end
    @random begin
        pk_η ~ MvNormal(pk_Ω)    # mean = 0, covariance = pk_Ω
    end
    @covariates begin
        """
        FSZCL: Clearance scaling factor
        """
        FSZCL
        """
        FSZV: Volume scaling factor
        """
        FSZV
    end
    @pre begin
        CL = FSZCL * pop_CL * exp(pk_η[1])
        Vc = FSZV * pop_Vc * exp(pk_η[2])
        tabs = pop_tabs * exp(pk_η[3])
        Ka = log(2) / tabs
    end
    @dosecontrol begin
        lags = (Depot = pop_lag,)
    end
    @vars begin
        cp := Central / Vc
    end
    @dynamics Depots1Central1

    @derived begin
        """
        Concentration (ng/mL)
        """
        conc ~ @. Normal(cp, sqrt((σ_prop * cp)^2 + σ_add^2))
    end
end
PumasModel
  Parameters: pop_CL, pop_Vc, pop_tabs, pop_lag, pk_Ω, σ_prop, σ_add
  Random effects: pk_η
  Covariates: FSZCL, FSZV
  Dynamical system variables: Depot, Central
  Dynamical system type: Closed form
  Derived: conc
  Observed: conc

2.2 Data Preparation

The Warfarin data used in this tutorial is pulled from PharmaDatasets for demonstration purposes. Note how the code reshapes and prepares the data in “wide” format for reading into Pumas. Only the conc column is treated as observations for the PK model.

warfarin_data = dataset("pumas/warfarin_pumas")

# Transform the data in a single chain of operations
warfarin_data_scales = @chain warfarin_data begin
    @rtransform begin
        # Scaling factors
        :FSZV = :wtbl / 70            # volume scaling
        :FSZCL = (:wtbl / 70)^0.75     # clearance scaling (allometric)
    end
end
330×12 DataFrame
305 rows omitted
Row id time evid amt cmt conc pca wtbl age sex FSZV FSZCL
Int64 Float64 Int64 Float64? Int64? Float64? Float64? Float64 Int64 String1 Float64 Float64
1 1 0.0 1 100.0 1 missing missing 66.7 50 M 0.952857 0.96443
2 1 0.5 0 missing missing 0.0 missing 66.7 50 M 0.952857 0.96443
3 1 1.0 0 missing missing 1.9 missing 66.7 50 M 0.952857 0.96443
4 1 2.0 0 missing missing 3.3 missing 66.7 50 M 0.952857 0.96443
5 1 3.0 0 missing missing 6.6 missing 66.7 50 M 0.952857 0.96443
6 1 6.0 0 missing missing 9.1 missing 66.7 50 M 0.952857 0.96443
7 1 9.0 0 missing missing 10.8 missing 66.7 50 M 0.952857 0.96443
8 1 12.0 0 missing missing 8.6 missing 66.7 50 M 0.952857 0.96443
9 1 24.0 0 missing missing 5.6 44.0 66.7 50 M 0.952857 0.96443
10 1 36.0 0 missing missing 4.0 27.0 66.7 50 M 0.952857 0.96443
11 1 48.0 0 missing missing 2.7 28.0 66.7 50 M 0.952857 0.96443
12 1 72.0 0 missing missing 0.8 31.0 66.7 50 M 0.952857 0.96443
13 1 96.0 0 missing missing missing 60.0 66.7 50 M 0.952857 0.96443
319 32 48.0 0 missing missing 6.9 24.0 62.0 21 M 0.885714 0.912999
320 32 72.0 0 missing missing 4.4 23.0 62.0 21 M 0.885714 0.912999
321 32 96.0 0 missing missing 3.5 20.0 62.0 21 M 0.885714 0.912999
322 32 120.0 0 missing missing 2.5 22.0 62.0 21 M 0.885714 0.912999
323 33 0.0 1 100.0 1 missing missing 66.7 50 M 0.952857 0.96443
324 33 0.0 0 missing missing missing 100.0 66.7 50 M 0.952857 0.96443
325 33 24.0 0 missing missing 9.2 49.0 66.7 50 M 0.952857 0.96443
326 33 36.0 0 missing missing 8.5 32.0 66.7 50 M 0.952857 0.96443
327 33 48.0 0 missing missing 6.4 26.0 66.7 50 M 0.952857 0.96443
328 33 72.0 0 missing missing 4.8 22.0 66.7 50 M 0.952857 0.96443
329 33 96.0 0 missing missing 3.1 28.0 66.7 50 M 0.952857 0.96443
330 33 120.0 0 missing missing 2.5 33.0 66.7 50 M 0.952857 0.96443

3 Creating a Pumas Population

Below is the creation of a population object in Pumas using read_pumas. Only the conc data are treated as the observation variable:

pop_pk = read_pumas(
    warfarin_data_scales;
    id = :id,
    time = :time,
    amt = :amt,
    cmt = :cmt,
    evid = :evid,
    covariates = [:sex, :wtbl, :FSZV, :FSZCL],
    observations = [:conc],
)
Population
  Subjects: 32
  Covariates: sex, wtbl, FSZV, FSZCL
  Observations: conc
Note

The same data can contain multiple endpoints or PD observations. In this tutorial, the focus is solely on PK fitting. PKPD modeling on this warfarin dataset will be introduced later.

Also note, that parameter inference can be expensive and for that reason we simplified the model for this tutorial to decrease overall runtime.

3.1 Obtaining fit results

Following the examples in previous tutorials, we perform a fit. We need the output of the fit function call to perform inference to obtain parameter uncertainty estimates.

# A named tuple of parameter values
param_vals = (
    pop_CL = 0.134,
    pop_Vc = 8.11,
    pop_tabs = 0.523,
    pop_lag = 0.1,
    pk_Ω = Diagonal([0.01, 0.01, 0.01]),
    σ_prop = 0.00752,
    σ_add = 0.0661,
)
foce_fit = fit(warfarin_pk_model, pop_pk, param_vals, FOCE();)
[ Info: Checking the initial parameter values.
[ Info: The initial negative log likelihood and its gradient are finite. Check passed.
Iter     Function value   Gradient norm 
     0     1.209064e+04     1.489225e+04
 * time: 0.040174007415771484
     1     2.643772e+03     3.167516e+03
 * time: 1.6518681049346924
     2     1.836601e+03     2.118430e+03
 * time: 1.6754789352416992
     3     9.351337e+02     8.722439e+02
 * time: 1.699368953704834
     4     6.402300e+02     4.199225e+02
 * time: 1.7167229652404785
     5     5.103664e+02     1.642121e+02
 * time: 1.7334730625152588
     6     4.760464e+02     5.453749e+01
 * time: 1.7501270771026611
     7     4.703757e+02     3.643518e+01
 * time: 1.766023874282837
     8     4.699019e+02     3.135992e+01
 * time: 1.9424278736114502
     9     4.697614e+02     2.953531e+01
 * time: 1.958122968673706
    10     4.693153e+02     2.463233e+01
 * time: 1.9741499423980713
    11     4.685743e+02     2.580427e+01
 * time: 1.990570068359375
    12     4.675133e+02     3.864937e+01
 * time: 2.006901979446411
    13     4.666775e+02     5.495470e+01
 * time: 2.0232460498809814
    14     4.661197e+02     5.692101e+01
 * time: 2.039592981338501
    15     4.656782e+02     4.770992e+01
 * time: 2.055497884750366
    16     4.651802e+02     3.087698e+01
 * time: 2.0714120864868164
    17     4.645523e+02     1.184834e+01
 * time: 2.087682008743286
    18     4.641447e+02     1.162249e+01
 * time: 2.1036109924316406
    19     4.639978e+02     1.125144e+01
 * time: 2.119882106781006
    20     4.639307e+02     1.156463e+01
 * time: 2.1352150440216064
    21     4.638001e+02     1.312870e+01
 * time: 2.1508688926696777
    22     4.635282e+02     1.480920e+01
 * time: 2.1693289279937744
    23     4.630353e+02     2.169377e+01
 * time: 2.250530958175659
    24     4.623847e+02     4.478029e+01
 * time: 2.2672479152679443
    25     4.617426e+02     6.468975e+01
 * time: 2.2842118740081787
    26     4.610293e+02     7.776996e+01
 * time: 2.3015289306640625
    27     4.597628e+02     8.785260e+01
 * time: 2.319093942642212
    28     4.566753e+02     9.769803e+01
 * time: 2.3372249603271484
    29     4.490421e+02     1.008838e+02
 * time: 2.3560070991516113
    30     4.391868e+02     9.978816e+01
 * time: 2.3759450912475586
    31     4.130704e+02     5.917685e+01
 * time: 2.3956398963928223
    32     4.055780e+02     3.852824e+01
 * time: 2.4145100116729736
    33     4.023118e+02     3.889618e+01
 * time: 2.435878038406372
    34     4.012516e+02     3.694778e+01
 * time: 2.4673969745635986
    35     4.004391e+02     2.061948e+01
 * time: 2.5096659660339355
    36     3.983040e+02     3.508423e+01
 * time: 2.5285260677337646
    37     3.969705e+02     3.841039e+01
 * time: 2.5480399131774902
    38     3.965462e+02     3.738343e+01
 * time: 2.5665180683135986
    39     3.950409e+02     3.064789e+01
 * time: 2.586111068725586
    40     3.945750e+02     2.876429e+01
 * time: 2.604630947113037
    41     3.937725e+02     2.571438e+01
 * time: 2.6235079765319824
    42     3.933955e+02     2.436112e+01
 * time: 2.641603946685791
    43     3.927564e+02     2.051069e+01
 * time: 2.6593120098114014
    44     3.916020e+02     1.629035e+01
 * time: 2.6773688793182373
    45     3.886991e+02     2.689824e+01
 * time: 2.6980130672454834
    46     3.870054e+02     2.298582e+01
 * time: 2.733448028564453
    47     3.853691e+02     2.614992e+01
 * time: 2.752039909362793
    48     3.841730e+02     2.207557e+01
 * time: 2.7701950073242188
    49     3.825113e+02     2.204399e+01
 * time: 2.789194107055664
    50     3.808880e+02     2.444784e+01
 * time: 2.808298110961914
    51     3.800407e+02     1.250611e+01
 * time: 2.826965093612671
    52     3.798092e+02     1.167926e+01
 * time: 2.844964027404785
    53     3.797789e+02     1.162382e+01
 * time: 2.8627378940582275
    54     3.797069e+02     1.152441e+01
 * time: 2.8961780071258545
    55     3.794424e+02     1.132717e+01
 * time: 2.914180040359497
    56     3.788131e+02     2.006438e+01
 * time: 2.932313919067383
    57     3.771525e+02     3.584695e+01
 * time: 2.950850009918213
    58     3.731299e+02     5.697249e+01
 * time: 2.9696619510650635
    59     3.658671e+02     6.542042e+01
 * time: 2.9892330169677734
    60     3.604194e+02     4.036489e+01
 * time: 3.008502960205078
    61     3.532841e+02     1.574006e+01
 * time: 3.026911973953247
    62     3.520181e+02     1.393300e+01
 * time: 3.0579800605773926
    63     3.517984e+02     6.701188e+00
 * time: 3.0761570930480957
    64     3.517541e+02     3.503978e+00
 * time: 3.093985080718994
    65     3.516436e+02     8.720957e+00
 * time: 3.1134140491485596
    66     3.511845e+02     1.406200e+01
 * time: 3.138350009918213
    67     3.510647e+02     2.540378e+00
 * time: 3.156342029571533
    68     3.510209e+02     3.157201e+00
 * time: 3.1866378784179688
    69     3.509959e+02     3.045642e+00
 * time: 3.2033920288085938
    70     3.509765e+02     2.673143e+00
 * time: 3.2202560901641846
    71     3.509751e+02     2.603975e+00
 * time: 3.236598014831543
    72     3.509724e+02     2.505719e+00
 * time: 3.2529349327087402
    73     3.509666e+02     2.379768e+00
 * time: 3.2794270515441895
    74     3.509504e+02     3.572030e+00
 * time: 3.2963268756866455
    75     3.509123e+02     6.006350e+00
 * time: 3.313178062438965
    76     3.508288e+02     8.822995e+00
 * time: 3.3301520347595215
    77     3.506944e+02     9.708012e+00
 * time: 3.347249984741211
    78     3.505767e+02     6.092631e+00
 * time: 3.3646490573883057
    79     3.505358e+02     1.734431e+00
 * time: 3.3949480056762695
    80     3.505314e+02     6.749379e-01
 * time: 3.4115118980407715
    81     3.505313e+02     6.721982e-01
 * time: 3.4271841049194336
    82     3.505312e+02     6.699487e-01
 * time: 3.4424400329589844
    83     3.505307e+02     6.606824e-01
 * time: 3.4583170413970947
    84     3.505298e+02     6.413909e-01
 * time: 3.4847888946533203
    85     3.505274e+02     9.083363e-01
 * time: 3.5007948875427246
    86     3.505222e+02     1.339147e+00
 * time: 3.5171210765838623
    87     3.505129e+02     1.608661e+00
 * time: 3.5337939262390137
    88     3.505026e+02     1.293164e+00
 * time: 3.5504889488220215
    89     3.504973e+02     5.140504e-01
 * time: 3.56723690032959
    90     3.504963e+02     6.340189e-02
 * time: 3.5959079265594482
    91     3.504963e+02     3.137914e-03
 * time: 3.6115119457244873
    92     3.504963e+02     5.681551e-04
 * time: 3.6260008811950684
FittedPumasModel

Dynamical system type:                 Closed form

Number of subjects:                             32

Observation records:         Active        Missing
    conc:                       251             47
    Total:                      251             47

Number of parameters:      Constant      Optimized
                                  0              9

Likelihood approximation:                     FOCE
Likelihood optimizer:                         BFGS

Termination Reason:                   GradientNorm
Log-likelihood value:                   -350.49625

--------------------
           Estimate
--------------------
pop_CL     0.13465
pop_Vc     8.0535
pop_tabs   0.55061
pop_lag    0.87158
pk_Ω₁,₁    0.070642
pk_Ω₂,₂    0.018302
pk_Ω₃,₃    0.91326
σ_prop     0.090096
σ_add      0.39115
--------------------

3.2 Computing Parameter Precision with infer

The infer function in Pumas estimates the uncertainty (precision) of parameter estimates. Depending on the chosen method, infer can provide standard errors, confidence intervals, and correlation matrices.

The signature for infer often looks like:

infer(
    fpm::FittedPumasModel;
    level = 0.95,
    rethrow_error::Bool = false,
    sandwich_estimator::Bool = true,
)

where:

  • fpm::FittedPumasModel: The result of fit (e.g., foce_fit).
  • level: The confidence interval level (e.g., 0.95). The confidence intervals are calculated as the (1-level)/2 and (1+level)/2 quantiles of the estimated parameters
  • rethrow_error: If rethrow_error is false (the default value), no error will be thrown if the variance-covariance matrix estimator fails. If it is true, an error will be thrown if the estimator fails.
  • sandwich_estimator: Whether to use the sandwich estimator also known as the robust variance-covariance estimator. If set to true (the default value), the sandwich estimator will be used. If set to false, the standard error will be calculated using the inverse of the Hessian matrix calculated using finite difference derivatives of the gradient calculated using automatic differentiation.

An example usage:

inference_results = infer(foce_fit; level = 0.95)
[ Info: Calculating: variance-covariance matrix.
[ Info: Done.
Asymptotic inference results using sandwich estimator

Dynamical system type:                 Closed form

Number of subjects:                             32

Observation records:         Active        Missing
    conc:                       251             47
    Total:                      251             47

Number of parameters:      Constant      Optimized
                                  0              9

Likelihood approximation:                     FOCE
Likelihood optimizer:                         BFGS

Termination Reason:                   GradientNorm
Log-likelihood value:                   -350.49625

---------------------------------------------------------
           Estimate   SE          95.0% C.I.
---------------------------------------------------------
pop_CL     0.13465    0.0066546   [ 0.12161  ; 0.1477  ]
pop_Vc     8.0535     0.22108     [ 7.6201   ; 8.4868  ]
pop_tabs   0.55061    0.18702     [ 0.18406  ; 0.91717 ]
pop_lag    0.87158    0.056687    [ 0.76048  ; 0.98269 ]
pk_Ω₁,₁    0.070642   0.024577    [ 0.022472 ; 0.11881 ]
pk_Ω₂,₂    0.018302   0.0051549   [ 0.0081988; 0.028406]
pk_Ω₃,₃    0.91326    0.40637     [ 0.11678  ; 1.7097  ]
σ_prop     0.090096   0.014521    [ 0.061636 ; 0.11856 ]
σ_add      0.39115    0.065398    [ 0.26297  ; 0.51932 ]
---------------------------------------------------------

This is the usual asymptotic variance-covariance estimator and we already saw this previous tutorials.

To get a matrix representation of this, use vcov()

vcov(inference_results)
9×9 Symmetric{Float64, Matrix{Float64}}:
  4.42841e-5    0.000217445   0.000302094  …   3.99916e-6    0.00019736
  0.000217445   0.0488775     0.00571323      -0.000846166  -0.0056657
  0.000302094   0.00571323    0.0349767        0.000227818   0.00412692
 -7.40855e-5   -0.00207014   -0.00450616       0.000458813   0.000494683
  0.000120614   5.09406e-5    0.00164596      -9.1424e-5     0.000734901
  2.90008e-7    0.000292148  -0.000131446  …   3.99746e-6    1.80866e-5
 -0.000263152  -0.023877     -0.0275659        0.00328879    0.0126135
  3.99916e-6   -0.000846166   0.000227818      0.000210856   0.000518153
  0.00019736   -0.0056657     0.00412692       0.000518153   0.00427687

and to get the condition number of the correlation matrix implied by vcov use

cond(inference_results)
50.11623683487897

Some users request the condition number of the covariance matrix itself and even if the use is often misguided it can be calculated as well.

cond(inference_results; correlation = false)
13082.75373321667

It is also possible to calculate the correlation matrix from the vcov output using the cov2cor function

cor_from_cov = cov2cor(vcov(inference_results))
9×9 Symmetric{Float64, Matrix{Float64}}:
  1.0          0.147799     0.242733  …  -0.0973098   0.0413859   0.453494
  0.147799     1.0          0.138178     -0.265766   -0.263578   -0.391865
  0.242733     0.138178     1.0          -0.362707    0.083889    0.337422
 -0.196394    -0.165183    -0.425047      0.555027    0.557394    0.133439
  0.737483     0.00937536   0.358102     -0.28125    -0.25618     0.45724
  0.00845409   0.256348    -0.136345  …   0.315212    0.0534038   0.0536508
 -0.0973098   -0.265766    -0.362707      1.0         0.557335    0.47462
  0.0413859   -0.263578     0.083889      0.557335    1.0         0.545635
  0.453494    -0.391865     0.337422      0.47462     0.545635    1.0

And we see that the cond call above matches the condition number of the correlation matrix

cond(cor_from_cov)
50.1162368348788

3.2.1 Failure of the asymptotic variance-covariance matrix

It is well-known that the asymptotic variance-covariance matrix can sometimes fail to compute. This can happen for a variety of reasons including:

  1. There are parameters very close to a bound (often 0)
  2. The parameter vector does not represent a local minimum (optimization failed)
  3. The parameter vector does represent a local minimum but it’s not the global solution

The first one is often easy to check. The problematic parameters can be ones than have lower or upper bounds set. Often this will be a variance of standard deviation that has moved very close to the lower boundary. Consider removing the associated random effect if the problematic parameter is a variance in its specification or error model component if a combined additive and proportional error model is used and a standard deviation is close to zero.

It is also possible that the parameters do not represent a local minimum. In other words, they come from a failed fit. In this case, it can often be hard to perform the associated calculations in a stable way, but most importantly the results would not be interpretable even if they can be calculated in this case. The formulas are only valid for parameters that represent the actual (maximum likelihood) estimates. Please try to restart the optimization at different starting points in this case.

If you have reasons to believe that the model should indeed be a combined error model or if the random effect should be present it is also possible that the model converged to a local minimum that is not the global minimum. If the optimization happened to move to a region of the parameter state space that is hard to get out of you will often have to restart the fit at different parameter values. It is not possible to verify if the minimum is global in general, but it is always advised to try out more than one set of initial parameters when fitting models.

3.2.2 Bootstrap

Sometimes it is appropriate to use a different method to calculate estimate the uncertainty of the estimated parameters. Bootstrapping is a very popular approach that is simply but can often come a quite significant computational cost. Researchers often perform a bootstrapping step if their computational budget allows it or if the asymptotic variance-covariance estimator fails. Bootstrapping is advantageous because it does not rely on any invertability of matrices and it cannot produce negative variance confidence intervals because the resampled estimator respects the bounds of the model.

The signature for bootstrapping in infer looks as follows.

infer(fpm::FittedPumasModel, bts::Bootstrap; level = 0.95)

This does not help much before also looking at the interface for Bootstrap itself.

Bootstrap(;
    rng = Random.default_rng,
    samples = 200,
    stratify_by = nothing,
    ensemblealg = EnsembleThreads(),
)

Bootstrap accepts a random number generator rng, the number of resampled datasets to produce samples, if sampling should be stratified according to the covariates in stratify_by, and finally the ensemble algorithm to control parallelization across fits. On the JuliaHub platform this can be used together with distributed computing to perform many resampled estimations in a short time.

bootstrap_results = infer(foce_fit, Bootstrap(samples = 50); level = 0.95)
Warning: Terminated early due to NaN in gradient.
@ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/7krni/src/multivariate/optimize/optimize.jl:117
Warning: Terminated early due to NaN in gradient.
@ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/7krni/src/multivariate/optimize/optimize.jl:117
Warning: Terminated early due to NaN in gradient.
@ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/7krni/src/multivariate/optimize/optimize.jl:117
Warning: Terminated early due to NaN in gradient.
@ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/7krni/src/multivariate/optimize/optimize.jl:117
Warning: Terminated early due to NaN in gradient.
@ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/7krni/src/multivariate/optimize/optimize.jl:117
Warning: Terminated early due to NaN in gradient.
@ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/7krni/src/multivariate/optimize/optimize.jl:117
Warning: Terminated early due to NaN in gradient.
@ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/7krni/src/multivariate/optimize/optimize.jl:117
Warning: Terminated early due to NaN in gradient.
@ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/7krni/src/multivariate/optimize/optimize.jl:117
Warning: Terminated early due to NaN in gradient.
@ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/7krni/src/multivariate/optimize/optimize.jl:117
Warning: Terminated early due to NaN in gradient.
@ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/7krni/src/multivariate/optimize/optimize.jl:117
Warning: Terminated early due to NaN in gradient.
@ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/7krni/src/multivariate/optimize/optimize.jl:117
Warning: Terminated early due to NaN in gradient.
@ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/7krni/src/multivariate/optimize/optimize.jl:117
Warning: Terminated early due to NaN in gradient.
@ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/7krni/src/multivariate/optimize/optimize.jl:117
Warning: Terminated early due to NaN in gradient.
@ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/7krni/src/multivariate/optimize/optimize.jl:117
Warning: Terminated early due to NaN in gradient.
@ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/7krni/src/multivariate/optimize/optimize.jl:117
Info: Bootstrap inference finished.
  Total resampled fits = 50
  Success rate = 1.0
  Unique resampled populations = 50
Bootstrap inference results

Dynamical system type:                 Closed form

Number of subjects:                             32

Observation records:         Active        Missing
    conc:                       251             47
    Total:                      251             47

Number of parameters:      Constant      Optimized
                                  0              9

Likelihood approximation:                     FOCE
Likelihood optimizer:                         BFGS

Termination Reason:                   GradientNorm
Log-likelihood value:                   -350.49625

--------------------------------------------------------
           Estimate   SE          95.0% C.I.
--------------------------------------------------------
pop_CL     0.13465    0.0069545   [ 0.12367 ; 0.14903 ]
pop_Vc     8.0535     1.3516      [ 7.6634  ; 8.5087  ]
pop_tabs   0.55061    0.21662     [ 0.15407 ; 0.97045 ]
pop_lag    0.87158    0.12747     [ 0.57722 ; 0.98395 ]
pk_Ω₁,₁    0.070642   0.026192    [ 0.020862; 0.12609 ]
pk_Ω₂,₂    0.018302   2.1748e14   [ 0.010253; 0.028208]
pk_Ω₃,₃    0.91326    0.82661     [ 0.22948 ; 3.6351  ]
σ_prop     0.090096   0.017894    [ 0.059196; 0.11581 ]
σ_add      0.39115    0.096061    [ 0.21259 ; 0.52427 ]
--------------------------------------------------------
Successful fits: 50 out of 50
Unique resampled populations: 50
No stratification.

Again, we can calculate a covariance matrix based on the samples with vcov

vcov(bootstrap_results)
9×9 Symmetric{Float64, Matrix{Float64}}:
  4.83649e-5    0.00156649   0.00064332   …   1.05506e-5    7.17266e-5
  0.00156649    1.82695      0.0165899        0.0145297    -0.079902
  0.00064332    0.0165899    0.0469264        0.000290836   0.00214464
 -0.000335938  -0.0643749   -0.0148099       -0.000278331   0.00541825
  0.000151952   0.0103243    0.00305516       7.50115e-5    0.00042322
  2.00138e11    2.8966e14    1.55351e12   …   2.59113e12   -1.13065e13
 -0.00123383   -0.234299    -0.115269         0.00163379    0.0224629
  1.05506e-5    0.0145297    0.000290836      0.000320184  -0.000275124
  7.17266e-5   -0.079902     0.00214464      -0.000275124   0.0092278

and we can even get a DataFrame that includes all the estimated parameters from the sampled population fits

DataFrame(bootstrap_results.vcov)
50×9 DataFrame
25 rows omitted
Row pop_CL pop_Vc pop_tabs pop_lag pk_Ω₁,₁ pk_Ω₂,₂ pk_Ω₃,₃ σ_prop σ_add
Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64
1 0.126633 8.2454 0.208499 0.968698 0.0452462 0.022725 1.99566 0.0994534 0.297097
2 0.142345 17.4745 0.581171 0.559044 0.121901 1.53781e15 0.0522443 0.176482 3.47492e-83
3 0.129743 7.66233 0.321423 1.39319 0.043103 0.0176078 2.02135 0.116853 0.538741
4 0.129183 7.70203 0.171302 0.980253 0.0469575 0.0174372 2.24075 0.0939945 0.320679
5 0.139977 7.85534 0.882448 0.897065 0.100464 0.0137745 0.753898 0.106596 0.524524
6 0.141935 7.63955 0.651021 0.858456 0.100478 0.0131245 0.792246 0.104594 0.486655
7 0.137902 7.87941 0.365576 0.858931 0.0627549 0.0151161 0.909492 0.0916048 0.364872
8 0.137627 7.83692 0.325784 0.872645 0.0662821 0.0265132 1.95468 0.0995674 0.460847
9 0.126958 8.16407 0.507714 0.856218 0.0691286 0.0250932 1.43943 0.0793042 0.379192
10 0.122033 8.2395 0.621112 0.887475 0.0129473 0.0212474 1.17782 0.0984797 0.325865
11 0.133583 8.22255 0.184489 0.984979 0.0472109 0.0199437 3.59088 0.107138 0.36427
12 0.141037 8.14233 0.854026 0.841431 0.0919972 0.019965 0.510299 0.104021 0.442929
13 0.135681 8.25597 0.650823 0.809248 0.0644645 0.0207617 0.47461 0.0647908 0.3002
39 0.142109 7.84883 0.733299 0.783871 0.100401 0.0221625 0.869036 0.112195 0.467019
40 0.135565 8.21299 0.541418 0.852501 0.0767428 0.0158428 0.494233 0.0576773 0.34197
41 0.146009 8.15419 0.504974 0.872607 0.0944353 0.0204069 0.28735 0.0750978 0.359405
42 0.134025 7.86251 0.598954 0.570327 0.0590242 0.0152175 0.379982 0.0932072 0.29449
43 0.126411 7.86927 0.519056 0.799271 0.0431169 0.0148975 0.822717 0.098374 0.383894
44 0.134079 7.7316 0.680075 0.767894 0.0702705 0.0122202 0.67917 0.0988016 0.335701
45 0.142489 8.15711 0.149071 0.980388 0.0783335 0.0251218 3.64794 0.0918925 0.378353
46 0.130618 8.04156 0.247018 0.962794 0.0284484 0.018707 1.48227 0.0987609 0.221193
47 0.130291 8.37867 0.431187 0.879756 0.0588176 0.0127812 0.568111 0.0988174 0.239478
48 0.133382 7.71499 0.324725 0.914282 0.0777735 0.0155787 1.75871 0.106412 0.49967
49 0.133116 8.01902 0.529131 0.920349 0.066509 0.0219015 0.461582 0.0916982 0.344573
50 0.146114 8.17198 0.690028 0.819135 0.0890088 0.017714 0.52526 0.0817504 0.361424

This is very useful for histogram plotting of parameter distributions.

3.2.3 Sampling Importance Re-sampling

Pumas has support for inference through Sampling Importance Re-sampling through the SIR() input to infer. The signature for SIR in infer looks as follows.

infer(fpm::FittedPumasModel, sir::SIR; level = 0.95, ensemblealg = EnsembleThreads())

This performs sampling importance re-sampling for the population in fpm. The confidence intervals are calculated as the (1-level)/2 and (1+level)/2 quantiles of the sampled parameters. ensemblealg can be EnsembleThreads() (the default value) to use multi-threading or EnsembleSerial() to use a single thread.

The signature for the SIR specification is

SIR(; rng, samples, resamples)

SIR accepts a random number generator rng, the number of samples from the proposal, samples, can be set and to complete the specification the resample has to be set. It is suggested that samples is at least 5 times larger than resamples in practice to have sufficient samples to resample from.

sir_results = infer(foce_fit, SIR(samples = 1000, resamples = 200); level = 0.95)
[ Info: Calculating: variance-covariance matrix.
[ Info: Done.
[ Info: Running SIR.
[ Info: Resampling.
Simulated inference results

Dynamical system type:                 Closed form

Number of subjects:                             32

Observation records:         Active        Missing
    conc:                       251             47
    Total:                      251             47

Number of parameters:      Constant      Optimized
                                  0              9

Likelihood approximation:                     FOCE
Likelihood optimizer:                         BFGS

Termination Reason:                   GradientNorm
Log-likelihood value:                   -350.49625

---------------------------------------------------------
           Estimate   SE          95.0% C.I.
---------------------------------------------------------
pop_CL     0.13465    0.0057638   [ 0.12573  ; 0.14803 ]
pop_Vc     8.0535     0.2073      [ 7.6619   ; 8.4511  ]
pop_tabs   0.55061    0.15247     [ 0.25193  ; 0.84939 ]
pop_lag    0.87158    0.039711    [ 0.77815  ; 0.93949 ]
pk_Ω₁,₁    0.070642   0.016792    [ 0.045127 ; 0.11117 ]
pk_Ω₂,₂    0.018302   0.0051341   [ 0.0099781; 0.029576]
pk_Ω₃,₃    0.91326    0.29186     [ 0.51588  ; 1.5607  ]
σ_prop     0.090096   0.0076219   [ 0.077837 ; 0.10757 ]
σ_add      0.39115    0.03583     [ 0.34596  ; 0.4794  ]
---------------------------------------------------------

Notice, that SIR bases its first samples number of samples from a truncated multivariate normal distribution with mean of the maximum likelihood population level parameters and covariance matrix that is the asymptotic matrix calculated by infer(fpm). This means that to use SIR the matrix is question has to be successfully calculated by infer(fpm) under the hood.

The methods for vcov and DataFrame(sir_results.vcov) that we saw for Bootstrap also applies here

vcov(sir_results)
9×9 Symmetric{Float64, Matrix{Float64}}:
  3.32215e-5   0.00035675   -0.00014213   …   1.54466e-6    4.61375e-5
  0.00035675   0.0429741     0.00500241       8.41146e-5   -0.00114543
 -0.00014213   0.00500241    0.0232472       -3.20658e-5    0.00120362
  2.06855e-5  -3.20011e-5   -0.00289462       2.733e-6      1.90243e-5
  5.3617e-5    0.000241812   0.000217077     -4.56506e-5    0.000260069
  1.78638e-7   0.000293942   2.39619e-5   …  -1.96089e-6    3.26251e-6
  9.9374e-5   -0.000599248  -0.0170095        0.000528837   0.00345498
  1.54466e-6   8.41146e-5   -3.20658e-5       5.8093e-5    -9.31452e-6
  4.61375e-5  -0.00114543    0.00120362      -9.31452e-6    0.00128377

and

DataFrame(sir_results.vcov)
200×9 DataFrame
175 rows omitted
Row pop_CL pop_Vc pop_tabs pop_lag pk_Ω₁,₁ pk_Ω₂,₂ pk_Ω₃,₃ σ_prop σ_add
Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64
1 0.140508 8.20068 0.409156 0.845905 0.0608777 0.0209108 1.06931 0.109655 0.386555
2 0.137945 7.82772 0.344418 0.886041 0.094169 0.020994 0.861486 0.0863098 0.339881
3 0.13494 8.02326 0.747288 0.846647 0.0649128 0.0172251 0.716548 0.100662 0.364372
4 0.141068 7.85067 0.78469 0.81168 0.0968588 0.027783 0.479766 0.0851472 0.393093
5 0.1393 7.79483 0.354509 0.836893 0.101666 0.0225861 1.33078 0.0897428 0.417486
6 0.134664 7.99295 0.277951 0.868544 0.0594312 0.018651 1.7399 0.104571 0.437451
7 0.134258 8.22943 0.665332 0.91468 0.0775037 0.0230833 1.37048 0.0931842 0.413196
8 0.133964 8.26702 0.492129 0.870172 0.0837631 0.0159115 1.22462 0.088322 0.381315
9 0.147951 8.21433 0.658627 0.866207 0.0962308 0.0151633 0.553559 0.0842621 0.385726
10 0.137347 8.19341 0.651507 0.904939 0.0730112 0.022827 1.4006 0.0897206 0.416005
11 0.143722 8.42359 0.512354 0.921094 0.0900185 0.0318578 1.38446 0.0957107 0.409034
12 0.133738 8.06674 0.477571 0.850372 0.110013 0.0144534 0.897113 0.0746473 0.445651
13 0.143014 7.97018 0.55312 0.886349 0.0794491 0.0268673 1.08398 0.0956146 0.404677
189 0.126585 8.08207 0.62054 0.78139 0.0758827 0.0144785 0.766255 0.0802123 0.413584
190 0.140629 8.01159 0.643504 0.776443 0.0844378 0.0100397 1.07266 0.0974287 0.448844
191 0.134441 7.97372 0.640053 0.902385 0.0658095 0.020857 0.90929 0.0991377 0.382156
192 0.138334 8.05436 0.589931 0.810537 0.0929078 0.010947 1.15099 0.0852702 0.445203
193 0.130104 8.30811 0.52506 0.812338 0.0335241 0.0330931 1.12353 0.0983875 0.327373
194 0.129767 8.23543 0.737301 0.883207 0.0688149 0.00616843 1.1011 0.103411 0.465953
195 0.138709 8.18901 0.615274 0.892393 0.0950863 0.0234772 1.04428 0.092368 0.457352
196 0.134358 8.05776 0.49792 0.956675 0.0546478 0.0223467 2.00295 0.0975638 0.474696
197 0.133336 8.27967 0.634338 0.892742 0.057272 0.0232192 0.883752 0.0926312 0.348719
198 0.135551 8.05469 0.317257 0.938722 0.0601095 0.0283266 1.28788 0.0980568 0.403095
199 0.147227 8.06584 0.557798 0.848538 0.0692308 0.0188421 1.24191 0.112855 0.479208
200 0.140353 8.49541 0.461573 0.914758 0.0813025 0.02484 1.30072 0.0860687 0.412141

3.2.4 Marginal MCMC

An alternative to Bootstrap and SIR is to simply use the MarginalMCMC sampler which is a Hamiltonian Monte Carlo (HMC) No-U-Turn Sampler (NUTS) that will sample from the marginal loglikelihood. This means that individual effects are marginalized out and then we sample the population level parameters. This does not resample populations like Bootstrap so inference may be more stable if many resampled populations lead to extreme estimates and it differs from SIR in that it does not need the asymptotic covariance matrix to be calculated and sampled from.

This method requires slightly more understanding from the user when setting the options that can be found through the docstring of MarginalMCMC. Some knowledge of Bayesian inference is advised.

inference_results = infer(foce_fit, MarginalMCMC(); level = 0.95)

As sampling based inference can be computationally intensive we exclude the actual invocation of this method from this tutorial.

4 Concluding Remarks

This tutorial showcased a typical Pumas workflow for parameter inference in Pumas models. We showed the different methods supported by Pumas for calculating parameter uncertainty.