RXR-3072: Add proper CWRES computation#59
Merged
Conversation
…2007) Replace the incorrect ad-hoc CWRES normalization with proper FOCE-based conditional weighted residuals. The Jacobian F = df/deta is computed via central finite differences and used to derive both CWRES and the FOCE variance-covariance matrix, replacing the more expensive numDeriv::hessian computation when residuals=TRUE. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Runs the same fit with both methods and checks the variance-covariance matrix values are equal within 1% tolerance. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Implements FOCE-based CWRES (Hooker et al. 2007) by computing the Jacobian (F = \partial f/\partial \eta) via central finite differences, then reusing it to derive both CWRES and a FOCE-based eta variance-covariance matrix—avoiding the default numDeriv::hessian path when residuals=TRUE.
Changes:
- Add new
calc_cwres()+simulate_with_etas()helpers to compute FOCE CWRES and FOCE vcov from a finite-difference Jacobian. - Update
get_map_estimates()/calc_residuals()to prefer FOCE vcov when residuals are computed and skipnumDeriv::hessianin that path. - Update tests and regenerate documentation (
.Rdfiles), plus bumpRoxygenNote.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
R/calc_cwres.R |
New FOCE CWRES + vcov implementation and simulation helper used for Jacobian finite differences. |
R/calc_residuals.R |
Integrates FOCE CWRES/vcov computation into residual calculation; stores FOCE vcov for later use. |
R/get_map_estimates.R |
Skips numDeriv Hessian when residuals are requested; prefers FOCE vcov when available. |
R/parse_residuals_from_predictions.R |
Removes placeholder CWRES computation; initializes CWRES to NA pending FOCE computation. |
tests/testthat/test-get_map_estimates.R |
Adds/updates tests for FOCE vcov behavior vs numDeriv fallback when residuals=FALSE. |
man/calc_cwres.Rd |
New generated documentation for calc_cwres(). |
man/simulate_with_etas.Rd |
New generated internal documentation for simulate_with_etas(). |
man/calc_residuals.Rd |
Updates generated docs to include new residual-calculation arguments. |
DESCRIPTION |
Updates RoxygenNote. |
CLAUDE.md |
Adds repository guidance for Claude Code workflows/architecture. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
The MAP estimation objective uses omega/weight_prior_var as the prior
covariance. The FOCE Hessian must match: H = (Omega/w)^{-1} + F'S^{-1}F.
CWRES continues to use the unscaled omega (model-based diagnostic).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
When residual error variance is zero, negative, or non-finite for any observation (e.g. both error$prop and error$add are 0 for an obs_type), return cwres=NA and vcov=NULL with a warning identifying the offending observations, instead of silently producing Inf/NaN. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Resolves conflicts in R/calc_residuals.R and R/get_map_estimates.R where master removed `...` while this branch added new explicit parameters and used `...` to forward to the new CWRES code. Resolution: accept master's intent (drop `...` from the public signatures and all downstream sim_ode calls) while keeping the new explicit `nonfixed`, `as_eta`, `steady_state_analytic`, and `weight_prior_var` parameters of calc_residuals(). Also drop `...` from calc_cwres()/simulate_with_etas() since nothing flows in any more. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
When the prior is flat (weight_prior=0) the scaled omega is undefined and the FOCE Hessian H = (Omega/w)^-1 + F'Sigma^-1*F is not meaningful. Return vcov = NULL in that case so the caller falls back to omega$full cleanly. CWRES itself is unaffected (uses unscaled omega). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Summary
numDeriv::hessiancomputation whenresiduals=TRUE(default), since the FOCE HessianH = Ω⁻¹ + F'Σ⁻¹Fis derived analytically from the same JacobiannumDeriv::hessianfor edge cases (e.g. single TDM scenarios)residuals=FALSE, the existingnumDeriv::hessianpath is preserved as fallbackTest plan
residuals=TRUE) and numDeriv path (residuals=FALSE)🤖 Generated with Claude Code