*! version 1.0.0 25jun2024

r2_nakagawa -- Nakagawa's R-squared statistic
for multilevel mixed-effects linear regression

Syntax

r2_nakagawa

Description

r2_nakagawa
is a post-estimation command
(also see: estat)
that computes the R-squared statistic following mixed.
R-squared is computed as described by Nakagawa and Schielzeth (2013)
for random intercepts,
and extended to random slopes as described by Johnson (2014).

r2_nakagawa
computes the marginal R-squared,
which is concerned with variance explained by the fixed effects,
and the conditional R-squared,
which is concerned with variance explained by both fixed and random effects
(Nakagawa and Schielzeth 2013).

More specifically, the marginal R-squared is computed as:

Var_f / (Var_f + Var_re + Var_e)

and the conditional R-squared is computed as:

(Var_f + Var_re) / (Var_f + Var_re + Var_e)

where Var_f is the variance of the fixed effects, Var_re
is the variance of the random effects, and Var_e is the variance of the random effects residuals.

Examples

Setup

. webuse nlswork

Random-intercept model, analogous to xtreg

. mixed ln_w grade age c.age#c.age ttl_exp
tenure c.tenure#c.tenure || id:

. r2_nakagawa

Random-intercept and random-slope (coefficient) model, correlated random
effects

. mixed ln_w grade age c.age#c.age ttl_exp
tenure c.tenure#c.tenure || id: tenure,
cov(unstruct)

. r2_nakagawa

Setup

. webuse productivity

Three-level nested model, observations nested within
state nested within region, fit
by maximum likelihood

. mixed gsp private emp hwy water other unemp || region: ||
state:, mle

. r2_nakagawa

Three-level nested random intercepts and random slopes (coefficients)

. mixed gsp private emp hwy water other unemp || region:emp
|| state:unemp, mle

. r2_nakagawa

Two-way crossed random effects

. mixed gsp private emp hwy water other unemp || _all: R.region || _all: R.state, mle

. r2_nakagawa

Saved results

r2_nakagawa saves the following in r():

Scalars

r(Var_f) variance of the fixed effects

r(Var_re) variance of the random effects

r(Var_e) variance of the random effects residuals

r(r2_c) conditional R-squared

r(r2_m) marginal R-squared

References

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: 133–142.

Johnson, P. C. D. 2014. Extension of Nakagawa and Schielzeth's R2 GLMM to random slopes models.
Methods in Ecology and Evolution 5: 944–946.

Citation of r2_nakagawa

r2_nakagawa is not an official Stata command. It is a free contribution
to the research community, like a paper. Please cite it as such:

Klein, D., and A. Linden. 2024. r2_nakagawa: Stata module for computing Nakagawa's R-squared statistic for multilevel mixed-effects linear regression.

Authors

Daniel Klein

klein.daniel.81@gmail.com

Ariel Linden

President, Linden Consulting Group, LLC

alinden@lindenconsulting.org

Acknowledgments

We wish to thank John Moran for advocating that we write this package.