Returns (pseudo)-R^2 values for all linear, generalized linear, and generalized linear mixed effects models.
a regression, or a list of structural equations.
The method used to compute the R2 value (See Details)
For mixed models, marginal R2 considers only the variance by the fixed effects, and the conditional R2 by both the fixed and random effects.
For GLMs (
glm), supported methods include:
mcfadden 1 - ratio of likelihoods of full vs. null models
coxsnell McFadden's R2 but raised to 2/N. Upper limit is < 1
nagelkerke Adjusts Cox-Snell R2 so that upper limit = 1. The
For GLMERs fit to Poisson, Gamma, and negative binomial distributions
glmer.nb), supported methods include
delta Approximates the observation variance based on
second-order Taylor series expansion. Can be used with many families and
lognormal Observation variance is the variance of the log-normal
trigamma Provides most accurate estimate of the observation
variance but is limited to only the log link. The DEFAULT method
For GLMERs fit to the binomial distribution (
glmmPQL), supported methods include:
theoretical Assumes observation variance is pi^2/3
delta Approximates the observation variance as above. The
data.frame with the response, its family and link,
the method used to estimate R2, and the R2 value itself. Mixed models also
return marginal and conditional R2 values.
Jon Lefcheck <email@example.com>
Nakagawa, Shinichi, Paul CD Johnson, and Holger Schielzeth. "The coefficient of determination R 2 and intra-class correlation coefficient from generalized linear mixed-effects models revisited and expanded." Journal of the Royal Society Interface 14.134 (2017): 20170213.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
## Not run: # Create data dat <- data.frame( ynorm = rnorm(100), ypois = rpois(100, 100), x1 = rnorm(100), random = letters[1:5] ) # Get R2 for linear model rsquared(lm(ynorm ~ x1, dat)) # Get R2 for generalized linear model rsquared(glm(ypois ~ x1, "poisson", dat)) rsquared(glm(ypois ~ x1, "poisson", dat), method = "mcfadden") # McFadden R2 # Get R2 for generalized least-squares model rsquared(gls(ynorm ~ x1, dat)) # Get R2 for linear mixed effects model (nlme) rsquared(nlme::lme(ynorm ~ x1, random = ~ 1 | random, dat)) # Get R2 for linear mixed effects model (lme4) rsquared(lme4::lmer(ynorm ~ x1 + (1 | random), dat)) # Get R2 for generalized linear mixed effects model (lme4) rsquared(lme4::glmer(ypois ~ x1 + (1 | random), family = poisson, dat)) rsquared(lme4::glmer(ypois ~ x1 + (1 | random), family = poisson, dat), method = "delta") # Get R2 for generalized linear mixed effects model (glmmPQL) rsquared(MASS::glmmPQL(ypois ~ x1, random = ~ 1 | random, family = poisson, dat)) ## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.