Adjusted R² for generalized linear mixed models

R2_model() computes an adjusted coefficient of determination (R²) for linear and generalized linear mixed models with Piepho’s (2023) method (R2_glmm() before 2.0, which printed the same values as percentages). It works for lmer, glmer and glmmTMB fits and picks the residual (distribution-specific) variance for the model’s family and link automatically.

library(predictmeans)
args(R2_model)
#> function (model, method = c("piepho", "nakagawa"), over_disp = FALSE) 
#> NULL

With over_disp = TRUE, an observation-level random effect (one level per observation) is treated as overdispersion and counted as residual variance.

Example 1: body length (linear mixed model)

data(Body_length)
str(Body_length)
#> 'data.frame':    960 obs. of  6 variables:
#>  $ Population: Factor w/ 12 levels "1","10","11",..: 1 1 1 1 1 1 1 1 1 1 ...
#>  $ Container : Factor w/ 120 levels "1","10","100",..: 1 1 1 1 1 1 1 1 33 33 ...
#>  $ Sex       : Factor w/ 2 levels "Female","Male": 1 1 1 1 1 1 1 1 2 2 ...
#>  $ Habitat   : Factor w/ 2 levels "A","B": 1 1 1 1 2 2 2 2 1 1 ...
#>  $ Treatment : Factor w/ 2 levels "Cont","Exp": 1 2 1 2 1 2 1 2 1 2 ...
#>  $ BodyL     : num  10.8 11.2 11.6 15.6 12.4 ...
mod_BodyL <- lmer(BodyL ~ Treatment + Habitat + Sex + (1|Population/Container),
                  data = Body_length)
R2_model(mod_BodyL)
#> # Adjusted R2 for mixed models (Piepho)
#>                Total                Fixed               Random 
#>               73.39%               40.09%               33.30% 
#> Container:Population           Population 
#>                5.18%               28.13%

Example 2: egg counts (Poisson GLMM with overdispersion)

Each female has her own level of individual within Container and Population, so the innermost random effect is observation-level and models overdispersion.

data(Poisson_ind)
mod_poisson <- glmer(Egg ~ Treatment + Habitat + (1|Population/Container/individual),
                     family = "poisson", data = Poisson_ind)
R2_model(mod_poisson, over_disp = TRUE)
#> # Adjusted R2 for mixed models (Piepho)
#>  Total  Fixed Random 
#> 50.93%  9.13% 41.79% 
#> 
#> Advice:
#> i For a non-gaussian model the R2 is on the link (latent) scale: not
#>   comparable with an lm R2 or across families and links; use it to compare
#>   models of the same family.

References

Piepho, H.-P. (2023). An adjusted coefficient of determination (R²) for generalized linear mixed models in one go. Biometrical Journal, 65(7), e2200290. https://doi.org/10.1002/bimj.202200290

Nakagawa, S. and Schielzeth, H. (2013). A general and simple method for obtaining R² from generalized linear mixed-effects models. Methods in Ecology and Evolution, 4, 133–142.

Beitler, P. J. and Landis, J. R. (1985). A mixed-effects model for categorical data. Biometrics, 41, 991–1000.