I have a mixed model with three random effects and many fixed effects. I set aside some of my data to validate my final model and now I would like to compute the $R^2$ on the validation set, in R.

I found some code for a mixed model with one random effect but not for a model with more than one random. Does anyone know a package or some code to compute $R^2$ for mixed models with more than one random effect? If this isn't possible, does anyone have a good suggestion for comparing model fits?

  • 5
    $\begingroup$ The lme4 package in R can estimate mixed effects models with more than one random effect. Because a mixed effects model isn't exactly analogous to a linear model without random effects, there isn't an easy direct equivalent for $R^2$. However, this paper by Gelman and Pardoe might be useful to you: tandfonline.com/doi/abs/10.1198/004017005000000517 $\endgroup$
    – smillig
    Nov 16, 2012 at 10:57

1 Answer 1


One possible approach was suggested by Nakagawa and Schielzeth (2013) where $R^2$ is decomposed into variance explained by fixed effects ($R^2_m$) and random effects ($R^2_c$).

Let's say your model looks like the one below, where $\alpha$ and $\gamma$ are random effects:

$$y_{ijk} = \beta_0 + \sum^M_{m=1} \beta_m X_{mijk} + \alpha_j + \gamma_k + \epsilon_{ijk}$$

if so, you can define $R^2$ for fixed effects as:

$$R^2_m = \frac{\sigma^2_f}{\sigma^2_f + \sigma^2_\alpha + \sigma^2_\gamma + \sigma^2_\epsilon}$$


$$\sigma^2_f = var\left( \sum^M_{m=1} \beta_m X_{mijk} \right)$$

And $R^2$ for random effects as:

$$R^2_c = \frac{\sigma^2_f + \sigma^2_\alpha + \sigma^2_\gamma}{\sigma^2_f + \sigma^2_\alpha + \sigma^2_\gamma + \sigma^2_\epsilon}$$

And this approach could be extended to GLMM's by decomposing $\sigma^2_\epsilon$ into (Nakagawa and Schielzeth, 2013, p. 137):

(i) multiplicative dispersion ($\omega$), (ii) additive dispersion ($\sigma^2_e$) and (iii) distribution-specific variance ($\sigma^2_d$)

Johnson (2014) suggested how this approach could be used for random slopes models.

Functions for estimating $R^2_m$ and $R^2_c$ can be found in MuMIn package using r.squaredGLMM function.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.