Compute R-squared values of linear (mixed) models, or
pseudo-R-squared values for generalized linear (mixed) models, or a
Bayesian version of R-squared for regression models for stanreg
and brmsfit
objects.
r2(x, n = NULL, loo = FALSE)
Fitted model of class lm
, glm
, lmerMod
,
lme
, glmerMod
, stanreg
or brmsfit
.
Optional, a lmerMod
object, representing the fitted null-model
(unconditional model) to x
. If n
is given, the pseudo-r-squared
for random intercept and random slope variances are computed
(Kwok et al. 2008) as well as the Omega squared value
(Xu 2003). See 'Examples' and 'Details'.
Logical, if TRUE
and x
is a stanreg
or
brmsfit
object, a LOO-adjusted r-squared is calculated. Else,
a rather "unadjusted" r-squared will be returned by calling
rstantools::bayes_R2()
.
For linear models, the r-squared and adjusted r-squared values.
For linear mixed models, the r-squared and Omega-squared values.
For glm
objects, Cox & Snell's and Nagelkerke's pseudo r-squared values.
For glmerMod
objects, Tjur's coefficient of determination.
For brmsfit
or stanreg
objects, the Bayesian version of r-squared is computed, calling rstantools::bayes_R2()
.
If loo = TRUE
, for brmsfit
or stanreg
objects a LOO-adjusted version of r-squared is returned.
For linear models, the r-squared and adjusted r-squared value is returned,
as provided by the summary
-function.
For linear mixed models, an r-squared approximation by computing the
correlation between the fitted and observed values, as suggested by
Byrnes (2008), is returned as well as a simplified version of
the Omega-squared value (1 - (residual variance / response variance),
Xu (2003), Nakagawa, Schielzeth 2013), unless n
is specified.
If n
is given, for linear mixed models pseudo r-squared measures based
on the variances of random intercept (tau 00, between-group-variance)
and random slope (tau 11, random-slope-variance), as well as the
r-squared statistics as proposed by Snijders and Bosker 2012 and
the Omega-squared value (1 - (residual variance full model / residual
variance null model)) as suggested by Xu (2003) are returned.
For generalized linear models, Cox & Snell's and Nagelkerke's pseudo r-squared values are returned.
For generalized linear mixed models, the coefficient of determination
as suggested by Tjur (2009) (see also cod
). Note
that Tjur's D is restricted to models with binary response.
The ("unadjusted") r-squared value and its standard error for
brmsfit
or stanreg
objects are robust measures, i.e.
the median is used to compute r-squared, and the median absolute
deviation as the measure of variability. If loo = TRUE
,
a LOO-adjusted r-squared is calculated, which comes conceptionally
closer to an adjusted r-squared measure.
More ways to compute coefficients of determination are shown
in this great GLMM faq.
Furthermore, see r.squaredGLMM
or
rsquared
for conditional and marginal
r-squared values for GLMM's.
Bolker B et al. (2017): GLMM FAQ.
Byrnes, J. 2008. Re: Coefficient of determination (R^2) when using lme() (https://stat.ethz.ch/pipermail/r-sig-mixed-models/2008q2/000713.html)
Kwok OM, Underhill AT, Berry JW, Luo W, Elliott TR, Yoon M. 2008. Analyzing Longitudinal Data with Multilevel Models: An Example with Individuals Living with Lower Extremity Intra-Articular Fractures. Rehabilitation Psychology 53(3): 370<U+2013>86. 10.1037/a0012765
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<U+2013>142. 10.1111/j.2041-210x.2012.00261.x
Rabe-Hesketh S, Skrondal A. 2012. Multilevel and longitudinal modeling using Stata. 3rd ed. College Station, Tex: Stata Press Publication
Raudenbush SW, Bryk AS. 2002. Hierarchical linear models: applications and data analysis methods. 2nd ed. Thousand Oaks: Sage Publications
Snijders TAB, Bosker RJ. 2012. Multilevel analysis: an introduction to basic and advanced multilevel modeling. 2nd ed. Los Angeles: Sage
Xu, R. 2003. Measuring explained variation in linear mixed effects models. Statist. Med. 22:3527-3541. 10.1002/sim.1572
Tjur T. 2009. Coefficients of determination in logistic regression models - a new proposal: The coefficient of discrimination. The American Statistician, 63(4): 366-372
rmse
for more methods to assess model quality.
# NOT RUN {
library(sjmisc)
library(lme4)
fit <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
r2(fit)
data(efc)
fit <- lm(barthtot ~ c160age + c12hour, data = efc)
r2(fit)
# Pseudo-R-squared values
efc$services <- ifelse(efc$tot_sc_e > 0, 1, 0)
fit <- glm(services ~ neg_c_7 + c161sex + e42dep,
data = efc, family = binomial(link = "logit"))
r2(fit)
# Pseudo-R-squared values for random effect variances
fit <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
fit.null <- lmer(Reaction ~ 1 + (Days | Subject), sleepstudy)
r2(fit, fit.null)
# }
Run the code above in your browser using DataLab