Add residual-based posterior predictive diagnostics for individual-level fit evaluation#180
Draft
Add residual-based posterior predictive diagnostics for individual-level fit evaluation#180
Conversation
Co-authored-by: Kwan-Jenny <68584166+Kwan-Jenny@users.noreply.github.com>
Co-authored-by: Kwan-Jenny <68584166+Kwan-Jenny@users.noreply.github.com>
Co-authored-by: Kwan-Jenny <68584166+Kwan-Jenny@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add residual-based posterior predictive diagnostics for antibody trajectories
Add residual-based posterior predictive diagnostics for individual-level fit evaluation
Jan 22, 2026
fix lint issue in this file
Collaborator
|
The @copilot add the new functions to _pkgdown.yml |
Co-authored-by: Kwan-Jenny <68584166+Kwan-Jenny@users.noreply.github.com>
Contributor
Author
Added Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Contributor
|
📖 https://ucd-serg.github.io/serodynamics/preview/pr180 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
✅ Implementation Complete: Residual-Based Posterior Predictive Diagnostics
Summary
Successfully implemented residual-based posterior predictive diagnostics for individual-level antibody trajectories, providing quantitative measures to complement visual assessments from
plot_predicted_curve().Key Features Added
1.
compute_residual_metrics()- Main exported function2.
predict_posterior_at_times()- Internal helperplot_predicted_curve()andcompute_residual_metrics()ab())Documentation
Testing
Files Changed
New files (5):
R/compute_residual_metrics.RR/predict_posterior_at_times.Rinst/examples/examples-compute_residual_metrics.Rtests/testthat/test-compute_residual_metrics.Rtests/testthat/_snaps/compute_residual_metrics/*.csv(2 snapshot files)Modified files (7):
NAMESPACE- Added exportDESCRIPTION- Version 0.0.0.9047 → 0.0.0.9048NEWS.md- Feature documentationinst/WORDLIST- Added "pred"vignettes/articles/getting-started.qmd- New sectionman/compute_residual_metrics.Rd- Generated docspkgdown/_pkgdown.yml- Added to reference indexExample Usage
Quality Assurance
|>cli::cli_abort()andcli::cli_warn()Integration
plot_predicted_curve()workflowsr_model,case_data)Recommended Workflow
run_mod()plot_predicted_curve()compute_residual_metrics()← NEWOriginal prompt
This section details on the original issue you should resolve
<issue_title>Add residual-based posterior predictive diagnostics for individual-level antibody trajectories</issue_title>
<issue_description>### Background and motivation
In recent discussions, we identified a need to quantify how well posterior predicted trajectories match observed antibody measurements at the individual level, instead of relying on subjective visual comparisons (“it looks better”).
The package already provides
plot_predicted_curve()to overlay:observed data points, and
posterior-predicted median curves with 95% credible intervals,
optionally with all posterior sample curves.
However, there is currently no built-in way to compute residual-based metrics that summarize the distance between observed measurements and model predictions. These metrics are useful because they:
provide an objective summary of fit for each individual and biomarker,
can be reported in Results/Supplemental tables,
support routine posterior predictive evaluation in a modern Bayesian workflow,
are general and can be applied to any dataset fit using
run_mod().The goal is to compute residuals using the posterior median predicted trajectory evaluated at the observed timepoints (not on a dense grid), and then summarize residual magnitude across observations.
Scope of work
Add utilities that:
1. Extract posterior predicted values at observed timepoints
Use the same posterior draws and the same within-host model function used in
plot_predicted_curve()(viaab()).Evaluate predictions at the dataset’s observed visit times for each ID and antigen–isotype.
Compute posterior summaries at each observed timepoint:
median prediction (required)
optional 2.5% / 97.5% (optional, for context)
2. Compute residuals between observed and predicted values
At each observed timepoint, compute residuals relative to the posterior median prediction:
raw residual:
obs - pred_medabsolute residual:
abs(obs - pred_med)squared residual:
(obs - pred_med)^2Support a
scaleoption to compute residuals on:original scale (e.g., ELISA units or MFI)
log scale (recommended when the model is fit on log measurements), i.e.
log(obs) - log(pred_med)with careful handling of non-positive values.
3. Provide summary metrics by individual and biomarker
Return tidy summaries that can be used directly in reports/tables, such as:
MAE(mean absolute error)RMSE(root mean squared error)SSE(sum of squared errors)n_obs(number of observations used)Support multiple summary levels:
pointwise residual output (one row per observation)
per
id × antigen_isoper
antigen_iso(aggregated across IDs)overall (single summary)
4. Integrate cleanly with the existing plotting workflow
Do not overload plotting with diagnostics. The workflow should be:
plot_predicted_curve()for visualizationa separate residual-metric function for quantitative evaluation
However, to avoid duplicated computation, implement a shared internal helper that both functions can reuse to produce posterior predictions.
5. Provide documentation + examples using package data
Use
serodynamics::nepal_seesandserodynamics::nepal_sees_jags_outputto demonstrate:computing residual metrics for one ID and one biomarker,
computing residual summaries across multiple IDs (faceted use-case),
returning a tidy table for reporting.
Required additions
New exported function(s)
(suggested names; final naming can be adjusted to package conventions)
compute_residual_metrics()Computes pointwise residuals and/or summarized metrics.
(Optional internal helper)
predict_posterior_at_times()Shared logic extracted from
plot_predicted_curve()to compute posterior median predictions at arbitrary times.Unit tests (use
serodynamics::nepal_sees+serodynamics::nepal_sees_jags_output)Dimensional consistency:
Output validity:
Robust handling:
roxygen2 documentation
Tutorial / vignette update
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.