Skip to content

get_predicted(), get_modelmatrix(): keep new-data rows for lme, gls, clmm and brms models - #1260

Merged
strengejacke merged 16 commits into
easystats:mainfrom
jmgirard:new-data-rows
Oct 11, 2026
Merged

strengejacke merged 16 commits into
easystats:mainfrom
jmgirard:new-data-rows

Conversation

@jmgirard

Copy link
Copy Markdown
Contributor

With new data, some rows were dropped or replaced by the predictions for the model data. This PR keeps one row per data row.

get_modelmatrix() for lme, gls, clmm and brmsfit models with new data:

  • A row with a missing value, or a factor level that the model data does not have, now holds NA values. Before, model.matrix() dropped it, so the rows no longer lined up with the data.
  • For brms, an ordered factor given as a character vector in data keeps the polynomial contrasts (g.L, g.Q). Before, it got treatment contrasts.

get_predicted() for lme and gls models (new method get_predicted.lme(), also used for gls):

  • Rows with a missing value or an unknown factor level in a fixed-effect predictor are NA, with one warning that names the columns. If no row can be predicted, the result is NULL with a warning. Before, predict() failed and the default method fell back to fitted(), so the result was the predictions for the model data (for example 120 rows for 5 rows of new data).
  • If data has no grouping column, lme models give population-level predictions (level = 0), as for lme4 models. Before, this also returned the predictions for the model data.
  • gnls and nlme models, and calls without data, still use the default method.

Confidence intervals of lme models: nlme stores one df value per coefficient (fixDF$X). get_predicted_ci() passed that vector to qt(), so the values were recycled against the predictions (with 2 new rows and 4 coefficients, the result had 4 rows). Now the smallest value is used for all rows, with or without new data.

get_predicted() with new data for models without a predict() method for new data (for example clmm): the fitted() fallback is only used when it has one value per data row. Otherwise the result is NULL with the existing "Could not compute predictions" warning. Before, clmm returned the fitted values of the model data.

Tests are in tests/testthat/test-get_modelmatrix.R and a new file, tests/testthat/test-get_predicted-newdata.R, because test-get_predicted.R skips on macOS. The full test suite passes locally, and the parameters tests pass with this branch installed. devtools::check() gives the same results as on main.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

New-data fallback can still return training predictions, while the new lme method ignores explicit prediction levels and requested confidence levels.

3 open findings
What changed in this PR

Improves new-data prediction and model-matrix row alignment for mixed, ordinal, and Bayesian models.

Changes:

  • Preserves rows containing missing or unknown predictor values.
  • Adds dedicated lme/gls prediction handling and corrects confidence-interval degrees of freedom.
  • Adds regression tests, release notes, S3 registrations, and a version bump.
File Description
R/​get_modelmatrix.R Preserves new-data rows and ordered contrasts.
R/​get_predicted.R Restricts fitted-value fallback by row count.
R/​get_predicted_mixed.R Adds lme and gls prediction methods.
R/​get_predicted_ci.R Uses the minimum lme degrees of freedom.
tests/​testthat/​test-get_modelmatrix.R Tests row preservation and ordered factors.
tests/​testthat/​test-get_predicted-newdata.R Tests new-data predictions and intervals.
NAMESPACE Registers new S3 methods.
NEWS.md Documents user-visible fixes.
DESCRIPTION Increments the development version.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread R/get_predicted.R
Comment thread R/get_predicted_mixed.R
Comment thread R/get_predicted_mixed.R
@strengejacke
strengejacke merged commit 9b641d9 into easystats:main Oct 11, 2026
7 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants