Skip to content

Add resi_pe() for mgcv gam/bam fits - #58

Merged
simonvandekar merged 1 commit into
masterfrom
gam-resi
Nov 20, 2026
Merged

simonvandekar merged 1 commit into
masterfrom
gam-resi

Conversation

@simonvandekar

@simonvandekar simonvandekar commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Adds resi_pe() point estimates for mgcv::gam and mgcv::bam fits. Confidence intervals are not included yet.

What it does

  • Smooth terms: f2S(F, Ref.df, n - sum(edf), n) from mgcv's Vp-based Wald statistic, or chisq2S when the scale is known (e.g. binomial). This was chosen because mgcv's statistic uses a rank-Ref.df pseudo-inverse, so its null mean is about Ref.df, and the -df/n correction matches it.
  • Parametric terms: factor terms use the same conversion with their own df; coefficients use t2S/z2S.
  • Terms mgcv tests differently (bs = "re", shrinkage bases, select = TRUE) return NA with a warning.
  • resi() on a gam/bam stops with a message pointing to resi_pe(), since intervals aren't implemented.
  • mgcv added to Suggests, R/resi_pe_gam.R added to Collate (otherwise an installed package omits the method), NEWS entry, regenerated man/.

Validation

  • tests/testthat/test-resi_pe_gam.R: 9 expectations pass; the full test suite passed on this branch.
  • Synthetic calibration (AIgams gamCalibrationSim/): the target is T2(k), the Wald noncentrality per observation of the fixed-basis projection (as for RESI under misspecification in other models). Scenarios include a threshold effect (truncation), a weak non-linear effect and heteroskedastic errors, with k in {5, 10, 20}. Null rejection rates are 0.03-0.06. Estimates are within 1% of T2(k) by n = 20000 (2% for the weak curve) and under 4% by n = 1000, except the weak curve, where REML often fits a straight line at small n (-36% at n = 200, -29% at 1000, -6% at 5000).
  • Resampling evaluation on insurance (AIgams tests/run_gam_insurance.R), including smoker x nonlinear age and smoker x bmi difference curves: real effects are within 2% by n = 1000 (smoker x bmi: -1% to -2% at n = 5000). The null term rejects at 0.044-0.068.
  • Covariance: mgcv's default Vp (parametric). mgcv's vcov.gam(sandwich = TRUE) was evaluated: it is consistent for the robust T2(k) and its SEs hold up under heteroskedasticity, where Vp SEs are 18-28% too small. Exposing it as an option is left for the CI work.

Independent of #57: this branches from master and touches different files.

Smooth-term RESI from mgcv's Vp Wald statistic with Ref.df (f2S, or chisq2S
when the scale is known); parametric terms via t2S/z2S; terms tested with
mgcv's random-effect test (bs = 're', shrinkage, select = TRUE) return NA.
resi() errors informatively (CIs not yet implemented).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
simonvandekar added a commit to simonvandekar/AIgams that referenced this pull request Nov 19, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

@simonvandekar simonvandekar left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Looks good

@simonvandekar
simonvandekar merged commit 46bb70e into master Nov 20, 2026
1 check passed
@simonvandekar
simonvandekar deleted the gam-resi branch November 20, 2026 16:14
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.

1 participant