Skip to content

Mixed model R2 (UPDATED)

March 28, 2013

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)


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

  1. Bruno Costa permalink

    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?

  2. James Wilson permalink

    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!

  3. Beth permalink

    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?



  4. Beth permalink

    PS I don’t receive any warnings

  5. Liam permalink

    I’m trying to use this function to find R2 for my Mixed Effect model but it keeps giving me the error:

    fitting model with the observation-level random effect term failed. Add the term manually
    In addition: Warning message:
    In value[[3L]](cond) : (p <- ncol(X)) == ncol(Y) is not TRUE

    Any ideas what to do?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: