Registry Mirror
Skip to content

Conversation

@avehtari

Copy link
Copy Markdown
Member

Summary

Fixes #150 and #384.

  • as.matrix.projection() for GAMs: Duplicated linear term #150: a submodel with x and s(x) had both terms. With the default bases of mgcv, s(x) contains the linear term, so the coefficient of x is not identifiable. as.matrix() showed this coefficient. The projection now drops x from such a submodel, and project() gives a message (option projpred.mssg_smooth_linear turns it off).
  • Inconsistency in reference model formula between GAMs and GAMMs #384: for an rstanarm GAMM, formula.gamm4() added the variables of the smooth terms as linear terms to the reference model formula. For an rstanarm GAM, it did not. Now both formulas have only the terms of the model. The search candidates do not change, because split_formula() adds these linear terms as candidates in both cases.

Changes

  • proj_to_submodl() calls flatten_formula(duplicates = FALSE), which drops these linear terms. The helper smooth_covered_terms() finds the linear terms that a smooth term contains; it parses the smooth call (the old regex stopped at the first )).
  • formula.gamm4() adds only the group-level terms to the formula of the gam part.
  • ?project (argument predictor_terms) describes the drop.

Notes

  • The forward search can still select x and then s(x). The submodel of that size is now fitted as s(x), and its size counts a term that the fit does not use. In the mgcv check, the fitted values of x + s(x) and s(x) differ by at most 1.8e-4 (REML), so the search order can change only in close cases.
  • nterms_all (the number of terms of the reference model formula) of an rstanarm GAMM now has the same definition as for rstanarm GAMs and brms GAMMs. It feeds the warning about unstable full-model projections (Projection onto the full model for multilevel Gaussian models #323) and the "Could not suggest submodel size" warning of suggest_size(). For GAMs and GAMMs, this count is smaller than the largest search size, because the search also has the linear terms of the smooth variables.

Tests

  • test_formula.R (no Stan fit): smooth_covered_terms(), flatten_formula(duplicates = FALSE) and formula.gamm4().
  • test_project.R: for an rstanarm GAM, the projection onto c("s.1", "s(s.1)") gives the message and the same fits and predictions as the projection onto "s(s.1)"; "s.1" alone keeps the linear term.
  • test_refmodel.R: the formulas of the rstanarm GAM and GAMM reference models have no linear term of a smooth variable.
  • These tests fail on master. Full suite with NOT_CRAN=true (R 4.5.3, lme4 2.0.6, gamm4 0.3.0, mgcv 1.9.4, rstanarm 2.32.2, brms 2.23.1): 58,560 expectations, 0 failures, 1 error in the new test_project.R test. After the fix of this test, the three changed test files have 0 failures and 0 errors.

This PR was assisted by Claude

@avehtari avehtari added the bug Bugs. label Nov 12, 2026
@avehtari
avehtari marked this pull request as draft November 12, 2026 09:55
@avehtari
avehtari marked this pull request as ready for review November 12, 2026 10:21
Comment thread R/formula.R
Comment on lines +66 to +69
smooth_args <- function(smooth) {
cl <- str2lang(smooth)
return(unname(vapply(as.list(cl)[-1], deparse_combine, character(1))))
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
smooth_args <- function(smooth) {
cl <- str2lang(smooth)
return(unname(vapply(as.list(cl)[-1], deparse_combine, character(1))))
}
smooth_args <- function(smooth) {
args <- as.list(str2lang(smooth))[-1]
arg_nms <- names(args)
if (is.null(arg_nms)) arg_nms <- rep("", length(args))
args <- args[arg_nms == ""]
return(unname(vapply(args, deparse_combine, character(1))))
}

Currently redundant, but more robust?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Bugs.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

as.matrix.projection() for GAMs: Duplicated linear term

2 participants