cod: Goodness-of-fit measures for regression models

Description Usage Arguments Details Value Note References Examples

View source: R/r2.R

Description

Compute Goodness-of-fit measures for various regression models, including mixed and Bayesian regression models.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
cod(x)

r2(x, ...)

## S3 method for class 'lme'
r2(x, n = NULL, ...)

## S3 method for class 'stanreg'
r2(x, loo = FALSE, ...)

## S3 method for class 'brmsfit'
r2(x, loo = FALSE, ...)

Arguments

x

Fitted model of class lm, glm, merMod, glmmTMB, lme, plm, stanreg or brmsfit. For method cod(), only a glm with binrary response.

...

Currently not used.

n

Optional, an lme 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'.

loo

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().

Details

For linear models, the r-squared and adjusted r-squared value is returned, as provided by the summary-function.

For mixed models (from lme4 or glmmTMB) marginal and conditional r-squared values are calculated, based on Nakagawa et al. 2017.

For lme-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 lme-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.

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.

Value

For r2(), depending on the model, returns:

For cod(), returns the D Coefficient of Discrimination, also known as Tjur's R-squared value.

Note

cod()

This method calculates the Coefficient of Discrimination D for generalized linear (mixed) models for binary data. It is an alternative to other Pseudo-R-squared values like Nakelkerke's R2 or Cox-Snell R2. The Coefficient of Discrimination D can be read like any other (Pseudo-)R-squared value.

r2()

For mixed models, the marginal r-squared considers only the variance of the fixed effects, while the conditional r-squared takes both the fixed and random effects into account.

For lme-objects, 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.

The Omega-squared statistics, if n is given, is 1 - the proportion of the residual variance of the full model compared to the null model's residual variance, or in short: the the proportion of the residual variation explained by the covariates.

Alternative ways to assess the "goodness-of-fit" is to compare the ICC of the null model with the ICC of the full model (see icc).

References

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
data(efc)

# Tjur's R-squared value
efc$services <- ifelse(efc$tot_sc_e > 0, 1, 0)
fit <- glm(services ~ neg_c_7 + c161sex + e42dep,
           data = efc, family = binomial(link = "logit"))
cod(fit)

library(lme4)
fit <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
r2(fit)

fit <- lm(barthtot ~ c160age + c12hour, data = efc)
r2(fit)

# Pseudo-R-squared values
fit <- glm(services ~ neg_c_7 + c161sex + e42dep,
           data = efc, family = binomial(link = "logit"))
r2(fit)

sjstats documentation built on Nov. 15, 2018, 5:04 p.m.