Estimating Parameters using SAEM
PumasEMModel
s can optionally be fitted using SAEM
. Here is an example PumasEMModel
definition:
using Pumas
using PumasUtilities
using Random
covariate_saem_cov = @emmodel begin
@random begin
CL ~ 1 + logwt | LogNormal
v ~ 1 | LogNormal
end
@covariance 2
@covariates wt
@pre begin
Vc = wt * v
end
@dynamics Central1
@post begin
cp = Central / Vc
end
@error begin
dv ~ ProportionalNormal(cp)
end
end
PumasEMModel
Parameters with random effects:
CL ~ (1, :logwt) | LogNormal
v ~ (1,) | LogNormal
Covariates: wt
Pre-dynamical variables: Vc
Dynamical system variables: Central
Post-dynamical variables: cp
See the documentation on the @emmodel
macro interface for an explanation of the syntax. To fit this model, we'll simulate data using simobs
.
sim_params_covariate_cov = (;
CL = (4.0, 0.75),
v = 70.0,
Ω = (Pumas.@SMatrix([0.1 0.05; 0.05 0.1]),),
σ = ((0.2,),),
)
obstimes = 0.0:100.0
dose = DosageRegimen(1_000; addl = 2, ii = 24)
function choose_covariates()
wt = (55 + 25rand()) / 70
return (; wt, logwt = log(wt))
end
pop = [Subject(; id = i, events = dose, covariates = choose_covariates()) for i = 1:72]
sims = simobs(
covariate_saem_cov,
pop,
sim_params_covariate_cov;
obstimes,
ensemblealg = EnsembleSerial(),
)
reread_df = DataFrame(sims);
pop_covariate = read_pumas(reread_df; observations = [:dv], covariates = [:logwt, :wt])
Population
Subjects: 72
Covariates: logwt, wt
Observations: dv
When specifying init
s, it is not necessary to specify any variance parameters for the random effects or error models.
init_covariate = (; CL = (2.0, 2 / 3), v = 50.0)
(CL = (2.0, 0.6666666666666666),
v = 50.0,)
If unspecified, they will be initialized to 1.0
or the identity matrix for covariances. SAEM
's stochastic exploration phase is most effective when these variance parameters are much larger than the true variances. Thus, if the true variances are believed to be around 1.0
or larger, it is recommended to specify larger initial values manually.
It is possible to pass a vector of random number generates as an argument to fit
. If so, the fit will use one thread per RNG
. By specifying the seeds of each RNG
, SAEM()
can be fully reproducible:
rngv = [MersenneTwister(1941964947i + 1) for i ∈ 1:Threads.nthreads()];
fit_covariate_cov1 = fit(
covariate_saem_cov,
pop_covariate,
init_covariate,
SAEM();
ensemblealg = EnsembleThreads(),
rng = rngv,
)
rngv = [MersenneTwister(1941964947i + 1) for i ∈ 1:Threads.nthreads()];
fit_covariate_cov2 = fit(
covariate_saem_cov,
pop_covariate,
init_covariate,
SAEM();
ensemblealg = EnsembleThreads(),
rng = rngv,
)
coef(fit_covariate_cov1) == coef(fit_covariate_cov2) # true
true
One can also specify the number of iterations for each of the three phases (rapid exploration, convergence, smoothing):
fit_covariate_cov =
fit(covariate_saem_cov, pop_covariate, init_covariate, SAEM(; iters = (1000, 500, 500)))
FittedPumasEMModel
Likelihood approximation: SAEM
Dynamical system type: Closed form
Log-likelihood value: -11361.466
Number of subjects: 72
Number of parameters: Fixed Optimized
0 7
Observation records: Active Missing
dv: 7272 0
Total: 7272 0
-------------------
Estimate
-------------------
CL₁ 4.1739
CL₂ 0.60848
v 75.738
Ω₁,₁ 0.096134
Ω₂,₁ 0.048567
Ω₂,₂ 0.10111
σ 0.20099
-------------------
The results of a fit can be analyzed normally:
infer_cov = infer(fit_covariate_cov)
Asymptotic inference results using sandwich estimator
Likelihood approximation: SAEM
Dynamical system type: Closed form
Log-likelihood value: -11361.466
Number of subjects: 72
Number of parameters: Fixed Optimized
0 7
Observation records: Active Missing
dv: 7272 0
Total: 7272 0
-------------------------------------------------------------------
Estimate SE 95.0% C.I.
-------------------------------------------------------------------
CL_base 4.1739 0.16037 [ 3.8596 ; 4.4882 ]
CL_logwt 0.60848 0.32048 [-0.019645; 1.2366 ]
v_base 75.738 2.8495 [70.154 ; 81.323 ]
ω_1₁,₁ 0.31006 0.023827 [ 0.26336 ; 0.35675]
ω_1₂,₁ 0.15664 0.029303 [ 0.099209; 0.21407]
ω_1₂,₂ 0.27673 0.020365 [ 0.23681 ; 0.31664]
σ_0 0.20099 0.0015352 [ 0.19798 ; 0.204 ]
-------------------------------------------------------------------
coeftable(infer_cov)
Row | parameter | estimate | se | relative_se | ci_lower | ci_upper |
---|---|---|---|---|---|---|
String | Float64 | Float64 | Float64 | Float64 | Float64 | |
1 | CL_base | 4.17393 | 0.160369 | 0.0384215 | 3.85962 | 4.48825 |
2 | CL_logwt | 0.608478 | 0.320477 | 0.526686 | -0.0196455 | 1.2366 |
3 | v_base | 75.7385 | 2.84945 | 0.0376222 | 70.1536 | 81.3233 |
4 | ω_1₁,₁ | 0.310055 | 0.0238267 | 0.0768465 | 0.263356 | 0.356755 |
5 | ω_1₂,₁ | 0.156641 | 0.0293026 | 0.187069 | 0.0992086 | 0.214073 |
6 | ω_1₂,₂ | 0.276728 | 0.0203652 | 0.0735929 | 0.236813 | 0.316643 |
7 | σ_0 | 0.200993 | 0.00153518 | 0.00763797 | 0.197984 | 0.204002 |
inspect_cov = inspect(fit_covariate_cov)
[ Info: Calculating predictions.
[ Info: Calculating weighted residuals.
[ Info: Calculating empirical bayes.
[ Info: Evaluating individual parameters.
[ Info: Evaluating dose control parameters.
[ Info: Done.