6

Let say I've ran this linear regression:

lm_mtcars <- lm(mpg ~ wt + vs, mtcars)

I can use anova() to see the amount of variance in the dependent variable accounted for by the two predictors:

anova(lm_mtcars)

Analysis of Variance Table

Response: mpg
          Df Sum Sq Mean Sq  F value    Pr(>F)    
wt         1 847.73  847.73 109.7042 2.284e-11 ***
vs         1  54.23   54.23   7.0177   0.01293 *  
Residuals 29 224.09    7.73                       
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Lets say I now add a random intercept for cyl:

library(lme4)
lmer_mtcars <- lmer(mpg ~ wt + vs + (1 | cyl), mtcars)
summary(lmer_mtcars)

Linear mixed model fit by REML ['lmerMod']
Formula: mpg ~ wt + vs + (1 | cyl)
   Data: mtcars

REML criterion at convergence: 148.8

Scaled residuals: 
     Min       1Q   Median       3Q      Max 
-1.67088 -0.68589 -0.08363  0.48294  2.16959 

Random effects:
 Groups   Name        Variance Std.Dev.
 cyl      (Intercept) 3.624    1.904   
 Residual             6.784    2.605   
Number of obs: 32, groups:  cyl, 3

Fixed effects:
            Estimate Std. Error t value
(Intercept)  31.4788     2.6007  12.104
wt           -3.8054     0.6989  -5.445
vs            1.9500     1.4315   1.362

Correlation of Fixed Effects:
   (Intr) wt    
wt -0.846       
vs -0.272  0.006

The variance accounted for by each fixed effect now drops because the random intercept for cyl is now accounting for some of the variance in mpg:

anova(lmer_mtcars)

Analysis of Variance Table
   Df  Sum Sq Mean Sq F value
wt  1 201.707 201.707 29.7345
vs  1  12.587  12.587  1.8555

But in lmer_mtcars, how can I tell what proportion of the variance is being accounted for by wt, vs and the random intecept for cyl?

luciano
  • 12,197
  • 30
  • 87
  • 119

2 Answers2

5

You can use MuMIn package and its r.squaredGLMM() function which will give you 2 approximated r-squared values based on Nakagawa & Schielzeth (2012) and Johnson (2014):

Marginal R^2 is the proportion of variance explained by the fixed effects alone.

Conditional R^2 is the proportion of variance explained by the fixed and random effects jointly.

Kuba Krukar
  • 114
  • 9
  • Maybe useful to also have a look at the discussion here which seems to suggest that the use of Marginal R^2 could be misleading in LMER and itself per se is not the same as "variance explained": https://stats.stackexchange.com/questions/92221/is-it-worth-reporting-small-fixed-effect-r2-marginal-r2-large-model-r – Chloe Jun 02 '21 at 03:47
2

"Variance explained" by the model is described by $R^2$ statistic. For LMM's Nakagawa and Schielzeth (2013) suggested that $R^2$ could be decomposed into variance explained by fixed effects ($R^2_m$) and random effects ($R^2_c$) (see here). In this approach, variance explained by the fixed effects is defined as:

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

where:

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

where $X_1,...,X_m$ are $M$ independent variables, $\sigma^2_\alpha$ is variance of random effect and $\sigma^2_\varepsilon$ is residual variance. So it gives you a part of what you are interested in. However, you have to remember that $R^2$ is not a perfect measure, e.g. this question: Is $R^2$ useful or dangerous?.

You could also find this tutorial on ANOVA and lme4 helpful.

Tim
  • 108,699
  • 20
  • 212
  • 390