Skip to content

get_variance(): include uncorrelated random-slope variances in var.random for lme4 models - #1263

Merged
strengejacke merged 3 commits into
easystats:mainfrom
jmgirard:hotfix-uncorrelated-slopes
Oct 11, 2026
Merged

strengejacke merged 3 commits into
easystats:mainfrom
jmgirard:hotfix-uncorrelated-slopes

Conversation

@jmgirard

Copy link
Copy Markdown
Contributor

get_variance() left the random-slope variance out of var.random for lme4 models with uncorrelated random effects.

library(lme4)
m <- lmer(Reaction ~ Days + (Days || Subject), data = sleepstudy)
insight::get_variance(m)$var.random
# before: 627.57 (random-intercept variance only)
# after: 1649.53 = vc[1] + vc[2] * mean(Days^2)

The correlated model (Days | Subject) gives 1698.08 for comparison.

Cause: lme4 splits (x || g) into several VarCorr() elements ("g", "g.1") and names nested terms with make.names() ("subgrp.grp"), but ranef() returns one element, "g" or "subgrp:grp". var.random selected the VarCorr() elements by the ranef() names. So it dropped the slope elements, and for nested terms such as (1 + Days || grp / subgrp) it gave an error inside .safe() and returned NULL. The new helper .vc_random_terms() maps each VarCorr() element to its ranef() term.

The fix for #304 (2021) corrected var.intercept and var.slope but not var.random. The test get_variance-7 recorded the old value 627.57, so this PR changes that expectation. A new test compares var.random with values computed by hand from VarCorr() for the plain, centered and nested cases.

This changes performance::r2_nakagawa() and performance::icc() for these models (easystats/performance#428).

…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.

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.

🟢 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 to ranef() 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.

@strengejacke
strengejacke merged commit 28b0314 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