rsquared: Goodness-of-fit statistics for linear models

Description Usage Arguments Details Value Note Author(s) References Examples

View source: R/rsquared.R

Description

Returns (pseudo)-R^2 and AIC values for component models in structural equation model (SEM).

Usage

1
rsquared(modelList, aicc = FALSE)

Arguments

modelList

a list of regressions representing the structural equation model.

aicc

whether AIC corrected for small sample size (AICc) should be returned. Default is FALSE.

Details

Returns goodness-of-fit statistics for generalized linear (mixed) models, including (marginal and condition) R^2 and Akaike Information Criterion (AIC(c)) values.

Value

Returns a data.frame with the model class, the family, the link function, the marginal R^2 (based on fixed effects only) and/or conditional R^2 (based on fixed and random effects, if present), and the AIC(c) score (based on ML).

Note

If the models in modelList are all fit the same response, the function automatically returns the delta AIC(c) as the final column.

This function sources the rsquared.glmm function found here: https://github.com/jslefche/rsquared.glmm, and thus may be periodically updated independently of this package.

Author(s)

Jon Lefcheck & Juan Casallas

References

Nakagawa, Shinichi, and Holger Schielzeth. "A general and simple method for obtaining R2 from generalized linear mixed-effects models." Methods in Ecology and Evolution 4.2 (2013): 133-142.

Johnson, Paul C.D. "Extension of Nakagawa & Schielzeth's R2GLMM to random slopes models." Methods in Ecology and Evolution.

Examples

 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Load example data
data(shipley2009)

# Load model packages
library(lme4)
library(nlme)

# Create list of models 
shipley2009.modlist = list(
  
  lme(DD ~ lat, random = ~1|site/tree, na.action = na.omit, 
      data = shipley2009),
  
  lme(Date ~ DD, random = ~1|site/tree, na.action = na.omit, 
      data = shipley2009),
  
  lme(Growth ~ Date, random = ~1|site/tree, na.action = na.omit, 
      data = shipley2009),
  
  glmer(Live ~ Growth+(1|site)+(1|tree), 
        family=binomial(link = "logit"), data = shipley2009) 
  
)

# Return model fit statistics
rsquared(shipley2009.modlist)

## Not run: 
  # Get R2 for linear model
  lm.mod = lm(DD ~ lat, data = shipley2009)
  rsquared(lm.mod)
  
  # Get R2 for generalized linear model
  glm.mod = glm(Live ~ Growth, family = "binomial", data = shipley2009)
  rsquared(glm.mod)
  
  # Get R2 for generalized least-squares model
  library(nlme)
  
  gls.mod = gls(DD ~ lat, na.action = na.omit, data = shipley2009)
  rsquared(gls.mod)
  
  # Can supply the models as a list
  # Use lm and gls -- should produce very similar R2s, will also produce delta AIC
  rsquared(list(lm.mod, gls.mod))
  
  # Get R2 for linear mixed effects model (nlme)
  lme.mod = lme(DD ~ lat, random = ~1|site/tree, na.action = na.omit, data = shipley2009)
  rsquared(lme.mod)
  
  # Get R2 for linear mixed effects model (lme4)
  library(lme4)
  
  lmer.mod = lmer(DD ~ lat + (1|site/tree), data = shipley2009)
  rsquared(lmer.mod)
  
  # Get R2 for generalized linear mixed effects model (lme4)
  glmer.mod = glmer(Live ~ Growth + (1|site/tree), family = "binomial", data = shipley2009)
  rsquared(glmer.mod)
  
## End(Not run)

Example output

Loading required package: Matrix

Attaching package: 'nlme'

The following object is masked from 'package:lme4':

    lmList

     Class   Family     Link    n  Marginal Conditional
1      lme gaussian identity 1431 0.4766446   0.6932572
2      lme gaussian identity 1431 0.4083328   0.9838487
3      lme gaussian identity 1431 0.1070265   0.8364736
4 glmerMod binomial    logit 1431 0.5589204   0.6291980
  Class   Family     Link    n R.squared
1    lm gaussian identity 1431 0.5012513
  Class   Family  Link    n R.squared
1   glm binomial logit 1431 0.2912273
  Class   Family     Link    n R.squared
1   gls gaussian identity 1431 0.5009022
  Class   Family     Link    n R.squared      AIC     dAIC
1    lm gaussian identity 1431 0.5012513 9738.400 0.000000
2   gls gaussian identity 1431 0.5009022 9745.598 7.198286
  Class   Family     Link    n  Marginal Conditional
1   lme gaussian identity 1431 0.4766446   0.6932572
    Class   Family     Link    n  Marginal Conditional
1 lmerMod gaussian identity 1431 0.4766414   0.6932599
     Class   Family  Link    n  Marginal Conditional
1 glmerMod binomial logit 1431 0.5589204    0.629198

piecewiseSEM documentation built on May 29, 2017, 6:05 p.m.