R2 is a useful tool for determining how strong the relationship between two variables is. Unfortunately, the definition of R2 for mixed effects models is difficult – do you include the random variable or just the fixed effects? Including just the fixed effects is essentially a standard linear model, while including the random effects could confuse some readers (you have a much higher R2). So which do you report? Nakagawa and Schielzeth (2013) say both! They even provide the formulae for their calculation.

Over on the sample(ECOLOGY) blog, an R function has been written for lme and lmer models and reports both R2 based on just the fixed effects (marginal R2) and that incorporating the random effects (conditional R2). Simply go to the link, copy the code into the console, hit enter and give the function a list of your models.

e.g. (from the sample(ECOLOGY) page)

```Example
mod1=lmer(rnorm(100,5,10)~rnorm(100,20,100)+(1|rep(c("A","B"),50)))
mod2=lmer(rnorm(100,5,10)~rnorm(100,20,100)+rnorm(100,0.5,2)+(1|rep(c("A","B"),50)))
rsquared.lme(list(mod1,mod2))```

A couple of warnings for lmer users though:

1. you might have to tweak the code if you only have a single random effect for in lmer models. If you have multiple random effect levels or lme models, you should be fine
2. the function is currently written for “mer” class models from lmer – the newer development versions of lmer use the “merMod” class and do away with @ as a slot

UPDATE: The function name has been changed to rsquared.glmm(). It now also handles models with poisson and binomial models. I think that lmer issues have been solved now too.

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

From → R, Uncategorized

Great stuff! Using a growth model in my master thesis and this will do great to report results. What package includes the rsquared.lme function?

• Hi,
The function isnt actually included in a package yet at all. You have to go to the sample(ecology) link (https://jslefche.wordpress.com/2013/03/13/r2-for-linear-mixed-effects-models/), scroll down to the code section and copy it into your R console.
Incidentally, the name of the function has been changed to rsquared.glmm as it now also handles poisson or binomial errors. I’ll change the post to reflect this.

Hi. You mention tweaking the code if you have only a single random effect. This is what I have (lmer, poisson) and I wondered if you could be more specific about the problems/solution in this case. When I run the code, my marginal and conditional R2 are suspiciously similar and I wonder whether there is a problem because of the type of model I am evaluating.

• Hi James,

I think they have since fixed the function so that it can cope with single random effects. If you got some output then it worked.
If your R2s are similar it probably means that your random effect isnt explaining much of the variation in the data.
You could set up two nested models using lme and gls (nlme package), one with the random effect (lme) and the second without it (gls) and compare the models with anova(model_without, model_with). I say to use nlme functions as lmer (and lme) doesnt run without random effects, but I dont know if there is an equivalent to gls for lmer. That might tell you if your random effect is worth keeping (statistically). If your experimental design requires the random effect then you can discuss whether to keep it or not…

Hope that helps!

Hi, thanks for your post. I would be very grateful for any advice – I keep receiving NA’s for both the marginal and conditional R2 when I apply the r.squaredGLMM function to my models. I have a Poisson distribution so I thought that I was receiving NA’s due to this – but from your post it seems like that shouldn’t matter. Similarly I only have one random effect – but again it seems this has been fixed. Do you have any ideas why I am still receiving NA’s? Many thanks, Beth

• Hi Beth,

To be honest I don’t know. I would be inclined to check out the comments on the sample(ECOLOGY) post. I get the impression that it’s been under relatively constant development for the last 2 years. Are you using the version of the function from the blog page or the github version? If you’re not already, its probably worth trying the github version. Maybe the blog version is outdated.

Alternatively, are you using a supported model class?

HTH

Alan

PS I don’t receive any warnings