Repository navigation
get_predicted(), get_modelmatrix(): keep new-data rows for lme, gls, clmm and brms models - #1260
Merged
Merged
Conversation
…s, ordered predictors as character
…o grouping column
… ordered factors ordered
… or unknown levels, population-level predictions without grouping columns, one df per row
… another number of rows
…ns, df note in NEWS
Contributor
There was a problem hiding this comment.
🟡 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/glsprediction 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.
…level and ci for lme and gls
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.


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()forlme,gls,clmmandbrmsfitmodels with newdata:NAvalues. Before,model.matrix()dropped it, so the rows no longer lined up with the data.datakeeps the polynomial contrasts (g.L,g.Q). Before, it got treatment contrasts.get_predicted()forlmeandglsmodels (new methodget_predicted.lme(), also used forgls):NA, with one warning that names the columns. If no row can be predicted, the result isNULLwith a warning. Before,predict()failed and the default method fell back tofitted(), so the result was the predictions for the model data (for example 120 rows for 5 rows of new data).datahas no grouping column,lmemodels give population-level predictions (level = 0), as for lme4 models. Before, this also returned the predictions for the model data.gnlsandnlmemodels, and calls withoutdata, still use the default method.Confidence intervals of
lmemodels:nlmestores one df value per coefficient (fixDF$X).get_predicted_ci()passed that vector toqt(), 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 newdata.get_predicted()with newdatafor models without apredict()method for new data (for exampleclmm): thefitted()fallback is only used when it has one value per data row. Otherwise the result isNULLwith the existing "Could not compute predictions" warning. Before,clmmreturned the fitted values of the model data.Tests are in
tests/testthat/test-get_modelmatrix.Rand a new file,tests/testthat/test-get_predicted-newdata.R, becausetest-get_predicted.Rskips 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.