# Quantifying uncertainty around R-squared for generalized linear mixed models

August 22, 2013
By

(This article was first published on Ecology in silico, and kindly contributed to R-bloggers)

People love $R^2$. As such, when Nakagawa and Schielzeth published an article in the journal Methods in Ecology and Evolution earlier this year, ecologists (amid increasing use of generalized linear mixed models (GLMMs)) rejoiced. Now there’s an R function that automates $R^2$ calculations for GLMMs fit with the lme4 package.

$R^2$ is usually reported as a point estimate of the variance explained by a model, using the maximum likelihood estimates of the model parameters and ignoring uncertainty around these estimates. Nakagawa and Schielzeth (2013) noted that it may be desirable to quantify the uncertainty around $R^2$ using MCMC sampling. So, here we are.

### Background

$R^2$ quantifies the proportion of observed variance explained by a statistical model. When it is large (near 1), much of the variance in the data is explained by the model.

Nakagawa and Schielzeth (2013) present two $R^2$ statistics for generalized linear mixed models:

1) Marginal $R^2_{GLMM(m)}$, which represents the proportion of variance explained by fixed effects:

where $\sigma^2_f$ represents the variance in the fitted values (on a link scale) based on the fixed effects:

$\boldsymbol{X}$ is the design matrix of the fixed effects, and $\boldsymbol{\beta}$ is the vector of fixed effects estimates.

$\sum_{l=1}^{u}\sigma^2_l$ represents the sum the variance components for all of $u$ random effects. $\sigma^2_d$ is the distribution-specific variance (Nakagawa & Schielzeth 2010), and $\sigma^2_d$ represents added dispersion.

2) Conditional $R^2_{GLMM(c)}$ represents the proportion of variance explained by the fixed and random effects combined:

### Point-estimation of $R^2_{GLMM}$

Here, I’ll follow the example of an overdispersed Poisson GLMM provided in the supplement to Nakagawa & Schielzeth, available here. This is their most complicated example, and the simpler ones ought to be relatively straightforward for those that are interested in normal or binomial GLMMs.

Having simulated a dataset, calculate the $R^2$ point-estimates, using the lme4 package to fit the model.

Having stored our point estimates, we can now turn to Bayesian methods instead, and generate $R^2$ posteriors.

### Posterior uncertainty in $R^2_{GLMM}$

We need to fit two models in order to get the needed parameters for $R^2_{GLMM}$. First, a model that includes all random effects, but only an intercept fixed effect is fit to estimate the distribution specific variance $\sigma^2_d$. Second, we fit a model that includes all random and all fixed effects to estimate the remaining variance components.

First I’ll clean up the data that we’ll feed to JAGS:

Then, fitting the intercept model:

Then, fit the full mixed-model with all fixed and random effects:

For every MCMC draw, we can calculate $R^2_{GLMM}$, generating posteriors for both the marginal and conditional values.

This plot shows the posterior $R^2_{GLMM}$ distributions for both the marginal and conditional cases, with the point estimates generated with lmer shown as vertical blue lines. Personally, I find it to be a bit more informative and intuitive to think of $R^2$ as a probability distribution that integrates uncertainty in its component parameters. That said, it is unconventional to represent $R^2$ in this way, which could compromise the ease with which this handy statistic can be explained to the uninitiated (e.g. first year biology undergraduates). But, being a derived parameter, those wishing to generate a posterior can do so relatively easily.

### Aside: correspondence between parameter estimates

Some may be wondering whether the parameter estimates generated with lme4 are comparable to those generated using JAGS. Having used vague priors, we would expect them to be similar. We can plot the Bayesian credible intervals (in blue), with the previous point estimates (as open black circles):