Repository navigation
Avoid the per-column loop of strip_contrasts() in the projection predictions - #652
Merged
Merged
Conversation
aloctavodia
approved these changes
Nov 19, 2026
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
#619 added
strip_contrasts(), which removes thecontrastsattributes of the columns of a data frame. It loops over all columns withdata[[nm]], and each column costs one[[.data.framedispatch.mu_fun()ofinit_refmodel()called it for every projection, also for the stored training data.init_refmodel()already strips the stored training data, so the call for these data had no effect. The cost grows with the number of columns. Thus the LOO validated search became slower for data with many predictors.This PR makes two changes:
strip_contrasts()finds the columns with acontrastsattribute in one pass over the column list and changes only these columns.mu_fun()callsstrip_contrasts()only for new data from the caller (newdata), not for the stored training data.The results change only for a data frame with two columns of the same name. The old loop removed the attribute only from the first of them; now
strip_contrasts()removes it from both.Effect
Setup: Bernoulli data with N 100 and D 100 or 500 predictors, brms reference model, forward search with
nterms_max = 20.cv_varsel()settings:cv_method = "LOO",nloo = 50,validate_search = TRUE. Times are in seconds. Master is 228756c; both versions ran in the same job on one node.loo_perturb_alphaInfInfBoth versions give the same rankings, suggested sizes and performance summaries (
all.equal(), tolerance 1e-12).Tests
New tests in
tests/testthat/test_contrasts.R:strip_contrasts()removes only thecontrastsattributes. Cases: factor columns with a matrix and with a function name, other attributes, character and matrix columns, duplicated column names, a data frame without columns.mu_fun()does not callstrip_contrasts()for the stored training data. For a subset of the training data (obs), onlysubfit_newdata_x()calls it, once.mu_fun()gives the same predictions for new data withcontrastsattributes as for the training rows. The test uses aproj_predfunthat stops for new data with acontrastsattribute.Without the change, 3 of the new expectations fail (duplicated column names and the two call counts). The full test suite with
NOT_CRAN=truepasses (R 4.5.3, lme4 2.0-6): 0 failures and 5 skips. It gives 19 warnings, the same as other full-suite runs on master: 1 CHOLMOD warning, 17 GPD warnings and 1pareto_kwarning.This PR was assisted by Claude.