Title: Partial Eta-Squared for Crossed and Nested Linear Mixed Models
Version: 0.1.2
Description: Computes partial eta-squared effect sizes for fixed effects in linear mixed models fitted with the 'lme4' package. Supports crossed and nested random effects structures with any number of grouping factors. Random slope variances are translated to the outcome scale using a variance decomposition approach, correctly accounting for predictor scaling and interaction terms. Both general and operative effect sizes are provided. Methods are based on Correll, Mellinger, McClelland, and Judd (2020) <doi:10.1016/j.tics.2019.12.009>, Correll, Mellinger, and Pedersen (2022) <doi:10.3758/s13428-021-01687-2>, and Rights and Sterba (2019) <doi:10.1037/met0000184>.
License: MIT + file LICENSE
URL: https://github.com/bcohen0901/pecanr
BugReports: https://github.com/bcohen0901/pecanr/issues
Encoding: UTF-8
RoxygenNote: 7.3.3
Imports: lme4
Suggests: spelling, testthat (≥ 3.0.0)
Config/testthat/edition: 3
Language: en-US
NeedsCompilation: no
Packaged: 2026-03-04 00:51:44 UTC; brandoncohen
Author: Brandon Cohen ORCID iD [aut, cre], Joshua Correll ORCID iD [aut, ths]
Maintainer: Brandon Cohen <brandon.cohen-1@colorado.edu>
Repository: CRAN
Date/Publication: 2026-03-17 18:00:08 UTC

pecanr: Partial Eta-Squared for Crossed and Nested Linear Mixed Models

Description

Computes partial eta-squared effect sizes for fixed effects in linear mixed models fitted with the 'lme4' package. Supports crossed and nested random effects structures with any number of grouping factors. Random slope variances are translated to the outcome scale using a variance decomposition approach, correctly accounting for predictor scaling and interaction terms. Both general and operative effect sizes are provided. Methods are based on Correll, Mellinger, McClelland, and Judd (2020) doi:10.1016/j.tics.2019.12.009, Correll, Mellinger, and Pedersen (2022) doi:10.3758/s13428-021-01687-2, and Rights and Sterba (2019) doi:10.1037/met0000184.

Author(s)

Maintainer: Brandon Cohen brandon.cohen-1@colorado.edu (ORCID)

Authors:

See Also

Useful links:


Batch Calculate Partial Eta-Squared for Multiple Effects

Description

Calculates partial eta-squared for all fixed effects in a model.

Usage

batch_eta2p(
  model,
  data,
  design = c("crossed", "nested"),
  subj_var = NULL,
  item_var = NULL,
  cross_vars = NULL,
  nest_vars = NULL,
  operative = FALSE,
  verbose = FALSE
)

Arguments

model

A fitted model object from lme4::lmer().

data

Data frame used to fit the model.

design

Character string: either "crossed" or "nested".

subj_var

Character string specifying the subject/participant grouping variable. Retained for backward compatibility; prefer cross_vars.

item_var

Character string specifying the item/stimulus grouping variable. Retained for backward compatibility; prefer cross_vars.

cross_vars

Character vector of ALL crossed grouping variable names (e.g., c("subject", "item", "rater")). Supersedes subj_var and item_var when provided. Supports any number of crossed factors.

nest_vars

Character vector specifying nesting variables from lowest to highest level (required for nested designs).

operative

Logical. If TRUE, calculates operative effect size (excludes variance components that don't contribute to SE). Default is FALSE.

verbose

Logical. If TRUE, prints detailed results. Default is TRUE.

Value

A data frame with one row per effect containing eta-squared values and variance components.


Partial Eta-Squared for Linear Mixed Models

Description

Calculates partial eta-squared effect sizes for fixed effects in linear mixed models with crossed or nested random effects.

Usage

eta2p(
  model,
  effect,
  data,
  design = c("crossed", "nested"),
  subj_var = NULL,
  item_var = NULL,
  cross_vars = NULL,
  nest_vars = NULL,
  effect_level = NULL,
  var_x = NULL,
  operative = FALSE,
  verbose = TRUE
)

Arguments

model

A fitted model object from lme4::lmer().

effect

Character string specifying the fixed effect to analyze. Must match a name in fixef(model).

data

Data frame used to fit the model.

design

Character string: either "crossed" or "nested".

subj_var

Character string specifying the subject/participant grouping variable. Retained for backward compatibility; prefer cross_vars.

item_var

Character string specifying the item/stimulus grouping variable. Retained for backward compatibility; prefer cross_vars.

cross_vars

Character vector of ALL crossed grouping variable names (e.g., c("subject", "item", "rater")). Supersedes subj_var and item_var when provided. Supports any number of crossed factors.

nest_vars

Character vector specifying nesting variables from lowest to highest level (required for nested designs).

effect_level

Character string specifying the level at which the effect varies (e.g., "L1", "L2"). If NULL, will be detected automatically.

var_x

Optional numeric. Pre-computed variance of the predictor (or interaction product). If supplied, overrides the internal var() calculation from data. Useful when raw data are unavailable but the predictor variance is known (e.g., from design: a +/-1 binary predictor has var_x = 1).

operative

Logical. If TRUE, calculates operative effect size (excludes variance components that don't contribute to SE). Default is FALSE.

verbose

Logical. If TRUE, prints detailed results. Default is TRUE.

Details

The function implements a variance decomposition approach for computing partial eta-squared in mixed models. Random slope variances are translated to the outcome scale using the formula:

\sigma^2_{slope}(Y) = \sigma^2_b \times \sigma^2_X

For interaction effects, the variance of the predictor is calculated as the variance of the actual product term (e.g., var(X1 * X2)), not the product of individual variances. This correctly accounts for centering, scaling, and correlation between predictors. The var_x argument allows bypassing this computation when the variance is known a priori.

For general effect sizes (default), all variance components are included in the denominator. For operative effect sizes (operative = TRUE), only variance components that contribute to the standard error of the effect are included.

Crossed designs support any number of grouping factors via cross_vars. The two-argument form (subj_var + item_var) is retained for backward compatibility and is equivalent to cross_vars = c(subj_var, item_var).

Value

An object of class "eta2p_lmm" containing:

eta2p

Partial eta-squared value

variance_effect

Variance explained by the effect

variance_error

Error variance

effect

Name of the effect

design

Design type ("crossed" or "nested")

operative

Whether operative effect size was calculated

variance_components

List of individual variance components

...

Additional design-specific information

References

Correll, J., Mellinger, C., McClelland, G. H., & Judd, C. M. (2020). Avoid Cohen's 'Small', 'Medium', and 'Large' for Power Analysis. Trends in Cognitive Sciences, 24(3), 200-207. doi:10.1016/j.tics.2019.12.009

Correll, J., Mellinger, C., & Pedersen, E. J. (2022). Flexible approaches for estimating partial eta squared in mixed-effects models with crossed random factors. Behavior Research Methods, 54, 1626-1642. doi:10.3758/s13428-021-01687-2

Rights, J. D., & Sterba, S. K. (2019). Quantifying explained variance in multilevel models: An integrative framework for defining R-squared measures. Psychological Methods, 24(3), 309-338. doi:10.1037/met0000184

Examples


library(lme4)

# --- Two crossed factors (backward-compatible call) ---
set.seed(42)
crossed_data <- data.frame(
  y         = rnorm(120),
  condition = rep(c(-0.5, 0.5), 60),
  subject   = factor(rep(1:20, each = 6)),
  item      = factor(rep(1:6, 20))
)
model <- lmer(y ~ condition + (1 | subject) + (1 | item),
              data = crossed_data)
eta2p(model, "condition", crossed_data,
      design   = "crossed",
      subj_var = "subject",
      item_var = "item")

# --- Three crossed factors using cross_vars ---
set.seed(42)
three_way_data <- data.frame(
  y         = rnorm(180),
  condition = rep(c(-0.5, 0.5), 90),
  subject   = factor(rep(1:20, each = 9)),
  item      = factor(rep(rep(1:6, each = 3), 10)),
  rater     = factor(rep(1:3, 60))
)
model3 <- lmer(y ~ condition + (1 | subject) + (1 | item) + (1 | rater),
               data = three_way_data)
eta2p(model3, "condition", three_way_data,
      design     = "crossed",
      cross_vars = c("subject", "item", "rater"))

# --- Supply predictor variance directly (var_x) ---
eta2p(model, "condition", crossed_data,
      design     = "crossed",
      cross_vars = c("subject", "item"),
      var_x      = 1)   # +/-1 binary predictor: var = 1 by design