---
title: "Adjusted R² for generalized linear mixed models"
author: "Dongwen Luo"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Adjusted R² for generalized linear mixed models}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", message = FALSE, warning = FALSE)
```

`R2_model()` gives an adjusted coefficient of determination (R²) for linear and
generalized linear mixed models by the method of Piepho (2023): the total R² and its
fixed and random shares, from one fit. For a GLMM the residual variance on the
link scale is chosen from the family and link. It replaces `R2_glmm()` of
predictmeans 1.x, with the same values given as numbers.

```{r}
library(predictmeans)
data(Body_length)
fm <- lmer(BodyL ~ Treatment + Habitat + Sex + (1 | Population/Container),
           data = Body_length)
R2_model(fm)
```

`over_disp = TRUE` counts an observation-level random effect (one level per
observation) as residual variance. `method = "nakagawa"` gives the R² of Nakagawa
and Schielzeth (2013).

The method, and all five examples of Piepho (2023) checked against the paper
(Poisson, binary and binomial GLMMs with three links, clinic- and patient-level
data, an ordinal model), are in the article
[Adjusted R² for GLMMs](https://dongwenluo.github.io/predictmeans/articles/R2-GLMM.html).

## References

Piepho, H.-P. (2023). An adjusted coefficient of determination (R²) for generalized
linear mixed models in one go. *Biometrical Journal*, 65(7), e2200290.
<https://doi.org/10.1002/bimj.202200290>

Nakagawa, S. and Schielzeth, H. (2013). A general and simple method for obtaining R²
from generalized linear mixed-effects models. *Methods in Ecology and Evolution*, 4,
133–142.
