Registry Mirror
Skip to content

Conversation

@avehtari

@avehtari avehtari commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Summary

Part of #238: this PR allows factor by variables and the argument bs. A numeric by variable still gives an error. #145 (the varsel() error with s(x, by = g)) was closed as a duplicate of #238. Its example uses the by variable also as a grouping factor, which still gives an error that suggests bs = "fs".

Since #636, projpred gives an error for each named argument in a smooth term of a brms or mgcv reference model, including k, bs and by. This PR allows:

  • k (basis dimension) and bs ("tp", "cr", "ps"), for example s(x, k = 5) and t2(x, z, k = c(4, 6));
  • a factor by variable, for example s(x, by = id);
  • factor smooths s(x, id, bs = "fs").

Other arguments (id, sp, fx, m, xt, pc, d) still give an error that names the term and the allowed arguments. The new terms work in the search, in all submodel fitters, in predictions for new data and in as.matrix() for brms and rstanarm reference models.

This PR depends on #692.

Changes

  • Parsing: projpred parses a smooth term as a call (smooth_term_info(), smooth_args(), called_funs()), not as text. This also fixes three cases of master: relevel(f, ref = "class s(1)") counted as a smooth term; mgcv::s(x) counted as an interaction (now an error that asks for s(x)); log(s(x)) stopped with "subscript out of bounds" (now an error).
  • Checks (check_smooth_call()): k must be a constant whole number of at least the smallest basis dimension of mgcv. In s(), "cr" and "ps" need one predictor. In t2(), k and bs have one value or one per predictor. Two smooth terms that differ only in k or bs give an error (mgcv labels both s(x)). The shrinkage bases "ts", "cs" and the cyclic bases "cc", "cp" still give an error.
  • Factor by variables: one smooth per level, with one penalty and one smoothing parameter per smooth (as in mgcv and brms). The by variable must be an unordered factor with at least two levels (a character variable becomes a factor). It must not be a grouping factor of a group-level term; the error suggests s(x, g, bs = "fs"). A formula with s(x) must not also have s(x, by = id). The search adds id + s(x, by = id) as one term, because the level smooths do not contain the level means. It adds this term only after x. An unknown level gives NA predictions in the fixed-basis fits, with one warning per prediction call (the NA warning).
  • Factor smooths: the factor must be the last predictor and is not a search term. The search adds the factor smooth after x. An unknown level contributes 0, also with gam() and gamm4(). For s(x, g, bs = "fs") + (1 | g), the factor smooth takes the level intercepts, so the projected SD of (1 | g) can be 0.
  • GAMM submodels of Fit Gaussian additive and multilevel submodels of all projected draws at once #692: a by smooth with random intercepts gets the fixed-basis GAMM fit. Factor smooths (3 penalty matrices) and by smooths with slopes get the fits at one theta. as.matrix() maps the fixed-basis GAMM fits with a by factor to the brms and rstanarm bases.
  • Formulas without k and bs: for y ~ t2(x) + (x | g), the default search term is t2(x) + (x | g) (master: s(x) + (x | g)). A linear x does not enter the search after t2(x, z) (on master, project() dropped it anyway, Drop a linear term next to a smooth term of the same variable #626). For s(log(x)), the linear search term is log(x) (master gives the text log(x).
  • smooth_sds() computes an SD only for the returned blocks. On master, the null-space block of a "cr" basis in rstanarm (penalty factor about 9e-15, can be negative) gave the warning "NaNs produced" for each draw.

Limits

  • project() does not match predictor_terms in another notation (for example "s(x,k=3)"). The separate PR "Match predictor_terms of project() also in another notation" adds this.
  • User predictor_terms without the main effect of a by factor give NA predictions (with the NA warning) for unknown levels in new data; the default search always adds the main effect.
  • as.matrix() stops if the reference model has weight 0 for all rows of one by level.

Speed

Simulated data, n = 300, brms reference models without group-level terms. The table gives the range over 3 processes of the time ratio of the per-draw gam() fits (projpred.smooth_fixed_basis = FALSE) to the fixed-basis fits. Master does not accept these terms.

model varsel() cv_varsel(), LOO LOO, validated search, nloo = 10
id + s(x, by = id), Gaussian 18.6 to 22.4 15.1 to 17.0 24.3 to 27.0
id + s(x, by = id), latent Bernoulli 14.4 to 21.0 10.2 to 12.2 22.0 to 25.6
s(x) + s(x, id, bs = "fs"), Gaussian 18.4 to 32.9 24.0 to 25.2 23.6 to 43.5
s(x) + s(x, id, bs = "fs"), latent Bernoulli 21.6 to 27.7 23.5 to 34.1 30.5 to 52.8

The rankings (also the fold-wise ones) and the suggested sizes were the same. The elpd values differed by at most 0.61, except in the validated LOO runs of the factor smooths (1.2 Gaussian; 6.8 Bernoulli at a size with SE 20.9). A Bernoulli run with nloo = 50 gave the same ranking and size and elpd differences of -0.44 to 1.34 (SE 0.8 to 10.4).

Tests

  • test_formula.R (7 new blocks), test_smooth_args.R (new, 5 blocks) and test_smooth_by.R (new, 29 blocks): parsing and each error, the search terms and their order, the gam(), gamm4() and fixed-basis fits (also GAMM submodels in both fitters and at one theta), new and lost levels, the NA warning, and LOO CV with validated search.
  • test_gamm_fixed.R (1 new block): with one draw, the fixed-basis GAMM fit of a by smooth and of a "cr" smooth equals the REML fit of gamm4() (tolerance 1e-4).
  • test_search_gauss.R, test_smooth_fit.R, test_smooth_basis.R (15 new blocks): the cross-product search, the fixed-basis fits and as.matrix() for the new terms; test_refmodel.R: stan_gamm4() fits with k and bs.
  • With the code of Fit Gaussian additive and multilevel submodels of all projected draws at once #692, the test files of this PR fail with 3 failures and 54 errors (a run before the NA warning and the blocks at one theta were added); 51 of them are the error for named arguments. Without the code of the NA warning, the 2 blocks that test it fail.
  • Full suite on CSC Roihu (R 4.6.1, testthat 3.3.2, lme4 2.0.6, brms 2.23.1, rstanarm 2.32.2). With NOT_CRAN=true at the branch head: 861 blocks, 66,919 expectations, 0 failed, 1 skipped, 14 warnings. In CRAN mode, before the merges of Raise five tolerances near machine precision #691 and of the GCV change of Fit Gaussian additive and multilevel submodels of all projected draws at once #692: 9,418 expectations, 0 failed, 2 warnings. The warnings are the same as in the runs of Fit Gaussian additive and multilevel submodels of all projected draws at once #692.
  • Not tested: K-fold CV with these terms, and MCMC fits of brms or rstanarm with bs = "ps".

This PR was assisted by Claude

… with gamm4, and update the docs and NEWS for the merged fitters
…ompare by and cr smooths of GAMM submodels with gamm4 REML fits, and correct the option docs
@avehtari avehtari added the feature New feature label Dec 1, 2026
@avehtari
avehtari added this pull request to stack #694 December 1, 2026 14:06
@avehtari
avehtari requested a review from aloctavodia December 1, 2026 14:06
# Conflicts:
#	tests/testthat/test_smooth_fit.R
# Conflicts:
#	tests/testthat/test_smooth_fit.R
Comment thread R/smooth_fit.R
by_lvls <- list()
for (sm in des$smooths) {
if (!identical(sm$by, "NA") && !is.null(sm$by.level)) {
by_lvls[[sm$by]] <- c(by_lvls[[sm$by]], sm$by.level)

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
by_lvls[[sm$by]] <- c(by_lvls[[sm$by]], sm$by.level)
by_lvls[[sm$by]] <- unique(c(by_lvls[[sm$by]], sm$by.level))

This branch has not been deployed

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

Labels

feature New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants