Repository navigation
get_variance(): include uncorrelated random-slope variances in var.random for lme4 models - #1263
Merged
strengejacke merged 3 commits intoOct 11, 2026
Merged
Conversation
…ndom for lme4 models
lme4 splits (x || g) into several VarCorr() elements ("g", "g.1") and names nested terms with make.names() ("subgrp.grp"), while ranef() returns one element "g" or "subgrp:grp". var.random selected VarCorr() elements by the ranef() names, so it dropped the slope elements, and for nested terms it errored and returned NULL.
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No unresolved review issues were identified.
0 open findings
What changed in this PR
Fixes get_variance() for lme4 models so uncorrelated random-slope variances are included in var.random, including nested terms.
Changes:
- Maps
VarCorr()components toranef()terms. - Adds regression coverage.
- Updates NEWS and the development version.
| File | Description |
|---|---|
tests/testthat/test-get_variance.R |
Adds regression coverage and updates expectations. |
R/compute_variances.R |
Maps split and nested variance components correctly. |
NEWS.md |
Documents the behavioral change. |
DESCRIPTION |
Bumps the development version. |
🧠 Review effort: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
get_variance()left the random-slope variance out ofvar.randomfor lme4 models with uncorrelated random effects.The correlated model
(Days | Subject)gives 1698.08 for comparison.Cause: lme4 splits
(x || g)into severalVarCorr()elements ("g","g.1") and names nested terms withmake.names()("subgrp.grp"), butranef()returns one element,"g"or"subgrp:grp".var.randomselected theVarCorr()elements by theranef()names. So it dropped the slope elements, and for nested terms such as(1 + Days || grp / subgrp)it gave an error inside.safe()and returnedNULL. The new helper.vc_random_terms()maps eachVarCorr()element to itsranef()term.The fix for #304 (2021) corrected
var.interceptandvar.slopebut notvar.random. The testget_variance-7recorded the old value 627.57, so this PR changes that expectation. A new test comparesvar.randomwith values computed by hand fromVarCorr()for the plain, centered and nested cases.This changes
performance::r2_nakagawa()andperformance::icc()for these models (easystats/performance#428).