r2: Compute the model's R2

Description Usage Arguments Value Note See Also Examples

View source: R/r2.R

Description

Calculate the R2, also known as the coefficient of determination, value for different model objects. Depending on the model, R2, pseudo-R2, or marginal / adjusted R2 values are returned.

Usage

1
2
3
4
5
6
7
r2(model, ...)

## Default S3 method:
r2(model, ci = NULL, ci_method = "analytical", verbose = TRUE, ...)

## S3 method for class 'merMod'
r2(model, tolerance = 1e-05, ...)

Arguments

model

A statistical model.

...

Arguments passed down to the related r2-methods.

ci

Confidence Interval (CI) level. Default is NULL. Confidence intervals for R2 can be calculated based on different methods, see ci_method.

ci_method

Method for constructing the R2 confidence interval. Options are "analytical" for sampling-theory-based frequentist intervals and "bootstrap" for bootstrap intervals. Analytical intervals are not available for all models. For Bayesian models, r2_bayes() is used.

verbose

Logical. Should details about R2 and CI methods be given (TRUE) or not (FALSE)?

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().

Value

Returns a list containing values related to the most appropriate R2 for the given model (or NULL if no R2 could be extracted). See the list below:

Note

If there is no r2()-method defined for the given model class, r2() tries to return a "generic r2 value, calculated as following: 1-sum((y-y_hat)^2)/sum((y-y_bar)^2))

See Also

r2_bayes(), r2_coxsnell(), r2_kullback(), r2_loo(), r2_mcfadden(), r2_nagelkerke(), r2_nakagawa(), r2_tjur(), r2_xu() and r2_zeroinflated().

Examples

1
2
3
4
5
6
7
model <- glm(vs ~ wt + mpg, data = mtcars, family = "binomial")
r2(model)

if (require("lme4")) {
  model <- lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris)
  r2(model)
}

Example output

$R2_Tjur
Tjur's R2 
0.4776926 

Loading required package: lme4
Loading required package: Matrix
# R2 for mixed models

  Conditional R2: 0.969
     Marginal R2: 0.658

performance documentation built on Oct. 1, 2021, 5:08 p.m.