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.020364999771118164
1 5.618146e+03 1.386157e+04
* time: 0.8553059101104736
2 2.806727e+03 6.164132e+03
* time: 0.8744220733642578
3 1.814316e+03 3.717848e+03
* time: 0.8921399116516113
4 9.458168e+02 1.261428e+03
* time: 0.9085209369659424
5 6.506311e+02 6.846058e+02
* time: 0.9243631362915039
6 5.067237e+02 3.407504e+02
* time: 0.9403789043426514
7 4.543150e+02 1.615765e+02
* time: 0.9556479454040527
8 4.390128e+02 1.001202e+02
* time: 0.9704489707946777
9 4.359539e+02 1.004875e+02
* time: 0.9851081371307373
10 4.350899e+02 9.448070e+01
* time: 0.9995179176330566
11 4.340047e+02 8.066223e+01
* time: 1.0142390727996826
12 4.322893e+02 5.164762e+01
* time: 1.029102087020874
13 4.307161e+02 2.264796e+01
* time: 1.0441710948944092
14 4.302010e+02 1.080854e+01
* time: 1.0590729713439941
15 4.301247e+02 1.195746e+01
* time: 1.0730419158935547
16 4.301086e+02 9.445190e+00
* time: 1.1496939659118652
17 4.300988e+02 7.960007e+00
* time: 1.1628549098968506
18 4.300587e+02 7.716936e+00
* time: 1.1755690574645996
19 4.300051e+02 7.117247e+00
* time: 1.1889350414276123
20 4.299453e+02 7.208415e+00
* time: 1.2026970386505127
21 4.299169e+02 7.127430e+00
* time: 1.21760892868042
22 4.299077e+02 7.022170e+00
* time: 1.2308881282806396
23 4.298994e+02 7.386201e+00
* time: 1.2436659336090088
24 4.298766e+02 8.313301e+00
* time: 1.2566070556640625
25 4.298227e+02 9.481826e+00
* time: 1.2695589065551758
26 4.296924e+02 1.085355e+01
* time: 1.282365083694458
27 4.294405e+02 1.706433e+01
* time: 1.2953341007232666
28 4.291427e+02 1.848276e+01
* time: 1.308393955230713
29 4.289303e+02 1.058023e+01
* time: 1.3218140602111816
30 4.288371e+02 9.815220e+00
* time: 1.3362069129943848
31 4.288319e+02 9.597527e+00
* time: 1.3486709594726562
32 4.288091e+02 8.868287e+00
* time: 1.3618109226226807
33 4.287649e+02 7.800905e+00
* time: 1.374871015548706
34 4.286492e+02 8.240185e+00
* time: 1.3880410194396973
35 4.284193e+02 1.118489e+01
* time: 1.4018571376800537
36 4.280494e+02 1.142225e+01
* time: 1.4159610271453857
37 4.277503e+02 6.478284e+00
* time: 1.4305779933929443
38 4.276843e+02 3.593549e+00
* time: 1.4450559616088867
39 4.276750e+02 2.901808e+00
* time: 1.458630084991455
40 4.276737e+02 2.925309e+00
* time: 1.47153902053833
41 4.276724e+02 2.942991e+00
* time: 1.4836390018463135
42 4.276678e+02 2.975008e+00
* time: 1.49574613571167
43 4.276570e+02 3.010425e+00
* time: 1.5082600116729736
44 4.276278e+02 3.489417e+00
* time: 1.5461750030517578
45 4.275555e+02 4.423545e+00
* time: 1.5590081214904785
46 4.273850e+02 5.582714e+00
* time: 1.5723850727081299
47 4.270496e+02 6.557308e+00
* time: 1.586940050125122
48 4.266090e+02 6.537839e+00
* time: 1.6026251316070557
49 4.263506e+02 3.908843e+00
* time: 1.6182079315185547
50 4.262968e+02 2.885389e+00
* time: 1.6337029933929443
51 4.262907e+02 2.842439e+00
* time: 1.6486611366271973
52 4.262900e+02 2.821199e+00
* time: 1.6625409126281738
53 4.262894e+02 2.809832e+00
* time: 1.6761510372161865
54 4.262879e+02 2.794691e+00
* time: 1.689445972442627
55 4.262842e+02 2.781348e+00
* time: 1.7028419971466064
56 4.262743e+02 3.009687e+00
* time: 1.7176830768585205
57 4.262477e+02 5.430536e+00
* time: 1.731281042098999
58 4.261705e+02 9.974222e+00
* time: 1.7455179691314697
59 4.258983e+02 2.055347e+01
* time: 1.7596700191497803
60 4.256807e+02 2.645976e+01
* time: 1.7828550338745117
61 4.254650e+02 3.195354e+01
* time: 1.8066129684448242
62 4.250777e+02 4.156365e+01
* time: 1.8762130737304688
63 4.250303e+02 5.260486e+01
* time: 1.8913319110870361
64 4.240447e+02 4.793859e+01
* time: 1.906182050704956
65 4.222991e+02 3.988756e+01
* time: 1.9337620735168457
66 4.175763e+02 1.012857e+01
* time: 1.9492900371551514
67 4.156021e+02 1.547767e+01
* time: 1.9704880714416504
68 4.143567e+02 2.010443e+01
* time: 1.9929699897766113
69 4.130607e+02 1.768446e+01
* time: 2.007513999938965
70 4.116886e+02 8.431093e+00
* time: 2.0231199264526367
71 4.112484e+02 7.131364e+00
* time: 2.0391390323638916
72 4.109556e+02 4.884649e+00
* time: 2.0547120571136475
73 4.106945e+02 2.263126e+00
* time: 2.0703370571136475
74 4.105934e+02 3.304597e+00
* time: 2.085374116897583
75 4.105660e+02 9.482860e-01
* time: 2.0997331142425537
76 4.105529e+02 3.362095e-01
* time: 2.113793134689331
77 4.105426e+02 3.015047e-01
* time: 2.128148078918457
78 4.105395e+02 1.804386e-01
* time: 2.142216920852661
79 4.105392e+02 3.818348e-02
* time: 2.155277967453003
80 4.105391e+02 4.591111e-02
* time: 2.168184995651245
81 4.105391e+02 8.641460e-03
* time: 2.1803550720214844
82 4.105391e+02 2.258521e-03
* time: 2.1917920112609863
83 4.105391e+02 2.645649e-04
* time: 2.201529026031494
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
┌ 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.005675 [0.11393 ; 0.13508 ]
pop_Vc 8.3876 0.59839 [7.9163 ; 9.7795 ]
pop_tabs 0.48983 0.27551 [2.0795e-10 ; 0.83922 ]
pop_lag 0.72171 0.21655 [0.49325 ; 1.2657 ]
pk_Ω₁,₁ 0.010524 0.011872 [7.0866e-152; 0.039105]
σ_prop 0.19957 0.17055 [0.093353 ; 0.34803 ]
σ_add 0.55153 0.29522 [5.5433e-113; 0.89966 ]
--------------------------------------------------------------------
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}:
3.22057e-5 0.000649514 0.000552396 … -0.000268754 0.000627688
0.000649514 0.358065 -0.0344415 0.0408305 -0.0483209
0.000552396 -0.0344415 0.0759046 -0.0138158 0.0633069
-0.000158311 -0.0269274 -0.0330874 0.00185827 -0.0147262
-1.38084e-5 -0.000356071 0.000194971 -0.000608553 0.00122161
-0.000268754 0.0408305 -0.0138158 … 0.0290874 -0.0218618
0.000627688 -0.0483209 0.0633069 -0.0218618 0.0871531
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.125578 | 8.98281 | 4.36765e-8 | 0.996993 | 0.0271002 | 0.223077 | 1.33317e-84 |
2 | 0.1244 | 8.42621 | 0.365245 | 0.659548 | 0.0136263 | 0.168822 | 0.525784 |
3 | 0.12375 | 8.18275 | 0.268741 | 0.898931 | 0.0213819 | 0.10219 | 0.592446 |
4 | 0.134236 | 11.3906 | 1.07654e-11 | 0.5075 | 1.91339e-29 | 0.234482 | 8.0869e-44 |
5 | 0.12591 | 8.24966 | 0.553074 | 0.750858 | 0.0121096 | 0.204993 | 0.575154 |
6 | 0.124153 | 8.98397 | 0.761204 | 0.581756 | 0.0391196 | 0.140152 | 0.740344 |
7 | 0.12371 | 8.15755 | 0.127495 | 1.34285 | 0.0154643 | 0.189903 | 0.483453 |
8 | 0.138751 | 8.3823 | 0.424005 | 0.888849 | 0.0101547 | 0.199156 | 0.579327 |
9 | 0.120933 | 8.2059 | 0.666814 | 0.737421 | 0.0326047 | 0.132204 | 0.832603 |
10 | 0.128936 | 8.07772 | 0.401559 | 0.880286 | 0.0166646 | 0.170339 | 0.548603 |
11 | 0.127492 | 8.4297 | 0.610871 | 0.520427 | 0.0095821 | 0.165066 | 0.67469 |
12 | 0.131641 | 8.38429 | 0.514789 | 0.752799 | 0.00819364 | 0.153051 | 0.788881 |
13 | 0.131052 | 8.54199 | 0.410945 | 0.707324 | 0.0152979 | 0.17767 | 0.512981 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
39 | 0.115264 | 9.62071 | 2.5426e-10 | 1.0 | 1.83777e-30 | 0.36214 | 2.99261e-73 |
40 | 0.130123 | 8.52793 | 0.648366 | 0.493677 | 0.00768785 | 0.17469 | 0.565061 |
41 | 0.12537 | 8.15756 | 0.804215 | 0.867829 | 1.88788e-8 | 0.146955 | 0.707962 |
42 | 0.122808 | 8.39852 | 0.5093 | 0.947771 | 0.0262906 | 0.164827 | 0.649535 |
43 | 0.134554 | 8.98091 | 0.59207 | 0.578642 | 0.0127102 | 0.177463 | 0.619504 |
44 | 0.131887 | 7.99153 | 0.851547 | 0.717072 | 0.00433128 | 0.179371 | 0.836413 |
45 | 0.122622 | 8.3403 | 0.472115 | 0.891206 | 0.0207328 | 0.149524 | 0.712664 |
46 | 0.124803 | 8.96368 | 0.167712 | 0.980106 | 0.0324106 | 0.148722 | 0.680361 |
47 | 0.126852 | 8.33738 | 0.54271 | 0.499876 | 0.0102378 | 0.197589 | 0.332224 |
48 | 0.125105 | 8.38061 | 0.717838 | 0.499632 | 0.0106155 | 0.201235 | 0.483197 |
49 | 0.110306 | 7.17442 | 1.9829e-5 | 0.999537 | 2.55877e-120 | 0.190939 | 3.84218e-140 |
50 | 0.117934 | 8.24291 | 0.629971 | 0.375121 | 0.0327102 | 0.0907874 | 0.756349 |
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.002948 [0.12252 ; 0.13232 ]
pop_Vc 8.3876 0.21782 [7.9931 ; 8.8372 ]
pop_tabs 0.48983 0.12362 [0.28363 ; 0.70865 ]
pop_lag 0.72171 0.091822 [0.51142 ; 0.84436 ]
pk_Ω₁,₁ 0.010524 0.005353 [0.0018699; 0.021192]
σ_prop 0.19957 0.029992 [0.15356 ; 0.27004 ]
σ_add 0.55153 0.20795 [0.059357 ; 0.82548 ]
-------------------------------------------------------------------
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.69072e-6 2.94411e-5 -1.36802e-5 … -9.77026e-7 0.000127756
2.94411e-5 0.047445 -0.000961304 0.00126493 -0.00782305
-1.36802e-5 -0.000961304 0.0152813 -0.000939813 0.00558809
5.74152e-5 -0.00298276 -0.0070377 -0.00028148 0.00420728
-2.48176e-7 3.77397e-5 1.0809e-6 -8.95634e-5 0.000517726
-9.77026e-7 0.00126493 -0.000939813 … 0.000899491 -0.0053148
0.000127756 -0.00782305 0.00558809 -0.0053148 0.0432435
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.127053 | 8.23451 | 0.428541 | 0.79629 | 0.0085515 | 0.208217 | 0.530797 |
2 | 0.12868 | 8.35468 | 0.326659 | 0.814659 | 0.00624035 | 0.289943 | 0.00292491 |
3 | 0.125361 | 7.9942 | 0.685668 | 0.66943 | 0.0102639 | 0.190855 | 0.572212 |
4 | 0.129661 | 8.75583 | 0.517447 | 0.68051 | 0.00754638 | 0.224275 | 0.288457 |
5 | 0.128429 | 8.39968 | 0.635695 | 0.607892 | 0.0199607 | 0.164134 | 0.66613 |
6 | 0.130862 | 8.31146 | 0.536503 | 0.603384 | 0.0185382 | 0.203149 | 0.55273 |
7 | 0.127163 | 8.50798 | 0.542894 | 0.565813 | 0.0117744 | 0.237692 | 0.104904 |
8 | 0.125423 | 8.65404 | 0.463781 | 0.713478 | 0.00425713 | 0.225015 | 0.47756 |
9 | 0.128353 | 8.7165 | 0.671547 | 0.497497 | 0.00493433 | 0.252517 | 0.138841 |
10 | 0.127047 | 8.37698 | 0.379458 | 0.721797 | 0.00769715 | 0.240557 | 0.0999945 |
11 | 0.128401 | 8.84186 | 0.609676 | 0.55972 | 0.0204749 | 0.173766 | 0.62084 |
12 | 0.129343 | 8.46809 | 0.52162 | 0.654657 | 0.00714006 | 0.220811 | 0.387542 |
13 | 0.1267 | 8.46219 | 0.345106 | 0.755135 | 0.0211245 | 0.194449 | 0.709214 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
189 | 0.129305 | 8.54362 | 0.686061 | 0.691248 | 0.00428072 | 0.186281 | 0.690024 |
190 | 0.124509 | 8.25767 | 0.581084 | 0.689727 | 0.0111921 | 0.17537 | 0.574822 |
191 | 0.132203 | 8.52513 | 0.67254 | 0.581522 | 0.0133289 | 0.162034 | 0.760769 |
192 | 0.123076 | 8.06721 | 0.486907 | 0.61508 | 0.00854871 | 0.227575 | 0.147408 |
193 | 0.128 | 8.539 | 0.543212 | 0.728575 | 0.0127043 | 0.195906 | 0.535774 |
194 | 0.127789 | 8.33705 | 0.570266 | 0.538872 | 0.00314258 | 0.260402 | 0.275802 |
195 | 0.122535 | 8.62508 | 0.562946 | 0.604098 | 0.0141064 | 0.202186 | 0.434273 |
196 | 0.126286 | 8.5461 | 0.422051 | 0.776902 | 0.0112255 | 0.186561 | 0.58058 |
197 | 0.123498 | 8.25939 | 0.366876 | 0.580682 | 0.00359432 | 0.199324 | 0.586833 |
198 | 0.128156 | 8.33574 | 0.298433 | 0.764659 | 0.0147789 | 0.215834 | 0.413738 |
199 | 0.124623 | 8.30535 | 0.290827 | 0.80815 | 0.0105447 | 0.231281 | 0.30538 |
200 | 0.126894 | 8.48889 | 0.315074 | 0.859328 | 0.00576064 | 0.239355 | 0.252896 |
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.