Skip to content

RXR-3164: Pass observation-level weights to covariance matrix calcs, and re-activate FOCE-type vcov#61

Merged
jasmineirx merged 11 commits into
masterfrom
RXR-3164
Jul 15, 2026
Merged

RXR-3164: Pass observation-level weights to covariance matrix calcs, and re-activate FOCE-type vcov#61
jasmineirx merged 11 commits into
masterfrom
RXR-3164

Conversation

@roninsightrx

@roninsightrx roninsightrx commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

There were two issues with the covariance matrix computation:

  • the computational approach was changed from finite differences using numDeriv, to FOCE-style analytic. While both are "correct", the latter one is guaranteed positive-definite, and faster. Slightly different definition, but now same definition as used in NONMEM. So we'll use that going forward.
  • the new computation however didn't use the weights that were used in the fit, so wasn't computing the right cov matrix. These are applied now.

Note: PR to update the handful of failing f-tests. After merging this PR, immediately rerun f-tests there.

@roninsightrx roninsightrx changed the title pass observation-level weights Pass observation-level weights to solve issue with covariance matrix calcs Jun 25, 2026
@roninsightrx roninsightrx changed the title Pass observation-level weights to solve issue with covariance matrix calcs Pass observation-level weights to covariance matrix calcs, and re-activate FOCE-type vcov Jun 30, 2026
@roninsightrx roninsightrx changed the title Pass observation-level weights to covariance matrix calcs, and re-activate FOCE-type vcov RXR-3164: Pass observation-level weights to covariance matrix calcs, and re-activate FOCE-type vcov Jun 30, 2026
@roninsightrx
roninsightrx requested a review from jasmineirx June 30, 2026 14:45

@jasmineirx jasmineirx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, a couple suggestions for better tests

Comment thread tests/testthat/test-calc_cwres.R Outdated
Comment thread tests/testthat/test-get_map_estimates.R Outdated
@roninsightrx
roninsightrx requested a review from jasmineirx July 1, 2026 12:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes variance–covariance (vcov) computation for MAP estimation by ensuring per-observation weights are incorporated into the FOCE-style Hessian/vcov, and by re-preferencing the FOCE-derived vcov (aligned with NONMEM’s FOCE-I behavior) for speed and positive-definiteness.

Changes:

  • Thread per-observation weights into FOCE Hessian/vcov computation (calc_cwres() via calc_residuals()), and prefer FOCE vcov in get_map_estimates().
  • Correct numDeriv-based vcov scaling in mle_wrapper() for an objective on the -2*logLik scale.
  • Add/adjust tests to validate FOCE vcov vs NONMEM reference values and to ensure weights affect vcov curvature.

Reviewed changes

Copilot reviewed 7 out of 10 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
tests/testthat/test-nonmem-vcov-comparison.R Adds a hard-coded NONMEM cross-check for FOCE vcov under unit vs weighted observations.
tests/testthat/test-get_map_estimates.R Updates MAP expectations and adds a FOCE vs numDeriv vcov comparison.
tests/testthat/test-calc_cwres.R Adds a focused regression test that weights influence FOCE vcov but not CWRES.
R/mle_wrapper.R Adjusts vcov scaling to match a -2*logLik objective.
R/get_map_estimates.R Skips numDeriv Hessian when residuals/FOCE Jacobian is available; prefers FOCE vcov source.
R/calc_residuals.R Forwards weights into calc_cwres() so FOCE Hessian curvature matches the weighted OFV.
R/calc_cwres.R Applies per-observation weights in the FOCE Hessian likelihood term.
man/parse_residuals_from_predictions.Rd Updates weights parameter documentation.
man/calc_residuals.Rd Updates weights parameter documentation.
man/calc_cwres.Rd Documents new weights argument in calc_cwres().
Files not reviewed (3)
  • man/calc_cwres.Rd: Generated file
  • man/calc_residuals.Rd: Generated file
  • man/parse_residuals_from_predictions.Rd: Generated file

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread R/calc_cwres.R Outdated
Comment thread R/calc_cwres.R
Comment thread R/get_map_estimates.R
Comment thread tests/testthat/test-nonmem-vcov-comparison.R Outdated
Comment thread tests/testthat/test-get_map_estimates.R
Comment thread man/parse_residuals_from_predictions.Rd Outdated
Comment thread man/calc_residuals.Rd Outdated

@jasmineirx jasmineirx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. thanks for adding the NONMEM equivalence test

@jasmineirx
jasmineirx merged commit 2f028e9 into master Jul 15, 2026
1 check passed
@jasmineirx
jasmineirx deleted the RXR-3164 branch July 15, 2026 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants