Returns the R squared values according to the model class.
r2(model) # S3 method for default r2(model) # S3 method for lm r2(model) # S3 method for lmerMod r2(model)
model | An object containing a model. |
---|
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 computations.
$$R^2 = \frac{var(\hat{y})}{var(\epsilon)}$$ Where \(var(\hat{y})\) is the variance explained by the model and \(var(\epsilon)\) is the residual variance.
$$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.
$$R_{marg}^{2} = \frac{var(f)}{var(f) + var(r) + var(\epsilon)}$$ Where \(var(f)\) is the variance of the fixed effects, \(var(r)\) is the variance of the random effects and \(var(\epsilon)\) is the residual variance.
$$R_{cond}^{2} = \frac{var(f) + var(r)}{var(f) + var(r) + var(\epsilon)}$$
Nakagawa, S., & 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 .
#> R2 R2_adj #> 1 0.6187057 0.6135181if (require(lme4, quietly = TRUE)) { m2 <- lmer( Sepal.Length ~ Sepal.Width + Petal.Length + (1 | Species), data = iris ) r2(m2) }#> R2_marg R2_cond #> 1 0.7437646 0.9541472