r2: Compute R squared

Description Usage Arguments Details Value R squared Adjusted R squared Marginal R squared Conditional R squared References Examples

View source: R/r2.R

Description

Returns the R squared values according to the model class.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
r2(model)

## Default S3 method:
r2(model)

## S3 method for class 'lm'
r2(model)

## S3 method for class 'lmerMod'
r2(model)

Arguments

model

An object containing a model.

Details

R squared computations.

Value

If the model is a linear model, it returns a data.frame with the R squared and adjusted R squared values. If the model is a linear mixed model it return a data.frame with the marginal and conditional R squared values as described by Nakagawa and Schielzeth (2013). See the formulas for the computations in "Details".

R squared

R^2 = \frac{var(\hat{y})}{var(ε)}

Where var(\hat{y}) is the variance explained by the model and var(ε) is the residual variance.

Adjusted R squared

R_{adj}^{2} = 1 - (1 - R^2)\frac{n - 1}{n - p - 1}

Where n is the number of data points and p is the number of predictors in the model.

Marginal R squared

R_{marg}^{2} = \frac{var(f)}{var(f) + var(r) + var(ε)}

Where var(f) is the variance of the fixed effects, var(r) is the variance of the random effects and var(ε) is the residual variance.

Conditional R squared

R_{cond}^{2} = \frac{var(f) + var(r)}{var(f) + var(r) + var(ε)}

References

Examples

1
2
3
4
5
6
7
8
m1 <- lm(Sepal.Length ~ Species, data = iris)
r2(m1)
if (require(lme4, quietly = TRUE)) {
  m2 <- lmer(
    Sepal.Length ~ Sepal.Width + Petal.Length + (1 | Species), data = iris
  )
  r2(m2)
}

lvmisc documentation built on April 5, 2021, 5:06 p.m.