Skip to content

Avoid the per-column loop of strip_contrasts() in the projection predictions - #652

Merged
avehtari merged 3 commits into
masterfrom
strip-contrasts-speed
Nov 19, 2026
Merged

avehtari merged 3 commits into
masterfrom
strip-contrasts-speed

Conversation

@avehtari

Copy link
Copy Markdown
Member

Summary

#619 added strip_contrasts(), which removes the contrasts attributes of the columns of a data frame. It loops over all columns with data[[nm]], and each column costs one [[.data.frame dispatch. mu_fun() of init_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 a contrasts attribute in one pass over the column list and changes only these columns.
  • mu_fun() calls strip_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.

D loo_perturb_alpha master this PR speedup
100 default 558.7 497.3 1.12
100 Inf 570.3 443.3 1.29
500 default 4784.3 3098.7 1.54
500 Inf 4207.9 2961.8 1.42

Both 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 the contrasts attributes. 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 call strip_contrasts() for the stored training data. For a subset of the training data (obs), only subfit_newdata_x() calls it, once.
  • mu_fun() gives the same predictions for new data with contrasts attributes as for the training rows. The test uses a proj_predfun that stops for new data with a contrasts attribute.

Without the change, 3 of the new expectations fail (duplicated column names and the two call counts). The full test suite with NOT_CRAN=true passes (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 1 pareto_k warning.

This PR was assisted by Claude.

@avehtari avehtari added the performance Performance issues (i.e., high runtime or high memory usage). label Nov 17, 2026
@avehtari
avehtari merged commit 7e2d0d9 into master Nov 19, 2026
6 checks passed
@avehtari
avehtari deleted the strip-contrasts-speed branch November 19, 2026 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance issues (i.e., high runtime or high memory usage).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants