f_model_compare: Compare Two Statistical Models

View source: R/fmodel_compare.R

f_model_compareR Documentation

Compare Two Statistical Models

Description

Compares two statistical models by calculating key metrics such as AIC, BIC, log-likelihood, R^2, and others. Supports comparison of nested models using ANOVA tests.

Usage

f_model_compare(
  model1,
  model2,
  nested = NULL,
  model1_name = NULL,
  model2_name = NULL,
  digits = 3
)

Arguments

model1

The first model object. Supported classes include: "lm", "glm", "aov", "lmerMod", "glmerMod", and "nls".

model2

The second model object. Supported classes include: "lm", "glm", "aov", "lmerMod", "glmerMod", and "nls".

nested

Logical. If TRUE, assumes the models are nested and performs an ANOVA comparison. If NULL (default), the function attempts to automatically determine if the models are nested.

model1_name

Optional character string. A custom name for model1 in the output. If NULL (default), the function uses deparse(substitute(model1)).

model2_name

Optional character string. A custom name for model2 in the output. If NULL (default), the function uses deparse(substitute(model2)).

digits

Integer. The number of decimal places to round the output metrics. Defaults to 3.

Details

Calculate various metrics to assess model fit:

  • AIC/BIC: Lower values indicate better fit.

  • Log-Likelihood: Higher values (less negative) indicate better fit.

  • R^2: Proportion of variance explained by the model.

  • Adjusted R^2: R^2 penalized for the number of parameters (for linear models).

  • Nagelkerke R^2: A pseudo-R^2 for generalized linear models (GLMs).

  • Marginal/Conditional R^2: For mixed models, marginal R^2 reflects fixed effects, while conditional R^2 includes random effects.

  • Sigma: Residual standard error.

  • Deviance: Model deviance.

  • SSE: Sum of squared errors.

  • Parameters (df): Number of model parameters.

  • Residual df: Residual degrees of freedom.

When nested models are detected or specified, model1 is always treated as the simpler model (fewer parameters). If the user passes the complex model first, the function automatically swaps them and issues a message.

If the models are nested, an ANOVA test is performed to compare them, and a p-value is provided to assess whether the more complex model significantly improves fit.

Value

A list of class "f_model_comparison" containing:

model1_name

The name of the first model (always the simpler model when nested).

model2_name

The name of the second model (always the more complex model when nested).

model1_class

The class of the first model.

model2_class

The class of the second model.

metrics_table

A data frame summarizing metrics for both models, their differences, and (if applicable) the ANOVA p-value.

formatted_metrics_table

A formatted version of the metrics table for printing.

anova_comparison

The ANOVA comparison results if the models are nested and an ANOVA test was performed.

nested

Logical indicating whether the models were treated as nested.

swapped

Logical indicating whether the model order was swapped to ensure model1 is the simpler model.

Supported Model Classes

The function supports the following model classes:

  • Linear models ("lm")

  • Generalized linear models ("glm")

  • Analysis of variance models ("aov")

  • Linear mixed models ("lmerMod")

  • Generalized linear mixed models ("glmerMod")

  • Nonlinear least squares models ("nls")

Note: Multi-stratum AOV models (fitted with Error()) are not supported and will produce a warning.

Note

  • The function supports a variety of model types but may issue warnings if unsupported or partially supported classes are used.

  • For GLMs, Nagelkerke's R^2 is used as a pseudo-R^2 approximation, computed from the model's null deviance to avoid refitting a null model.

  • For mixed models, the function relies on the 'r.squaredGLMM' function from the 'MuMIn' package for R^2 calculation.

  • For NLS models, R^2 is provided for convenience but should be interpreted with caution as it does not have the same statistical properties as in linear models.

  • The idea of this function (not the code), I got from Dustin Fife's function 'model.comparison' in the super cool 'flexplot package'.

Author(s)

Sander H. van Delden plantmind@proton.me

See Also

AIC, BIC, anova, logLik, r.squaredGLMM

Examples

# Example with linear models.
model1 <- lm(mpg ~ wt, data = mtcars)
model2 <- lm(mpg ~ wt + hp, data = mtcars)
comparison <- f_model_compare(model1, model2)
print(comparison)

# Example with GLMs.

model1 <- glm(am ~ wt, data = mtcars, family = binomial)
model2 <- glm(am ~ wt + hp, data = mtcars, family = binomial)
comparison <- f_model_compare(model1, model2)
print(comparison)


# Models can be passed in any order - the function auto-swaps if needed.
complex <- lm(mpg ~ wt + hp + qsec, data = mtcars)
simple  <- lm(mpg ~ wt, data = mtcars)
comparison <- f_model_compare(complex, simple)
# model1 will be "simple", model2 will be "complex" in the output

# Example with custom model names (useful when calling from wrapper functions).
comparison <- f_model_compare(model1, model2,
                              model1_name = "Weight only",
                              model2_name = "Weight + Horsepower")
print(comparison)


rfriend documentation built on July 7, 2026, 1:06 a.m.