Compute the *marginal* and *conditional* r-squared value for
mixed effects models with complex random effects structures.

## Usage

```
r2_nakagawa(
model,
by_group = FALSE,
tolerance = 1e-05,
ci = NULL,
iterations = 100,
ci_method = NULL,
verbose = TRUE,
...
)
```

## Arguments

- model
A mixed effects model.

- by_group
Logical, if

`TRUE`

, returns the explained variance at different levels (if there are multiple levels). This is essentially similar to the variance reduction approach by*Hox (2010), pp. 69-78*.- tolerance
Tolerance for singularity check of random effects, to decide whether to compute random effect variances for the conditional r-squared or not. Indicates up to which value the convergence result is accepted. When

`r2_nakagawa()`

returns a warning, stating that random effect variances can't be computed (and thus, the conditional r-squared is`NA`

), decrease the tolerance-level. See also`check_singularity()`

.- ci
Confidence resp. credible interval level. For

`icc()`

and`r2()`

, confidence intervals are based on bootstrapped samples from the ICC resp. R2 value. See`iterations`

.- iterations
Number of bootstrap-replicates when computing confidence intervals for the ICC or R2.

- ci_method
Character string, indicating the bootstrap-method. Should be

`NULL`

(default), in which case`lme4::bootMer()`

is used for bootstrapped confidence intervals. However, if bootstrapped intervals cannot be calculated this was, try`ci_method = "boot"`

, which falls back to`boot::boot()`

. This may successfully return bootstrapped confidence intervals, but bootstrapped samples may not be appropriate for the multilevel structure of the model. There is also an option`ci_method = "analytical"`

, which tries to calculate analytical confidence assuming a chi-squared distribution. However, these intervals are rather inaccurate and often too narrow. It is recommended to calculate bootstrapped confidence intervals for mixed models.- verbose
Toggle warnings and messages.

- ...
Arguments passed down to

`brms::posterior_predict()`

.

## Details

Marginal and conditional r-squared values for mixed models are calculated
based on *Nakagawa et al. (2017)*. For more details on the computation of
the variances, see `?insight::get_variance`

. The random effect variances are
actually the mean random effect variances, thus the r-squared value is also
appropriate for mixed models with random slopes or nested random effects
(see *Johnson, 2014*).

**Conditional R2**: takes both the fixed and random effects into account.**Marginal R2**: considers only the variance of the fixed effects.

The contribution of random effects can be deduced by subtracting the
marginal R2 from the conditional R2 or by computing the `icc()`

.

## References

Hox, J. J. (2010). Multilevel analysis: techniques and applications (2nd ed). New York: Routledge.

Johnson, P. C. D. (2014). Extension of Nakagawa and Schielzeth’s R2 GLMM to random slopes models. Methods in Ecology and Evolution, 5(9), 944–946. doi:10.1111/2041-210X.12225

Nakagawa, S., and Schielzeth, H. (2013). A general and simple method for obtaining R2 from generalized linear mixed-effects models. Methods in Ecology and Evolution, 4(2), 133–142. doi:10.1111/j.2041-210x.2012.00261.x

Nakagawa, S., Johnson, P. C. D., and Schielzeth, H. (2017). The coefficient of determination R2 and intra-class correlation coefficient from generalized linear mixed-effects models revisited and expanded. Journal of The Royal Society Interface, 14(134), 20170213.