using Pumas
using PharmaDatasets
using DataFramesMeta
using PumasUtilitiesCalculating Parameter Uncertainty
1 Introduction
A typical workflow for fitting a Pumas model and deriving parameter precision typically involves:
- Preparing the data and the model.
- Checking model-data compatibility.
- Obtaining initial parameter estimates.
- Fitting the model via a chosen estimation method.
- Interpreting the fit results.
- Computing parameter uncertainty based on the asymptotic variance-covariance formulas (robust or not).
- (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:
FSZCLandFSZV - Differential equations describing the PK behavior in the compartments.
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 ~ @. CombinedNormal(cp, σ_add, σ_prop)
end
endPumasModel
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| 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
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.04807090759277344 1 2.643772e+03 3.167516e+03 * time: 2.630375862121582 2 1.836601e+03 2.118430e+03 * time: 2.6559369564056396 3 9.351337e+02 8.722439e+02 * time: 2.681324005126953 4 6.402300e+02 4.199225e+02 * time: 2.7062768936157227 5 5.103664e+02 1.642121e+02 * time: 2.7309160232543945 6 4.760464e+02 5.453749e+01 * time: 2.7548038959503174 7 4.703757e+02 3.643518e+01 * time: 4.733490943908691 8 4.699019e+02 3.135992e+01 * time: 4.752802848815918 9 4.697614e+02 2.953531e+01 * time: 4.771333932876587 10 4.693153e+02 2.463233e+01 * time: 4.7894368171691895 11 4.685743e+02 2.580427e+01 * time: 4.8087639808654785 12 4.675133e+02 3.864937e+01 * time: 4.828780889511108 13 4.666775e+02 5.495470e+01 * time: 4.848706007003784 14 4.661197e+02 5.692101e+01 * time: 4.869323015213013 15 4.656782e+02 4.770992e+01 * time: 4.888653993606567 16 4.651802e+02 3.087698e+01 * time: 4.907728910446167 17 4.645523e+02 1.184834e+01 * time: 4.927351951599121 18 4.641447e+02 1.162249e+01 * time: 4.948163032531738 19 4.639978e+02 1.125144e+01 * time: 4.970925807952881 20 4.639307e+02 1.156463e+01 * time: 4.9937310218811035 21 4.638001e+02 1.312870e+01 * time: 5.01677393913269 22 4.635282e+02 1.480920e+01 * time: 5.038254022598267 23 4.630353e+02 2.169377e+01 * time: 5.0582098960876465 24 4.623847e+02 4.478029e+01 * time: 5.077876806259155 25 4.617426e+02 6.468975e+01 * time: 5.0987138748168945 26 4.610293e+02 7.776996e+01 * time: 5.120874881744385 27 4.597628e+02 8.785260e+01 * time: 5.141788005828857 28 4.566753e+02 9.769803e+01 * time: 5.163262844085693 29 4.490421e+02 1.008838e+02 * time: 5.18592381477356 30 4.391868e+02 9.978816e+01 * time: 5.209959983825684 31 4.130704e+02 5.917685e+01 * time: 5.234653949737549 32 4.055780e+02 3.852824e+01 * time: 5.342396974563599 33 4.023118e+02 3.889618e+01 * time: 5.367537975311279 34 4.012516e+02 3.694778e+01 * time: 5.4082818031311035 35 4.004391e+02 2.061948e+01 * time: 5.4308319091796875 36 3.983040e+02 3.508423e+01 * time: 5.4529759883880615 37 3.969705e+02 3.841039e+01 * time: 5.475142955780029 38 3.965462e+02 3.738343e+01 * time: 5.496229887008667 39 3.950409e+02 3.064789e+01 * time: 5.518172979354858 40 3.945750e+02 2.876429e+01 * time: 5.5390520095825195 41 3.937725e+02 2.571438e+01 * time: 5.560366868972778 42 3.933955e+02 2.436112e+01 * time: 5.58204984664917 43 3.927564e+02 2.051069e+01 * time: 5.602881908416748 44 3.916020e+02 1.629035e+01 * time: 5.624118804931641 45 3.886991e+02 2.689824e+01 * time: 5.644366979598999 46 3.870054e+02 2.298582e+01 * time: 5.663103818893433 47 3.853691e+02 2.614992e+01 * time: 5.707810878753662 48 3.841730e+02 2.207557e+01 * time: 5.727065801620483 49 3.825113e+02 2.204399e+01 * time: 5.746184825897217 50 3.808880e+02 2.444784e+01 * time: 5.766003847122192 51 3.800407e+02 1.250611e+01 * time: 5.785405874252319 52 3.798092e+02 1.167926e+01 * time: 5.804163932800293 53 3.797789e+02 1.162382e+01 * time: 5.8227620124816895 54 3.797069e+02 1.152441e+01 * time: 5.840788841247559 55 3.794424e+02 1.132717e+01 * time: 5.859774827957153 56 3.788131e+02 2.006438e+01 * time: 5.878332853317261 57 3.771525e+02 3.584695e+01 * time: 5.897320985794067 58 3.731299e+02 5.697249e+01 * time: 5.917426824569702 59 3.658671e+02 6.542042e+01 * time: 5.937747001647949 60 3.604194e+02 4.036489e+01 * time: 5.978269815444946 61 3.532841e+02 1.574006e+01 * time: 5.9998250007629395 62 3.520181e+02 1.393300e+01 * time: 6.021374940872192 63 3.517984e+02 6.701188e+00 * time: 6.041980981826782 64 3.517541e+02 3.503978e+00 * time: 6.0625319480896 65 3.516436e+02 8.720957e+00 * time: 6.083197832107544 66 3.511845e+02 1.406200e+01 * time: 6.107919931411743 67 3.510647e+02 2.540378e+00 * time: 6.126168966293335 68 3.510209e+02 3.157201e+00 * time: 6.144171953201294 69 3.509959e+02 3.045642e+00 * time: 6.161505937576294 70 3.509765e+02 2.673143e+00 * time: 6.1968488693237305 71 3.509751e+02 2.603975e+00 * time: 6.214148998260498 72 3.509724e+02 2.505719e+00 * time: 6.231274843215942 73 3.509666e+02 2.379768e+00 * time: 6.248921871185303 74 3.509504e+02 3.572030e+00 * time: 6.2667248249053955 75 3.509123e+02 6.006350e+00 * time: 6.284411907196045 76 3.508288e+02 8.822995e+00 * time: 6.302067995071411 77 3.506944e+02 9.708012e+00 * time: 6.319978952407837 78 3.505767e+02 6.092631e+00 * time: 6.337718963623047 79 3.505358e+02 1.734431e+00 * time: 6.369365930557251 80 3.505314e+02 6.749379e-01 * time: 6.387204885482788 81 3.505313e+02 6.721982e-01 * time: 6.404048919677734 82 3.505312e+02 6.699487e-01 * time: 6.420694828033447 83 3.505307e+02 6.606824e-01 * time: 6.437394857406616 84 3.505298e+02 6.413909e-01 * time: 6.454203844070435 85 3.505274e+02 9.083363e-01 * time: 6.471344947814941 86 3.505222e+02 1.339147e+00 * time: 6.488548994064331 87 3.505129e+02 1.608661e+00 * time: 6.5224609375 88 3.505026e+02 1.293164e+00 * time: 6.5404908657073975 89 3.504973e+02 5.140504e-01 * time: 6.558198928833008 90 3.504963e+02 6.340189e-02 * time: 6.576372861862183 91 3.504963e+02 3.137914e-03 * time: 6.593740940093994 92 3.504963e+02 5.681551e-04 * time: 6.6104958057403564
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 offit(e.g.,foce_fit).level: The confidence interval level (e.g., 0.95). The confidence intervals are calculated as the(1-level)/2and(1+level)/2quantiles of the estimated parametersrethrow_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 totrue(the default value), the sandwich estimator will be used. If set tofalse, 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.11623683496925
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.753733216321
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.11623683496949
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:
- There are parameters very close to a bound (often 0)
- The parameter vector does not represent a local minimum (optimization failed)
- 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.
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.007318 [ 0.12393 ; 0.14864 ]
pop_Vc 8.0535 0.18886 [ 7.7182 ; 8.4181 ]
pop_tabs 0.55061 0.20547 [ 0.20703 ; 0.97832 ]
pop_lag 0.87158 0.16753 [ 0.5563 ; 1.3962 ]
pk_Ω₁,₁ 0.070642 0.024112 [ 0.028097 ; 0.12167 ]
pk_Ω₂,₂ 0.018302 0.004648 [ 0.0093965; 0.026484]
pk_Ω₃,₃ 0.91326 0.45909 [ 0.28885 ; 2.2622 ]
σ_prop 0.090096 0.014031 [ 0.065768 ; 0.11794 ]
σ_add 0.39115 0.083113 [ 0.23252 ; 0.497 ]
--------------------------------------------------------
Unique resampled populations: 50 out of 50
Stratification by .
Again, we can calculate a covariance matrix based on the samples with vcov
vcov(bootstrap_results)9×9 Symmetric{Float64, Matrix{Float64}}:
5.35534e-5 0.000526588 0.000363393 … 3.51342e-5 0.000232887
0.000526588 0.035669 0.012749 0.000167912 -0.000590455
0.000363393 0.012749 0.0422168 0.000707704 0.00491784
-8.87407e-5 -0.000693103 -0.011693 -5.54334e-5 0.00597319
0.000128811 0.00054429 0.001967 1.17787e-5 0.000644754
-5.87486e-6 8.79343e-5 -0.000294543 … -9.29461e-6 -1.54616e-5
-0.000242651 -0.0315627 -0.0489178 0.000704611 0.0097512
3.51342e-5 0.000167912 0.000707704 0.000196866 0.000307252
0.000232887 -0.000590455 0.00491784 0.000307252 0.0069078
and we can even get a DataFrame that includes all the estimated parameters from the sampled population fits
DataFrame(bootstrap_results.vcov)| 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.138605 | 7.8957 | 0.61819 | 0.899563 | 0.0793847 | 0.00794878 | 0.889524 | 0.095197 | 0.473749 |
| 2 | 0.141255 | 8.43289 | 1.08284 | 0.805332 | 0.0966564 | 0.0141378 | 0.288559 | 0.0996487 | 0.389441 |
| 3 | 0.140112 | 8.14507 | 0.866356 | 0.699606 | 0.130808 | 0.0106341 | 0.584682 | 0.0774065 | 0.36524 |
| 4 | 0.145865 | 8.22682 | 0.722411 | 0.804394 | 0.0876616 | 0.0101384 | 0.449542 | 0.0982301 | 0.327512 |
| 5 | 0.125549 | 8.08315 | 0.213336 | 0.88386 | 0.0351899 | 0.0199125 | 0.581266 | 0.0790613 | 0.226018 |
| 6 | 0.140798 | 7.929 | 0.67995 | 0.498887 | 0.123383 | 0.0267636 | 0.545571 | 0.0868177 | 0.319068 |
| 7 | 0.133774 | 8.22447 | 0.750283 | 0.920569 | 0.0441059 | 0.016859 | 0.653901 | 0.104632 | 0.379911 |
| 8 | 0.134981 | 8.05638 | 0.660891 | 0.726493 | 0.072265 | 0.01523 | 0.94635 | 0.0712545 | 0.317572 |
| 9 | 0.124975 | 7.70236 | 0.475108 | 0.85103 | 0.0474784 | 0.0179118 | 1.25672 | 0.0902731 | 0.382067 |
| 10 | 0.134226 | 7.80776 | 0.823588 | 0.964136 | 0.0754916 | 0.016448 | 1.232 | 0.118263 | 0.471111 |
| 11 | 0.125236 | 8.10652 | 0.613488 | 0.89103 | 0.0246407 | 0.0270565 | 1.14984 | 0.111691 | 0.320553 |
| 12 | 0.140294 | 7.89533 | 0.502416 | 0.875518 | 0.0941851 | 0.018318 | 0.462664 | 0.0794753 | 0.35946 |
| 13 | 0.133306 | 8.16105 | 0.498093 | 0.840694 | 0.0623879 | 0.0201541 | 0.643516 | 0.0714039 | 0.320308 |
| ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
| 39 | 0.128703 | 8.14686 | 0.836465 | 0.847671 | 0.0730447 | 0.0142105 | 1.11243 | 0.084295 | 0.434388 |
| 40 | 0.148653 | 8.11767 | 0.818758 | 0.880443 | 0.0905497 | 0.00923782 | 0.895422 | 0.116814 | 0.477267 |
| 41 | 0.142026 | 7.87599 | 0.651159 | 0.785371 | 0.105251 | 0.0191469 | 1.00333 | 0.0848797 | 0.431012 |
| 42 | 0.141387 | 8.25926 | 0.705095 | 0.939312 | 0.0590468 | 0.0186882 | 0.789904 | 0.113852 | 0.436806 |
| 43 | 0.140998 | 8.20895 | 0.86227 | 1.44247 | 0.0836423 | 0.0226791 | 0.799693 | 0.081046 | 0.701528 |
| 44 | 0.145075 | 7.98025 | 0.965633 | 0.542868 | 0.115756 | 0.00994323 | 0.405128 | 0.105754 | 0.254936 |
| 45 | 0.139512 | 8.36727 | 0.504903 | 0.821997 | 0.0814494 | 0.025519 | 0.683388 | 0.0616799 | 0.344199 |
| 46 | 0.135102 | 7.94908 | 0.12582 | 1.40393 | 0.0639436 | 0.0200853 | 2.70475 | 0.0823103 | 0.363263 |
| 47 | 0.14688 | 7.85921 | 0.430771 | 0.837858 | 0.1065 | 0.0103214 | 1.09176 | 0.0993948 | 0.457964 |
| 48 | 0.122249 | 7.79551 | 0.47798 | 0.931226 | 0.0260374 | 0.0166578 | 1.16476 | 0.0861424 | 0.320469 |
| 49 | 0.131743 | 8.09641 | 0.873679 | 0.8026 | 0.0797947 | 0.0137281 | 0.374015 | 0.0885568 | 0.322999 |
| 50 | 0.131349 | 7.77275 | 0.583034 | 0.835038 | 0.076252 | 0.0234827 | 1.0178 | 0.0997629 | 0.438348 |
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.0053461 [ 0.12577 ; 0.14651]
pop_Vc 8.0535 0.20686 [ 7.6622 ; 8.5006 ]
pop_tabs 0.55061 0.15169 [ 0.27332 ; 0.87778]
pop_lag 0.87158 0.041225 [ 0.77382 ; 0.93899]
pk_Ω₁,₁ 0.070642 0.016142 [ 0.045643; 0.10825]
pk_Ω₂,₂ 0.018302 0.0051396 [ 0.010675; 0.02967]
pk_Ω₃,₃ 0.91326 0.32089 [ 0.42914 ; 1.6287 ]
σ_prop 0.090096 0.0076047 [ 0.076376; 0.10732]
σ_add 0.39115 0.039066 [ 0.34067 ; 0.48667]
-------------------------------------------------------
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.8581e-5 0.00029673 -9.09871e-7 … -9.07816e-7 3.75947e-5
0.00029673 0.0427923 0.00303384 -0.00013039 -0.00261001
-9.09871e-7 0.00303384 0.0230104 0.000171038 0.000395179
6.63875e-6 -0.000554134 -0.00263481 2.63091e-5 9.07884e-5
3.92151e-5 0.000153061 0.000117752 -3.20371e-5 0.000312731
3.2684e-6 0.00016708 -7.52297e-5 … -1.10472e-5 2.10414e-5
-3.24527e-5 -0.00254273 -0.0269288 -6.94775e-5 0.00318881
-9.07816e-7 -0.00013039 0.000171038 5.78319e-5 -2.29718e-5
3.75947e-5 -0.00261001 0.000395179 -2.29718e-5 0.00152612
and
DataFrame(sir_results.vcov)| 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.148834 | 8.02681 | 0.677182 | 0.894045 | 0.0618706 | 0.0196805 | 0.833632 | 0.0923761 | 0.379526 |
| 2 | 0.123407 | 8.11705 | 0.544741 | 0.827331 | 0.0713491 | 0.0313455 | 1.55015 | 0.0849969 | 0.395499 |
| 3 | 0.129893 | 7.68071 | 0.540395 | 0.878731 | 0.0810069 | 0.0244153 | 0.720865 | 0.0753865 | 0.468424 |
| 4 | 0.132654 | 8.02556 | 0.393569 | 0.815684 | 0.0841987 | 0.0163122 | 1.08254 | 0.0889067 | 0.405191 |
| 5 | 0.131583 | 8.36759 | 0.598634 | 0.810375 | 0.0648606 | 0.0218532 | 1.34706 | 0.0862424 | 0.374545 |
| 6 | 0.142828 | 7.9624 | 0.764774 | 0.86322 | 0.0677335 | 0.0219139 | 0.840477 | 0.090357 | 0.39977 |
| 7 | 0.132105 | 7.89105 | 0.310026 | 0.852319 | 0.0938946 | 0.0329353 | 1.64907 | 0.0763842 | 0.44548 |
| 8 | 0.140689 | 8.12315 | 0.62155 | 0.884604 | 0.0835961 | 0.0201084 | 1.01081 | 0.0767262 | 0.431156 |
| 9 | 0.135067 | 8.00154 | 0.586279 | 0.807686 | 0.102373 | 0.0188927 | 0.649483 | 0.0857812 | 0.34447 |
| 10 | 0.12577 | 8.24548 | 0.604978 | 0.82272 | 0.0598739 | 0.0192316 | 0.904508 | 0.0900032 | 0.341017 |
| 11 | 0.135399 | 7.8939 | 0.421548 | 0.881832 | 0.073266 | 0.0331707 | 1.15673 | 0.0823352 | 0.400908 |
| 12 | 0.139302 | 8.16877 | 0.456889 | 0.912965 | 0.0670079 | 0.0229405 | 1.41614 | 0.0974649 | 0.385054 |
| 13 | 0.127224 | 7.95361 | 0.687725 | 0.831963 | 0.0653026 | 0.0149402 | 0.803249 | 0.100121 | 0.355874 |
| ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
| 189 | 0.1385 | 8.1888 | 0.845646 | 0.73552 | 0.0829002 | 0.022071 | 0.714751 | 0.0908622 | 0.395658 |
| 190 | 0.132445 | 8.01448 | 0.51379 | 0.896139 | 0.0816485 | 0.0135399 | 0.65619 | 0.087299 | 0.374343 |
| 191 | 0.136933 | 8.0237 | 0.253909 | 0.911829 | 0.0953016 | 0.0172296 | 1.29648 | 0.0696516 | 0.398116 |
| 192 | 0.132463 | 8.0635 | 0.631412 | 0.906857 | 0.0683555 | 0.0171024 | 0.66143 | 0.0907139 | 0.412705 |
| 193 | 0.137453 | 8.14856 | 0.37214 | 0.773924 | 0.10695 | 0.0224389 | 1.40659 | 0.0775836 | 0.433066 |
| 194 | 0.127269 | 8.02639 | 0.582777 | 0.862158 | 0.0731455 | 0.0154684 | 1.08289 | 0.0950091 | 0.40565 |
| 195 | 0.144326 | 7.88323 | 0.465608 | 0.897779 | 0.079922 | 0.0168162 | 1.21281 | 0.0984038 | 0.449549 |
| 196 | 0.132659 | 8.01706 | 0.59734 | 0.860069 | 0.0863569 | 0.021399 | 1.18842 | 0.0895817 | 0.451507 |
| 197 | 0.134792 | 8.19744 | 0.657815 | 0.898409 | 0.0658468 | 0.0169043 | 0.674749 | 0.0886008 | 0.380711 |
| 198 | 0.136712 | 8.18057 | 0.729274 | 0.829358 | 0.0998849 | 0.0240643 | 0.817538 | 0.072843 | 0.450667 |
| 199 | 0.140301 | 8.16379 | 0.578488 | 0.830088 | 0.0758107 | 0.0159641 | 1.02873 | 0.0776839 | 0.408784 |
| 200 | 0.13507 | 7.43913 | 0.547349 | 0.852778 | 0.069478 | 0.0143328 | 1.43076 | 0.0892834 | 0.455747 |
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.