Learn R Programming

sjstats (version 0.2.0)

r2: Compute R-squared of (generalized) linear (mixed) models

Description

Compute R-squared values of linear (mixed) models, or pseudo-R-squared values for generalized linear (mixed) models.

Usage

r2(x, n = NULL)

Arguments

x
Fitted model of class lm, glm, lmerMod/lme or glmerMod.
n
Optional, a lmerMod object, representing the fitted null-model to x (unconditional model). If n is given, the pseudo-r-squared for random intercept and random slope variances are computed (see Kwok et al. 2008; see 'Examples' and 'Details').

Value

  • 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.

Details

If n is given, the Pseudo-R2 statistic is the proportion of explained variance in the random effect after adding co-variates or predictors to the model, or in short: the proportion of the explained variance in the random effect of the full (conditional) model x compared to the null (unconditional) model n.

References

  • DRAFT r-sig-mixed-models FAQ
  • Byrnes, J. 2008. Re: Coefficient of determination (R^2) when using lme(). gmane.comp.lang.r.lme4.devel
  • 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–86 (\Sexpr[results=rd,stage=build]{tools:::Rd_expr_doi("#1")}10.1037/a0012765http://doi.org/10.1037/a0012765doi:\ifelse{latex}{\out{~}}{ }latex~ 10.1037/a0012765 )
  • Xu, R. 2003. Measuring explained variation in linear mixed effects models. Statist. Med. 22:3527-3541. \Sexpr[results=rd,stage=build]{tools:::Rd_expr_doi("#1")}10.1002/sim.1572http://doi.org/10.1002/sim.1572doi:\ifelse{latex}{\out{~}}{ }latex~ 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

Examples

Run this code
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