Conversation
…ace and seed two test projections
… 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
…s that are grouping factors
…hat the fit does not know
…ap of a factor smooth with (1 | g)
# Conflicts: # tests/testthat/test_smooth_fit.R
# Conflicts: # tests/testthat/test_smooth_fit.R
aloctavodia
approved these changes
Dec 4, 2026
| 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) |
Collaborator
There was a problem hiding this comment.
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
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
Part of #238: this PR allows factor
byvariables and the argumentbs. A numericbyvariable still gives an error. #145 (thevarsel()error withs(x, by = g)) was closed as a duplicate of #238. Its example uses thebyvariable also as a grouping factor, which still gives an error that suggestsbs = "fs".Since #636, projpred gives an error for each named argument in a smooth term of a brms or mgcv reference model, including
k,bsandby. This PR allows:k(basis dimension) andbs("tp","cr","ps"), for examples(x, k = 5)andt2(x, z, k = c(4, 6));byvariable, for examples(x, by = id);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 inas.matrix()for brms and rstanarm reference models.This PR depends on #692.
Changes
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 fors(x));log(s(x))stopped with "subscript out of bounds" (now an error).check_smooth_call()):kmust be a constant whole number of at least the smallest basis dimension of mgcv. Ins(),"cr"and"ps"need one predictor. Int2(),kandbshave one value or one per predictor. Two smooth terms that differ only inkorbsgive an error (mgcv labels boths(x)). The shrinkage bases"ts","cs"and the cyclic bases"cc","cp"still give an error.byvariables: one smooth per level, with one penalty and one smoothing parameter per smooth (as in mgcv and brms). Thebyvariable 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 suggestss(x, g, bs = "fs"). A formula withs(x)must not also haves(x, by = id). The search addsid + s(x, by = id)as one term, because the level smooths do not contain the level means. It adds this term only afterx. An unknown level givesNApredictions in the fixed-basis fits, with one warning per prediction call (theNAwarning).x. An unknown level contributes 0, also withgam()andgamm4(). Fors(x, g, bs = "fs") + (1 | g), the factor smooth takes the level intercepts, so the projected SD of(1 | g)can be 0.bysmooth with random intercepts gets the fixed-basis GAMM fit. Factor smooths (3 penalty matrices) andbysmooths with slopes get the fits at onetheta.as.matrix()maps the fixed-basis GAMM fits with abyfactor to the brms and rstanarm bases.kandbs: fory ~ t2(x) + (x | g), the default search term ist2(x) + (x | g)(master:s(x) + (x | g)). A linearxdoes not enter the search aftert2(x, z)(on master,project()dropped it anyway, Drop a linear term next to a smooth term of the same variable #626). Fors(log(x)), the linear search term islog(x)(master gives the textlog(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 matchpredictor_termsin another notation (for example"s(x,k=3)"). The separate PR "Matchpredictor_termsofproject()also in another notation" adds this.predictor_termswithout the main effect of abyfactor giveNApredictions (with theNAwarning) 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 onebylevel.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.varsel()cv_varsel(), LOOnloo = 10id + s(x, by = id), Gaussianid + s(x, by = id), latent Bernoullis(x) + s(x, id, bs = "fs"), Gaussians(x) + s(x, id, bs = "fs"), latent BernoulliThe 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 = 50gave 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) andtest_smooth_by.R(new, 29 blocks): parsing and each error, the search terms and their order, thegam(),gamm4()and fixed-basis fits (also GAMM submodels in both fitters and at onetheta), new and lost levels, theNAwarning, and LOO CV with validated search.test_gamm_fixed.R(1 new block): with one draw, the fixed-basis GAMM fit of abysmooth and of a"cr"smooth equals the REML fit ofgamm4()(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 andas.matrix()for the new terms;test_refmodel.R:stan_gamm4()fits withkandbs.NAwarning and the blocks at onethetawere added); 51 of them are the error for named arguments. Without the code of theNAwarning, the 2 blocks that test it fail.NOT_CRAN=trueat 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.bs = "ps".This PR was assisted by Claude