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.053121089935302734
     1     2.643772e+03     3.167516e+03
 * time: 4.353262901306152
     2     1.836601e+03     2.118430e+03
 * time: 4.378635883331299
     3     9.351337e+02     8.722439e+02
 * time: 4.403965950012207
     4     6.402300e+02     4.199225e+02
 * time: 4.429430961608887
     5     5.103664e+02     1.642121e+02
 * time: 4.451404094696045
     6     4.760464e+02     5.453749e+01
 * time: 4.472965955734253
     7     4.703757e+02     3.643518e+01
 * time: 4.493746042251587
     8     4.699019e+02     3.135992e+01
 * time: 4.514163970947266
     9     4.697614e+02     2.953531e+01
 * time: 4.534344911575317
    10     4.693153e+02     2.463233e+01
 * time: 4.555310964584351
    11     4.685743e+02     2.580427e+01
 * time: 4.5756919384002686
    12     4.675133e+02     3.864937e+01
 * time: 4.596426010131836
    13     4.666775e+02     5.495470e+01
 * time: 4.618933916091919
    14     4.661197e+02     5.692101e+01
 * time: 4.644572019577026
    15     4.656782e+02     4.770992e+01
 * time: 4.669893026351929
    16     4.651802e+02     3.087698e+01
 * time: 4.6943519115448
    17     4.645523e+02     1.184834e+01
 * time: 4.720148086547852
    18     4.641447e+02     1.162249e+01
 * time: 4.745578050613403
    19     4.639978e+02     1.125144e+01
 * time: 4.7701170444488525
    20     4.639307e+02     1.156463e+01
 * time: 4.794377088546753
    21     4.638001e+02     1.312870e+01
 * time: 4.818643093109131
    22     4.635282e+02     1.480920e+01
 * time: 4.843302011489868
    23     4.630353e+02     2.169377e+01
 * time: 4.961672067642212
    24     4.623847e+02     4.478029e+01
 * time: 4.984257936477661
    25     4.617426e+02     6.468975e+01
 * time: 5.0067689418792725
    26     4.610293e+02     7.776996e+01
 * time: 5.02910304069519
    27     4.597628e+02     8.785260e+01
 * time: 5.050715923309326
    28     4.566753e+02     9.769803e+01
 * time: 5.073649883270264
    29     4.490421e+02     1.008838e+02
 * time: 5.097464084625244
    30     4.391868e+02     9.978816e+01
 * time: 5.124541997909546
    31     4.130704e+02     5.917685e+01
 * time: 5.155893087387085
    32     4.055780e+02     3.852824e+01
 * time: 5.183881998062134
    33     4.023118e+02     3.889618e+01
 * time: 5.21340799331665
    34     4.012516e+02     3.694778e+01
 * time: 5.254909992218018
    35     4.004391e+02     2.061948e+01
 * time: 5.28421688079834
    36     3.983040e+02     3.508423e+01
 * time: 5.311774015426636
    37     3.969705e+02     3.841039e+01
 * time: 5.341007947921753
    38     3.965462e+02     3.738343e+01
 * time: 5.369385004043579
    39     3.950409e+02     3.064789e+01
 * time: 5.4786388874053955
    40     3.945750e+02     2.876429e+01
 * time: 5.50225305557251
    41     3.937725e+02     2.571438e+01
 * time: 5.526115894317627
    42     3.933955e+02     2.436112e+01
 * time: 5.54985499382019
    43     3.927564e+02     2.051069e+01
 * time: 5.572654962539673
    44     3.916020e+02     1.629035e+01
 * time: 5.596206903457642
    45     3.886991e+02     2.689824e+01
 * time: 5.620804071426392
    46     3.870054e+02     2.298582e+01
 * time: 5.6438140869140625
    47     3.853691e+02     2.614992e+01
 * time: 5.667226076126099
    48     3.841730e+02     2.207557e+01
 * time: 5.690661907196045
    49     3.825113e+02     2.204399e+01
 * time: 5.717371940612793
    50     3.808880e+02     2.444784e+01
 * time: 5.746648073196411
    51     3.800407e+02     1.250611e+01
 * time: 5.7745819091796875
    52     3.798092e+02     1.167926e+01
 * time: 5.826616048812866
    53     3.797789e+02     1.162382e+01
 * time: 5.848037958145142
    54     3.797069e+02     1.152441e+01
 * time: 5.868757009506226
    55     3.794424e+02     1.132717e+01
 * time: 5.890776872634888
    56     3.788131e+02     2.006438e+01
 * time: 5.912086009979248
    57     3.771525e+02     3.584695e+01
 * time: 5.93341588973999
    58     3.731299e+02     5.697249e+01
 * time: 5.95533299446106
    59     3.658671e+02     6.542042e+01
 * time: 5.978360891342163
    60     3.604194e+02     4.036489e+01
 * time: 6.000776052474976
    61     3.532841e+02     1.574006e+01
 * time: 6.022028923034668
    62     3.520181e+02     1.393300e+01
 * time: 6.047055959701538
    63     3.517984e+02     6.701188e+00
 * time: 6.092717885971069
    64     3.517541e+02     3.503978e+00
 * time: 6.1143739223480225
    65     3.516436e+02     8.720957e+00
 * time: 6.13796591758728
    66     3.511845e+02     1.406200e+01
 * time: 6.167356967926025
    67     3.510647e+02     2.540378e+00
 * time: 6.188047885894775
    68     3.510209e+02     3.157201e+00
 * time: 6.209033966064453
    69     3.509959e+02     3.045642e+00
 * time: 6.230506896972656
    70     3.509765e+02     2.673143e+00
 * time: 6.251734972000122
    71     3.509751e+02     2.603975e+00
 * time: 6.27112603187561
    72     3.509724e+02     2.505719e+00
 * time: 6.305401086807251
    73     3.509666e+02     2.379768e+00
 * time: 6.324588060379028
    74     3.509504e+02     3.572030e+00
 * time: 6.344253063201904
    75     3.509123e+02     6.006350e+00
 * time: 6.364238977432251
    76     3.508288e+02     8.822995e+00
 * time: 6.383581876754761
    77     3.506944e+02     9.708012e+00
 * time: 6.402738094329834
    78     3.505767e+02     6.092631e+00
 * time: 6.422445058822632
    79     3.505358e+02     1.734431e+00
 * time: 6.442036867141724
    80     3.505314e+02     6.749379e-01
 * time: 6.479696989059448
    81     3.505313e+02     6.721982e-01
 * time: 6.50142502784729
    82     3.505312e+02     6.699487e-01
 * time: 6.522875070571899
    83     3.505307e+02     6.606824e-01
 * time: 6.544504880905151
    84     3.505298e+02     6.413909e-01
 * time: 6.5658838748931885
    85     3.505274e+02     9.083363e-01
 * time: 6.587253093719482
    86     3.505222e+02     1.339147e+00
 * time: 6.608867883682251
    87     3.505129e+02     1.608661e+00
 * time: 6.643795967102051
    88     3.505026e+02     1.293164e+00
 * time: 6.664731025695801
    89     3.504973e+02     5.140504e-01
 * time: 6.686349868774414
    90     3.504963e+02     6.340189e-02
 * time: 6.707606077194214
    91     3.504963e+02     3.137914e-03
 * time: 6.728453874588013
    92     3.504963e+02     5.681551e-04
 * time: 6.747812986373901
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)
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.0072513   [ 0.12047  ; 0.14762 ]
pop_Vc     8.0535     0.27439     [ 7.5916   ; 8.5223  ]
pop_tabs   0.55061    0.18583     [ 0.27962  ; 0.97564 ]
pop_lag    0.87158    0.15236     [ 0.65502  ; 1.3799  ]
pk_Ω₁,₁    0.070642   0.025184    [ 0.015682 ; 0.10846 ]
pk_Ω₂,₂    0.018302   0.0057014   [ 0.0069524; 0.028122]
pk_Ω₃,₃    0.91326    0.72401     [ 0.32434  ; 2.5986  ]
σ_prop     0.090096   0.014461    [ 0.067789 ; 0.12017 ]
σ_add      0.39115    0.072606    [ 0.24957  ; 0.51139 ]
---------------------------------------------------------
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}}:
  5.25821e-5    2.21192e-5    0.000328735  …   1.77024e-5    0.00027751
  2.21192e-5    0.0752893     0.0146347       -0.00158147   -0.0104772
  0.000328735   0.0146347     0.0345316        0.000300488   0.0025155
  7.71931e-6   -0.0111713    -0.00988743       0.000630733   0.00328267
  0.000136217  -0.000302203   0.00169022      -6.90336e-5    0.000945981
 -5.31069e-7    0.000525595  -0.000300389  …  -1.45633e-5   -4.74572e-5
 -0.000312775  -0.081522     -0.0599075        0.00530373    0.0222594
  1.77024e-5   -0.00158147    0.000300488      0.000209108   0.000505273
  0.00027751   -0.0104772     0.0025155        0.000505273   0.00527166

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.135655 8.09531 0.723392 0.76981 0.0829879 0.0125593 0.504945 0.081468 0.337014
2 0.141313 7.76679 0.725322 0.755009 0.132208 0.0179243 0.662656 0.0950127 0.434892
3 0.129578 7.90864 0.604956 0.898933 0.0400858 0.0132248 1.03431 0.110615 0.325303
4 0.138915 8.14757 0.830404 0.876447 0.0758985 0.0190915 0.670585 0.11259 0.404395
5 0.141628 7.78378 0.233512 0.908156 0.0852226 0.0273409 2.17333 0.0798403 0.439139
6 0.135901 7.85705 0.457504 0.887974 0.0643707 0.0125646 0.977419 0.0903572 0.402783
7 0.134574 8.53169 0.44694 0.879975 0.0553517 0.0198864 0.463956 0.0714972 0.237616
8 0.126475 7.74229 0.518508 0.747203 0.0682741 0.011416 0.312261 0.0710326 0.341272
9 0.14325 8.3463 0.752489 0.82721 0.0816744 0.0154706 0.449257 0.100071 0.327065
10 0.138724 7.49508 0.563806 0.921858 0.0775345 0.0166807 1.90622 0.120712 0.522768
11 0.147751 8.22582 0.607901 0.94766 0.0909949 0.0203662 0.983486 0.107894 0.461179
12 0.147992 8.4901 0.745076 0.716402 0.0875194 0.0137959 0.402646 0.0900335 0.324092
13 0.136943 7.84736 0.484166 1.39629 0.0791582 0.015795 1.21279 0.0876623 0.455068
39 0.128979 8.15299 0.610063 0.946761 0.0476978 0.0274871 2.15611 0.0952027 0.431241
40 0.138666 8.04706 0.389379 0.886289 0.0831764 0.0135139 0.365958 0.0744485 0.320968
41 0.144983 7.83283 0.591541 0.807101 0.0998208 0.0194001 1.06084 0.0951495 0.438016
42 0.124494 7.97014 0.448237 0.958107 0.0146472 0.013238 1.22321 0.0999983 0.27502
43 0.138851 7.97022 0.533094 0.895852 0.0877334 0.0165478 0.750999 0.0759199 0.398216
44 0.130762 8.10454 0.476695 1.38355 0.0773728 0.0227124 1.58534 0.0900281 0.403465
45 0.13027 7.72969 0.323921 0.968314 0.0276605 0.0217859 1.61339 0.108636 0.32418
46 0.119303 8.47044 0.384415 0.864812 0.0192453 0.024333 1.01968 0.0887914 0.290447
47 0.133901 8.63117 0.650708 0.637193 0.0823794 0.032108 0.649987 0.0664102 0.247834
48 0.14172 8.007 0.702202 0.823757 0.0947967 0.014127 0.661848 0.0978865 0.416734
49 0.142572 7.87683 1.01645 0.885258 0.0744171 0.00573492 0.403144 0.11085 0.472414
50 0.146353 7.58796 0.538174 0.918404 0.0875999 0.0125992 1.02895 0.106454 0.468807

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.0053138   [ 0.12565  ; 0.14525 ]
pop_Vc     8.0535     0.20484     [ 7.6255   ; 8.4246  ]
pop_tabs   0.55061    0.162       [ 0.23184  ; 0.86558 ]
pop_lag    0.87158    0.038097    [ 0.79259  ; 0.93344 ]
pk_Ω₁,₁    0.070642   0.017629    [ 0.047283 ; 0.10996 ]
pk_Ω₂,₂    0.018302   0.0052361   [ 0.0099247; 0.029419]
pk_Ω₃,₃    0.91326    0.3         [ 0.50994  ; 1.6129  ]
σ_prop     0.090096   0.0080173   [ 0.077456 ; 0.10788 ]
σ_add      0.39115    0.034273    [ 0.34223  ; 0.47791 ]
---------------------------------------------------------

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}}:
  2.82362e-5    0.000199627   7.04089e-5   …  -4.39593e-6    4.7719e-5
  0.000199627   0.0419611     0.00802586       5.36131e-5   -0.000757165
  7.04089e-5    0.00802586    0.0262447        0.000112214   0.000821665
  6.53524e-6    0.000145355  -0.00166748      -6.28735e-7    0.000337775
  4.82846e-5    0.000179796   0.00054256      -3.64205e-5    0.000284623
 -1.18475e-7    0.000136803  -6.54439e-5   …   1.17821e-7    2.51547e-5
 -0.000159224  -0.00563792   -0.0255239        0.000261353   0.00289314
 -4.39593e-6    5.36131e-5    0.000112214      6.42772e-5   -3.53706e-6
  4.7719e-5    -0.000757165   0.000821665     -3.53706e-6    0.00117464

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.135467 8.27219 0.447142 0.912476 0.072936 0.022553 1.11215 0.0794054 0.427842
2 0.140607 8.40374 0.779105 0.903015 0.0615872 0.0256693 0.727147 0.094959 0.409641
3 0.136851 7.85121 0.568205 0.81585 0.0902318 0.0198466 0.840192 0.0966242 0.372005
4 0.128471 8.11062 0.374316 0.865583 0.0891658 0.0236463 1.4377 0.084451 0.462991
5 0.131859 7.81027 0.414021 0.909735 0.0596216 0.0120463 1.27544 0.0777852 0.448946
6 0.132896 7.96042 0.511543 0.848601 0.0473163 0.019751 0.524463 0.0984046 0.402095
7 0.131018 7.95873 0.231875 0.876061 0.0810904 0.0176112 1.11993 0.0847211 0.337166
8 0.133792 7.71674 0.292892 0.876897 0.0654437 0.0102946 1.36546 0.0931553 0.43545
9 0.135194 7.99405 0.591318 0.892245 0.0513799 0.0207882 1.0637 0.0907462 0.412023
10 0.131934 8.13058 0.856681 0.694512 0.0770589 0.0163564 0.564371 0.0978994 0.383528
11 0.129826 8.00784 0.454967 0.865458 0.0781138 0.0176946 0.661374 0.092287 0.393923
12 0.138105 7.92409 0.843253 0.828782 0.0680509 0.025981 1.16372 0.0932036 0.438948
13 0.133335 7.95377 0.643653 0.792133 0.0650019 0.0219219 0.548065 0.0902033 0.377141
189 0.128912 8.42068 0.721579 0.862742 0.0714481 0.0193996 0.855546 0.0875884 0.371549
190 0.13514 7.90483 0.445864 0.858001 0.0487741 0.021965 1.04151 0.0867419 0.367457
191 0.142548 8.04022 0.458774 0.903464 0.107689 0.028245 1.18795 0.0896559 0.465399
192 0.12397 7.76461 0.449947 0.84043 0.0665313 0.0127125 1.16389 0.0884248 0.411184
193 0.143362 8.2206 0.545946 0.896416 0.087462 0.0246814 1.27817 0.0962316 0.499953
194 0.137591 7.96574 0.394666 0.838367 0.052019 0.0165662 0.854376 0.085668 0.326127
195 0.141646 8.26331 0.606119 0.855604 0.0882481 0.0177219 0.666899 0.089314 0.417019
196 0.138322 8.06494 0.393612 0.877799 0.0451024 0.0203099 1.30527 0.10402 0.367433
197 0.135316 8.12806 0.768041 0.892892 0.0682673 0.0147626 0.519906 0.0939125 0.389685
198 0.131811 7.95002 0.649913 0.883438 0.0879107 0.0154658 0.79821 0.0919493 0.42082
199 0.135262 7.88392 0.689797 0.829027 0.0875159 0.0294114 1.37123 0.0831895 0.477845
200 0.145708 7.99315 0.641199 0.944179 0.115268 0.0161439 0.688667 0.0972852 0.449988

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.