|
| 1 | +# Script: AN01-pk_model_uncertainty.jl |
| 2 | +# Purpose: Provide uncertainty estimates for the points estimates of the model |
| 3 | +# ============================================================== |
| 4 | + |
| 5 | +using Pumas, CairoMakie, DataFrames, DataFramesMeta, CategoricalArrays, Logging |
| 6 | +include(joinpath("..", "..", "Day1", "01-TeachingMaterial", "MW03-pk_model_fitting.jl")) # This gives us the fitted model 'fpm' |
| 7 | + |
| 8 | +# Introduction to Parameter Uncertainty |
| 9 | +# -------------------------------- |
| 10 | +# Understanding uncertainty in parameter estimates is crucial because: |
| 11 | +# 1. Some parameters might be poorly determined in the model for the data available |
| 12 | +# 2. Parameter uncertainty affects predictions and simulations |
| 13 | +# 3. Some parameters may be more precisely estimated than others |
| 14 | +# 4. Uncertainty helps inform experimental design and data collection |
| 15 | +# |
| 16 | +# We'll explore a variety of approaches: |
| 17 | +# 1. Asymptotic confidence intervals |
| 18 | +# a Sandwich estimator (default) |
| 19 | +# b Inverse Hessian (the classical maximum likelihood estimator) |
| 20 | +# 2. Bootstrap Method (non-parametric resampling) |
| 21 | +# 3. Sampling importance resampling (SIR) |
| 22 | + |
| 23 | +@info "Asymptotic confidence intervals" |
| 24 | +@info "Sandwich estimator (default)" |
| 25 | +@info "====================================" |
| 26 | +asymp_inf_a = infer(fpm) |
| 27 | + |
| 28 | +@info "Sandwich estimator (default)" |
| 29 | +asymp_inf_b = infer(fpm; sandwich_estimator = false) |
| 30 | + |
| 31 | +# Step 2: Bootstrap Analysis |
| 32 | +# ---------------------- |
| 33 | +# Bootstrap analysis: |
| 34 | +# - Resamples the data with replacement |
| 35 | +# - Refits the model to each sample |
| 36 | +# - Is more robust but computationally intensive |
| 37 | + |
| 38 | +@info "Performing Bootstrap Analysis..." |
| 39 | +@info "This will take some time as we need to refit the model multiple times" |
| 40 | + |
| 41 | +# Perform bootstrap with progress updates |
| 42 | +bts_inf = infer(fpm, Bootstrap(samples = 100)) |
| 43 | + |
| 44 | +@info "Bootstrap Analysis Complete!" |
| 45 | +@info "Parameter Standard Errors from Bootstrap:" |
| 46 | +coeftable(bts_inf) |
| 47 | + |
| 48 | +# Step 3: Sampling importance resampling |
| 49 | +sir_inf = infer(fpm, SIR(samples = 1000, resamples = 200)) |
| 50 | + |
| 51 | +# Educational Note: |
| 52 | +# --------------- |
| 53 | +@info "Key Takeaways from Uncertainty Analysis:" |
| 54 | +@info "1. Parameter uncertainty can be estimated through multiple methods" |
| 55 | +@info "2. Bootstrap is more robust but computationally intensive" |
| 56 | +@info "3. Some parameters may be estimated more precisely than others" |
| 57 | +@info "4. Fixing poorly identified parameters can improve estimation" |
| 58 | +@info "5. Understanding uncertainty is crucial for model-based decision making" |
| 59 | + |
| 60 | +# Next Steps: |
| 61 | +# ---------- |
| 62 | +@info "Next Steps:" |
| 63 | +@info "1. Use uncertainty estimates in simulations (simulation.jl)" |
| 64 | +@info "2. Consider ways to reduce uncertainty:" |
| 65 | +@info " - Collect more data" |
| 66 | +@info " - Modify sampling times" |
| 67 | +@info " - Simplify the model if appropriate" |
0 commit comments