using Pumas
using PharmaDatasets
using DataFramesMeta
using PumasUtilities
using StatsBase # for cov2cor
Calculating 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 precision.
- (Optionally) proceeding with more advanced techniques like bootstrapping or SIR for robust uncertainty quantification.
The following sections will walk through these steps using a one-compartment PK model for Warfarin, focusing on the PK aspects only. 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
andFSZV
- Differential equations describing the PK behavior in the compartments.
= @model begin
warfarin_pk_model @metadata begin
= "Warfarin 1-compartment PK model (PD removed)"
desc = u"hr"
timeu end
@param begin
# PK parameters
"""
Clearance (L/hr)
"""
∈ RealDomain(lower = 0.0, upper = 13.4, init = 0.134)
pop_CL """
Central volume (L)
"""
∈ RealDomain(lower = 0.0, upper = 81.1, init = 8.11)
pop_Vc """
Absorption lag time (hr)
"""
∈ RealDomain(lower = 0.0, upper = 5.23, init = 0.523)
pop_tabs """
Lag time (hr)
"""
∈ RealDomain(lower = 0.0, upper = 5.0, init = 0.1)
pop_lag
# Inter-individual variability
"""
- ΩCL: Clearance
"""
∈ PDiagDomain([0.01])
pk_Ω
# Residual variability
"""
σ_prop: Proportional error
"""
∈ RealDomain(lower = 0.0, init = 0.00752)
σ_prop """
σ_add: Additive error
"""
∈ RealDomain(lower = 0.0, init = 0.0661)
σ_add end
@random begin
~ MvNormal(pk_Ω) # mean = 0, covariance = pk_Ω
pk_η end
@covariates begin
"""
FSZCL: Clearance scaling factor
"""
FSZCL"""
FSZV: Volume scaling factor
"""
FSZVend
@pre begin
= FSZCL * pop_CL
CL = FSZV * pop_Vc * exp(pk_η[1])
Vc
= pop_tabs
tabs = log(2) / tabs
Ka end
@dosecontrol begin
= (Depot = pop_lag,)
lags end
@vars begin
:= Central / Vc
cp end
@dynamics begin
' = -Ka * Depot
Depot' = Ka * Depot - (CL / Vc) * Central
Centralend
@derived begin
"""
Concentration (ng/mL)
"""
~ @. Normal(cp, sqrt((σ_prop * cp)^2 + σ_add^2))
conc 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: Matrix exponential
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.
= dataset("paganz2024/warfarin_long")
warfarin_data
# Step 2: Fix Duplicate Time Points
# -------------------------------
# Some subjects have duplicate time points for DVID = 1
# For this dataset, the triple (ID, TIME, DVID) should define
# a row uniquely, but
nrow(warfarin_data)
nrow(unique(warfarin_data, ["ID", "TIME", "DVID"]))
# We can identify the problematic rows by grouping on the index variables
@chain warfarin_data begin
@groupby :ID :TIME :DVID
@transform :tmp = length(:ID)
@rsubset :tmp > 1
end
# It is important to understand the reason for the duplicate values.
# Sometimes the duplication is caused by recording errors, sometimes
# it is a data processing error, e.g. when joining tables, or it can
# be genuine records, e.g. when samples have been analyzed in multiple
# labs. The next step depends on which of the causes are behind the
# duplications.
#
# In this case, we will assume that both values are informative and
# we will therefore just adjust the time stamp a bit for the second
# observation.
= @chain warfarin_data begin
warfarin_data_processed @groupby :ID :TIME :DVID
@transform :tmp = 1:length(:ID)
@rtransform :TIME = :tmp == 1 ? :TIME : :TIME + 1e-6
@select Not(:tmp)
end
# Transform the data in a single chain of operations
= @chain warfarin_data_processed begin
warfarin_data_wide @rsubset !contains(:ID, "#")
@rtransform begin
# Scaling factors
:FSZV = :WEIGHT / 70 # volume scaling
:FSZCL = (:WEIGHT / 70)^0.75 # clearance scaling (allometric)
# Column name for the DV
:DVNAME = "DV$(:DVID)"
# Dosing indicator columns
:CMT = ismissing(:AMOUNT) ? missing : 1
:EVID = ismissing(:AMOUNT) ? 0 : 1
end
unstack(Not([:DVID, :DVNAME, :DV]), :DVNAME, :DV)
rename!(:DV1 => :conc, :DV2 => :pca)
end
Row | ID | TIME | WEIGHT | AGE | SEX | AMOUNT | FSZV | FSZCL | CMT | EVID | DV0 | pca | conc |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
String3 | Float64 | Float64 | Int64 | Int64 | Float64? | Float64 | Float64 | Int64? | Int64 | Float64? | Float64? | Float64? | |
1 | 1 | 0.0 | 66.7 | 50 | 1 | 100.0 | 0.952857 | 0.96443 | 1 | 1 | missing | missing | missing |
2 | 1 | 0.0 | 66.7 | 50 | 1 | missing | 0.952857 | 0.96443 | missing | 0 | missing | 100.0 | missing |
3 | 1 | 24.0 | 66.7 | 50 | 1 | missing | 0.952857 | 0.96443 | missing | 0 | missing | 49.0 | 9.2 |
4 | 1 | 36.0 | 66.7 | 50 | 1 | missing | 0.952857 | 0.96443 | missing | 0 | missing | 32.0 | 8.5 |
5 | 1 | 48.0 | 66.7 | 50 | 1 | missing | 0.952857 | 0.96443 | missing | 0 | missing | 26.0 | 6.4 |
6 | 1 | 72.0 | 66.7 | 50 | 1 | missing | 0.952857 | 0.96443 | missing | 0 | missing | 22.0 | 4.8 |
7 | 1 | 96.0 | 66.7 | 50 | 1 | missing | 0.952857 | 0.96443 | missing | 0 | missing | 28.0 | 3.1 |
8 | 1 | 120.0 | 66.7 | 50 | 1 | missing | 0.952857 | 0.96443 | missing | 0 | missing | 33.0 | 2.5 |
9 | 2 | 0.0 | 66.7 | 31 | 1 | 100.0 | 0.952857 | 0.96443 | 1 | 1 | missing | missing | missing |
10 | 2 | 0.0 | 66.7 | 31 | 1 | missing | 0.952857 | 0.96443 | missing | 0 | missing | 100.0 | missing |
11 | 2 | 0.5 | 66.7 | 31 | 1 | missing | 0.952857 | 0.96443 | missing | 0 | missing | missing | 0.0 |
12 | 2 | 2.0 | 66.7 | 31 | 1 | missing | 0.952857 | 0.96443 | missing | 0 | missing | missing | 8.4 |
13 | 2 | 3.0 | 66.7 | 31 | 1 | missing | 0.952857 | 0.96443 | missing | 0 | missing | missing | 9.7 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
306 | 31 | 48.0 | 83.3 | 24 | 1 | missing | 1.19 | 1.13936 | missing | 0 | missing | 24.0 | 6.4 |
307 | 31 | 72.0 | 83.3 | 24 | 1 | missing | 1.19 | 1.13936 | missing | 0 | missing | 22.0 | 4.5 |
308 | 31 | 96.0 | 83.3 | 24 | 1 | missing | 1.19 | 1.13936 | missing | 0 | missing | 28.0 | 3.4 |
309 | 31 | 120.0 | 83.3 | 24 | 1 | missing | 1.19 | 1.13936 | missing | 0 | missing | 42.0 | 2.5 |
310 | 32 | 0.0 | 62.0 | 21 | 1 | 93.0 | 0.885714 | 0.912999 | 1 | 1 | missing | missing | missing |
311 | 32 | 0.0 | 62.0 | 21 | 1 | missing | 0.885714 | 0.912999 | missing | 0 | missing | 100.0 | missing |
312 | 32 | 24.0 | 62.0 | 21 | 1 | missing | 0.885714 | 0.912999 | missing | 0 | missing | 36.0 | 8.9 |
313 | 32 | 36.0 | 62.0 | 21 | 1 | missing | 0.885714 | 0.912999 | missing | 0 | missing | 27.0 | 7.7 |
314 | 32 | 48.0 | 62.0 | 21 | 1 | missing | 0.885714 | 0.912999 | missing | 0 | missing | 24.0 | 6.9 |
315 | 32 | 72.0 | 62.0 | 21 | 1 | missing | 0.885714 | 0.912999 | missing | 0 | missing | 23.0 | 4.4 |
316 | 32 | 96.0 | 62.0 | 21 | 1 | missing | 0.885714 | 0.912999 | missing | 0 | missing | 20.0 | 3.5 |
317 | 32 | 120.0 | 62.0 | 21 | 1 | missing | 0.885714 | 0.912999 | missing | 0 | missing | 22.0 | 2.5 |
2.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:
= read_pumas(
pop
warfarin_data_wide;= :ID,
id = :TIME,
time = :AMOUNT,
amt = :CMT,
cmt = :EVID,
evid = [:SEX, :WEIGHT, :FSZV, :FSZCL],
covariates = [:conc],
observations )
Population
Subjects: 31
Covariates: SEX, WEIGHT, 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.
2.4 Checking Model-Data Compatibility
Before performing any fit, it is recommended to verify whether the defined model can handle the provided dataset. Pumas offers functions such as loglikelihood
and findinfluential
for these checks.
2.4.1 The loglikelihood
Function
The loglikelihood
function computes the log-likelihood of the model given data and parameters. In Pumas, the signature typically looks like:
loglikelihood(model, population, params, approx)
where:
model
: The Pumas model definition (e.g.,warfarin_pk_model
).population
: A Pumas population object (e.g.,pop
).params
: A named tuple or dictionary containing parameter values.approx
: The approximation method to use. Common options includeFOCE()
,FO()
,LaplaceI()
, etc.
For example, one might write:
# A named tuple of parameter values
= (
param_vals = 0.12,
pop_CL = 7.3,
pop_Vc = 0.523,
pop_tabs = 0.5,
pop_lag = Diagonal([0.01]),
pk_Ω = 0.00752,
σ_prop = 0.0661,
σ_add
)= fit(warfarin_pk_model, pop, param_vals, FOCE()) foce_fit
[ 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 2.754619e+04 3.345035e+04
* time: 0.027840137481689453
1 5.618146e+03 1.386157e+04
* time: 1.0565710067749023
2 2.806727e+03 6.164132e+03
* time: 1.0785679817199707
3 1.814316e+03 3.717848e+03
* time: 1.0991380214691162
4 9.458168e+02 1.261428e+03
* time: 1.1184430122375488
5 6.506311e+02 6.846058e+02
* time: 1.137132167816162
6 5.067237e+02 3.407504e+02
* time: 1.155717134475708
7 4.543150e+02 1.615765e+02
* time: 1.17336106300354
8 4.390128e+02 1.001202e+02
* time: 1.1904079914093018
9 4.359539e+02 1.004875e+02
* time: 1.2070610523223877
10 4.350899e+02 9.448070e+01
* time: 1.2233130931854248
11 4.340047e+02 8.066223e+01
* time: 1.2399821281433105
12 4.322893e+02 5.164762e+01
* time: 1.256864070892334
13 4.307161e+02 2.264796e+01
* time: 1.2734739780426025
14 4.302010e+02 1.080854e+01
* time: 1.3584530353546143
15 4.301247e+02 1.195746e+01
* time: 1.3738760948181152
16 4.301086e+02 9.445190e+00
* time: 1.3892710208892822
17 4.300988e+02 7.960007e+00
* time: 1.4039571285247803
18 4.300587e+02 7.716936e+00
* time: 1.4188330173492432
19 4.300051e+02 7.117247e+00
* time: 1.4342751502990723
20 4.299453e+02 7.208415e+00
* time: 1.4500961303710938
21 4.299169e+02 7.127430e+00
* time: 1.4655671119689941
22 4.299077e+02 7.022170e+00
* time: 1.4808759689331055
23 4.298994e+02 7.386201e+00
* time: 1.495575189590454
24 4.298766e+02 8.313301e+00
* time: 1.510688066482544
25 4.298227e+02 9.481826e+00
* time: 1.5260210037231445
26 4.296924e+02 1.085355e+01
* time: 1.5415141582489014
27 4.294405e+02 1.706433e+01
* time: 1.5572900772094727
28 4.291427e+02 1.848276e+01
* time: 1.5729291439056396
29 4.289303e+02 1.058023e+01
* time: 1.5888621807098389
30 4.288371e+02 9.815220e+00
* time: 1.6050000190734863
31 4.288319e+02 9.597527e+00
* time: 1.6197099685668945
32 4.288091e+02 8.868287e+00
* time: 1.6356561183929443
33 4.287649e+02 7.800905e+00
* time: 1.6514301300048828
34 4.286492e+02 8.240185e+00
* time: 1.6670801639556885
35 4.284193e+02 1.118489e+01
* time: 1.6830780506134033
36 4.280494e+02 1.142225e+01
* time: 1.6994011402130127
37 4.277503e+02 6.478284e+00
* time: 1.7163550853729248
38 4.276843e+02 3.593549e+00
* time: 1.7330620288848877
39 4.276750e+02 2.901808e+00
* time: 1.7490391731262207
40 4.276737e+02 2.925309e+00
* time: 1.7646770477294922
41 4.276724e+02 2.942991e+00
* time: 1.7801051139831543
42 4.276678e+02 2.975008e+00
* time: 1.8380701541900635
43 4.276570e+02 3.010425e+00
* time: 1.8546221256256104
44 4.276278e+02 3.489417e+00
* time: 1.8717281818389893
45 4.275555e+02 4.423545e+00
* time: 1.8890111446380615
46 4.273850e+02 5.582714e+00
* time: 1.9069180488586426
47 4.270496e+02 6.557308e+00
* time: 1.925084114074707
48 4.266090e+02 6.537839e+00
* time: 1.943507194519043
49 4.263506e+02 3.908843e+00
* time: 1.9616141319274902
50 4.262968e+02 2.885389e+00
* time: 1.9801239967346191
51 4.262907e+02 2.842439e+00
* time: 1.9983041286468506
52 4.262900e+02 2.821199e+00
* time: 2.0167109966278076
53 4.262894e+02 2.809832e+00
* time: 2.034550189971924
54 4.262879e+02 2.794691e+00
* time: 2.053194046020508
55 4.262842e+02 2.781348e+00
* time: 2.072111129760742
56 4.262743e+02 3.009687e+00
* time: 2.0911591053009033
57 4.262477e+02 5.430536e+00
* time: 2.110504150390625
58 4.261705e+02 9.974222e+00
* time: 2.130488157272339
59 4.258983e+02 2.055347e+01
* time: 2.151236057281494
60 4.256807e+02 2.645976e+01
* time: 2.1848690509796143
61 4.254650e+02 3.195354e+01
* time: 2.2188611030578613
62 4.250777e+02 4.156365e+01
* time: 2.314378023147583
63 4.250303e+02 5.260486e+01
* time: 2.35090708732605
64 4.240447e+02 4.793859e+01
* time: 2.370526075363159
65 4.222991e+02 3.988756e+01
* time: 2.390158176422119
66 4.175763e+02 1.012857e+01
* time: 2.410703182220459
67 4.156021e+02 1.547767e+01
* time: 2.4396111965179443
68 4.143567e+02 2.010443e+01
* time: 2.4695851802825928
69 4.130607e+02 1.768446e+01
* time: 2.489320993423462
70 4.116886e+02 8.431093e+00
* time: 2.5101330280303955
71 4.112484e+02 7.131364e+00
* time: 2.5309641361236572
72 4.109556e+02 4.884649e+00
* time: 2.5518441200256348
73 4.106945e+02 2.263126e+00
* time: 2.5730910301208496
74 4.105934e+02 3.304597e+00
* time: 2.593473196029663
75 4.105660e+02 9.482860e-01
* time: 2.613939046859741
76 4.105529e+02 3.362095e-01
* time: 2.635197162628174
77 4.105426e+02 3.015047e-01
* time: 2.6567859649658203
78 4.105395e+02 1.804386e-01
* time: 2.6769001483917236
79 4.105392e+02 3.818348e-02
* time: 2.694594144821167
80 4.105391e+02 4.591111e-02
* time: 2.711292028427124
81 4.105391e+02 8.641460e-03
* time: 2.7278430461883545
82 4.105391e+02 2.258521e-03
* time: 2.743338108062744
83 4.105391e+02 2.645649e-04
* time: 2.7566750049591064
FittedPumasModel
Successful minimization: true
Likelihood approximation: FOCE
Likelihood Optimizer: BFGS
Dynamical system type: Matrix exponential
Log-likelihood value: -410.53908
Number of subjects: 31
Number of parameters: Fixed Optimized
0 7
Observation records: Active Missing
conc: 239 47
Total: 239 47
-----------------------
Estimate
-----------------------
pop_CL 0.12758
pop_Vc 8.3876
pop_tabs 0.48983
pop_lag 0.72171
pk_Ω₁,₁ 0.010524
σ_prop 0.19957
σ_add 0.55153
-----------------------
2.5 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(
::FittedPumasModel;
fpm= 0.95,
level ::Bool = false,
rethrow_error::Bool = true,
sandwich_estimator )
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)/2
and(1+level)/2
quantiles 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. 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.
An example usage:
= infer(foce_fit; level = 0.95) inference_results
[ Info: Calculating: variance-covariance matrix.
[ Info: Done.
Asymptotic inference results using sandwich estimator
Successful minimization: true
Likelihood approximation: FOCE
Likelihood Optimizer: BFGS
Dynamical system type: Matrix exponential
Log-likelihood value: -410.53908
Number of subjects: 31
Number of parameters: Fixed Optimized
0 7
Observation records: Active Missing
conc: 239 47
Total: 239 47
---------------------------------------------------------------------
Estimate SE 95.0% C.I.
---------------------------------------------------------------------
pop_CL 0.12758 0.0046779 [ 0.11842 ; 0.13675 ]
pop_Vc 8.3876 0.22464 [ 7.9474 ; 8.8279 ]
pop_tabs 0.48983 0.17922 [ 0.13856 ; 0.8411 ]
pop_lag 0.72171 0.16464 [ 0.39902 ; 1.0444 ]
pk_Ω₁,₁ 0.010524 0.0071909 [-0.0035698; 0.024618]
σ_prop 0.19957 0.044554 [ 0.11225 ; 0.28689 ]
σ_add 0.55153 0.2842 [-0.0054881; 1.1085 ]
---------------------------------------------------------------------
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)
7×7 Symmetric{Float64, Matrix{Float64}}:
2.1883e-5 0.000332493 5.45952e-6 … 3.30925e-5 0.000379372
0.000332493 0.050461 0.00233924 0.00117512 -0.00391389
5.45952e-6 0.00233924 0.032121 -0.000413843 0.00462368
0.00019032 -0.0071644 -0.0160153 -0.00259753 0.0226855
-1.83205e-6 -9.94307e-5 -0.000353466 -0.000234415 0.00119
3.30925e-5 0.00117512 -0.000413843 … 0.00198502 -0.0102392
0.000379372 -0.00391389 0.00462368 -0.0102392 0.0807681
and to get the condition number of the correlation matrix implied by vcov
use
cond(inference_results)
44.47139507390104
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)
10854.252604987605
It is also possible to calculate the correlation matrix from the vcov
output using the cov2cor
function from StatsBase
= cov2cor(Matrix(vcov(inference_results))) cor_from_cov
7×7 Matrix{Float64}:
1.0 0.31641 0.00651188 … -0.0544625 0.158779 0.285359
0.31641 1.0 0.0581035 -0.0615543 0.117415 -0.0613072
0.00651188 0.0581035 1.0 -0.274263 -0.0518272 0.0907765
0.247112 -0.193717 -0.542756 0.550551 -0.354113 0.484835
-0.0544625 -0.0615543 -0.274263 1.0 -0.731675 0.582294
0.158779 0.117415 -0.0518272 … -0.731675 1.0 -0.808653
0.285359 -0.0613072 0.0907765 0.582294 -0.808653 1.0
And we see that the cond
call above matches the condition number of the correlation matrix
cond(cor_from_cov)
44.47139507390104
2.5.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.
2.5.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(;
= Random.default_rng,
rng = 200,
samples = nothing,
stratify_by = EnsembleThreads(),
ensemblealg )
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.
= infer(foce_fit, Bootstrap(samples = 50); level = 0.95) bootstrap_results
┌ Warning: Terminated early due to NaN in gradient.
└ @ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/ZhuZN/src/multivariate/optimize/optimize.jl:98
┌ Warning: Terminated early due to NaN in gradient.
└ @ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/ZhuZN/src/multivariate/optimize/optimize.jl:98
┌ Warning: Terminated early due to NaN in gradient.
└ @ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/ZhuZN/src/multivariate/optimize/optimize.jl:98
┌ Warning: Terminated early due to NaN in gradient.
└ @ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/ZhuZN/src/multivariate/optimize/optimize.jl:98
┌ Warning: Terminated early due to NaN in gradient.
└ @ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/ZhuZN/src/multivariate/optimize/optimize.jl:98
┌ Warning: Terminated early due to NaN in gradient.
└ @ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/ZhuZN/src/multivariate/optimize/optimize.jl:98
┌ Warning: Terminated early due to NaN in gradient.
└ @ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/ZhuZN/src/multivariate/optimize/optimize.jl:98
┌ Warning: Terminated early due to NaN in gradient.
└ @ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/ZhuZN/src/multivariate/optimize/optimize.jl:98
┌ Warning: Terminated early due to NaN in gradient.
└ @ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/ZhuZN/src/multivariate/optimize/optimize.jl:98
┌ Warning: Terminated early due to NaN in gradient.
└ @ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/ZhuZN/src/multivariate/optimize/optimize.jl:98
┌ Warning: Terminated early due to NaN in gradient.
└ @ Optim ~/run/_work/PumasTutorials.jl/PumasTutorials.jl/custom_julia_depot/packages/Optim/ZhuZN/src/multivariate/optimize/optimize.jl:98
┌ Info: Bootstrap inference finished.
│ Total resampled fits = 50
│ Success rate = 1.0
└ Unique resampled populations = 50
Bootstrap inference results
Successful minimization: true
Likelihood approximation: FOCE
Likelihood Optimizer: BFGS
Dynamical system type: Matrix exponential
Log-likelihood value: -410.53908
Number of subjects: 31
Number of parameters: Fixed Optimized
0 7
Observation records: Active Missing
conc: 239 47
Total: 239 47
--------------------------------------------------------------------
Estimate SE 95.0% C.I.
--------------------------------------------------------------------
pop_CL 0.12758 0.049707 [0.10943 ; 0.14334 ]
pop_Vc 8.3876 1.571 [6.9928 ; 10.244 ]
pop_tabs 0.48983 0.27768 [5.3359e-7 ; 0.86574 ]
pop_lag 0.72171 0.1839 [0.4968 ; 1.0 ]
pk_Ω₁,₁ 0.010524 0.011808 [5.215e-79 ; 0.036166]
σ_prop 0.19957 1.769 [0.075233 ; 0.28436 ]
σ_add 0.55153 0.30582 [2.9579e-116; 0.91403 ]
--------------------------------------------------------------------
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)
7×7 Matrix{Float64}:
0.00247081 0.0744889 0.00713803 … 0.0869423 -0.00302846
0.0744889 2.46798 0.204042 2.57599 -0.0730363
0.00713803 0.204042 0.0771041 0.234532 0.0348872
-0.00109644 -0.0269498 -0.0336948 -0.0258234 -0.0212816
-7.65084e-5 -0.00110434 0.000187027 -0.0031925 0.00230502
0.0869423 2.57599 0.234532 … 3.12943 -0.143379
-0.00302846 -0.0730363 0.0348872 -0.143379 0.0935244
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_Ω₁,₁ | σ_prop | σ_add |
---|---|---|---|---|---|---|---|
Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | |
1 | 0.135175 | 8.39811 | 0.564577 | 0.6634 | 0.00582785 | 0.225835 | 0.37769 |
2 | 0.133637 | 8.57208 | 0.549971 | 0.951618 | 0.0254604 | 0.165505 | 0.687631 |
3 | 0.124322 | 8.4453 | 0.455475 | 0.620348 | 0.0137868 | 0.167735 | 0.583125 |
4 | 0.135073 | 8.43253 | 0.503238 | 0.690185 | 0.0158018 | 0.147854 | 0.821073 |
5 | 0.124444 | 8.17174 | 0.805164 | 0.488214 | 2.23345e-10 | 0.179057 | 0.682639 |
6 | 0.133152 | 8.22381 | 0.390986 | 0.849976 | 0.0109805 | 0.185165 | 0.75736 |
7 | 0.132996 | 8.31309 | 0.622861 | 0.75064 | 0.00689214 | 0.153262 | 0.837761 |
8 | 0.144336 | 10.6512 | 0.121373 | 0.916338 | 1.28494e-106 | 0.173561 | 2.7241e-129 |
9 | 0.124389 | 8.25699 | 0.661485 | 0.691952 | 7.4399e-10 | 0.164051 | 0.756688 |
10 | 0.114851 | 8.26133 | 0.0943829 | 1.4947 | 0.0284981 | 0.0742399 | 0.642479 |
11 | 0.133158 | 8.19032 | 0.584724 | 0.881794 | 0.0103163 | 0.182199 | 0.654715 |
12 | 0.133837 | 8.50587 | 0.465672 | 0.676962 | 0.00638902 | 0.182697 | 0.535889 |
13 | 0.127928 | 8.2168 | 0.656253 | 0.800147 | 0.0275024 | 0.134609 | 0.79723 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
39 | 0.128647 | 8.21614 | 0.470363 | 0.684001 | 0.0155254 | 0.163668 | 0.788704 |
40 | 0.134979 | 8.58739 | 0.364157 | 0.881497 | 0.0369369 | 0.124792 | 0.935501 |
41 | 0.130251 | 8.13023 | 0.46527 | 0.717524 | 0.0157143 | 0.162017 | 0.610404 |
42 | 0.124927 | 8.7531 | 0.525946 | 0.561603 | 0.0164374 | 0.158832 | 0.705722 |
43 | 0.122152 | 8.49019 | 4.15059e-9 | 0.999996 | 6.77691e-135 | 0.241843 | 3.07323e-83 |
44 | 0.12862 | 8.50102 | 0.569731 | 0.715056 | 6.66497e-10 | 0.188884 | 0.58755 |
45 | 0.132123 | 8.19804 | 0.391365 | 0.777406 | 0.013773 | 0.182285 | 0.763217 |
46 | 0.125933 | 8.22744 | 0.432347 | 0.586395 | 0.0292002 | 0.120257 | 0.834049 |
47 | 0.131828 | 8.42803 | 0.488984 | 0.720136 | 0.00754987 | 0.215658 | 0.687501 |
48 | 0.12945 | 8.0876 | 0.237585 | 0.760896 | 0.00726597 | 0.186104 | 0.628473 |
49 | 0.129102 | 8.61346 | 0.572642 | 0.690742 | 0.00539275 | 0.17699 | 0.471174 |
50 | 0.126611 | 8.39068 | 0.490131 | 0.663827 | 0.0100943 | 0.115705 | 0.652958 |
This is very useful for histogram plotting of parameter distributions.
2.5.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.
= infer(foce_fit, SIR(samples = 1000, resamples = 200); level = 0.95) sir_results
[ Info: Calculating: variance-covariance matrix.
[ Info: Done.
[ Info: Running SIR.
[ Info: Resampling.
Simulated inference results
Successful minimization: true
Likelihood approximation: FOCE
Likelihood Optimizer: BFGS
Dynamical system type: Matrix exponential
Log-likelihood value: -410.53908
Number of subjects: 31
Number of parameters: Fixed Optimized
0 7
Observation records: Active Missing
conc: 239 47
Total: 239 47
-------------------------------------------------------------------
Estimate SE 95.0% C.I.
-------------------------------------------------------------------
pop_CL 0.12758 0.0028963 [0.12197 ; 0.1333 ]
pop_Vc 8.3876 0.24066 [7.9332 ; 8.8405 ]
pop_tabs 0.48983 0.13264 [0.24441 ; 0.74641]
pop_lag 0.72171 0.091714 [0.50355 ; 0.84321]
pk_Ω₁,₁ 0.010524 0.0053638 [0.0012933; 0.02113]
σ_prop 0.19957 0.028962 [0.16231 ; 0.27523]
σ_add 0.55153 0.20917 [0.039908 ; 0.86033]
-------------------------------------------------------------------
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)
7×7 Matrix{Float64}:
8.38868e-6 0.000132666 8.81323e-8 … 5.7814e-7 0.000100732
0.000132666 0.0579191 -0.00505429 -0.000203157 -0.00385386
8.81323e-8 -0.00505429 0.0175931 -0.00107094 0.00727587
3.68159e-5 0.000465673 -0.0080209 -0.000299421 0.00310756
2.09475e-6 0.000235207 2.033e-5 -8.62468e-5 0.000535516
5.7814e-7 -0.000203157 -0.00107094 … 0.000838786 -0.00521685
0.000100732 -0.00385386 0.00727587 -0.00521685 0.0437531
and
DataFrame(sir_results.vcov)
Row | pop_CL | pop_Vc | pop_tabs | pop_lag | pk_Ω₁,₁ | σ_prop | σ_add |
---|---|---|---|---|---|---|---|
Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | |
1 | 0.127571 | 8.65869 | 0.19149 | 0.808368 | 0.00674417 | 0.260674 | 0.0424837 |
2 | 0.126884 | 8.31607 | 0.532419 | 0.619936 | 0.00300365 | 0.255596 | 0.1466 |
3 | 0.12869 | 8.32958 | 0.638063 | 0.588227 | 0.0035923 | 0.260376 | 0.0255513 |
4 | 0.128182 | 7.91908 | 0.569104 | 0.761485 | 0.016481 | 0.1901 | 0.684522 |
5 | 0.128051 | 8.61148 | 0.290952 | 0.870127 | 0.0200761 | 0.181501 | 0.61098 |
6 | 0.128252 | 8.45581 | 0.319999 | 0.763224 | 0.0021016 | 0.263796 | 0.0400501 |
7 | 0.125095 | 8.51985 | 0.478599 | 0.657732 | 0.0115755 | 0.181019 | 0.557215 |
8 | 0.126083 | 8.31458 | 0.509982 | 0.576583 | 0.00873988 | 0.223252 | 0.302577 |
9 | 0.126005 | 8.5038 | 0.456142 | 0.703952 | 0.00602465 | 0.24008 | 0.252261 |
10 | 0.131828 | 8.49869 | 0.445267 | 0.680906 | 0.00753632 | 0.208978 | 0.627915 |
11 | 0.126417 | 8.66168 | 0.673703 | 0.541912 | 0.0112181 | 0.191789 | 0.570906 |
12 | 0.12767 | 8.86345 | 0.451972 | 0.494212 | 0.00532104 | 0.227752 | 0.399223 |
13 | 0.126427 | 8.56067 | 0.47573 | 0.522138 | 0.0190835 | 0.201154 | 0.507841 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
189 | 0.128254 | 8.38625 | 0.503374 | 0.674967 | 0.00822299 | 0.210554 | 0.332138 |
190 | 0.128723 | 8.46931 | 0.746122 | 0.491684 | 0.00894756 | 0.203937 | 0.393635 |
191 | 0.129779 | 8.34794 | 0.504504 | 0.839448 | 0.0220383 | 0.160892 | 0.760316 |
192 | 0.131045 | 8.55697 | 0.546378 | 0.707979 | 0.0132994 | 0.152927 | 0.822215 |
193 | 0.122134 | 8.23774 | 0.489023 | 0.529574 | 0.00290233 | 0.236042 | 0.0681814 |
194 | 0.124065 | 8.23777 | 0.757667 | 0.494929 | 0.0106029 | 0.189046 | 0.614687 |
195 | 0.126841 | 8.26098 | 0.694462 | 0.703826 | 0.015406 | 0.165526 | 0.793722 |
196 | 0.124526 | 7.89228 | 0.506947 | 0.717785 | 0.00558366 | 0.268672 | 0.332431 |
197 | 0.124948 | 8.28127 | 0.379557 | 0.777262 | 0.00774241 | 0.186265 | 0.597754 |
198 | 0.129879 | 8.05742 | 0.616092 | 0.775751 | 0.0142323 | 0.166636 | 0.906107 |
199 | 0.128529 | 8.30409 | 0.62777 | 0.612318 | 0.00453657 | 0.247357 | 0.331663 |
200 | 0.129403 | 8.20053 | 0.740329 | 0.608549 | 0.00480835 | 0.207861 | 0.684651 |
2.5.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.
= infer(foce_fit, MarginalMCMC(); level = 0.95) inference_results
As sampling based inference can be computationally intensive we exclude the actual invocation of this method from this tutorial.
3 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.