Learn R Programming

rfriend (version 1.0.0)

f_model_comparison: Compare Two Statistical Models

Description

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

Usage

f_model_comparison(model1, model2, nested = NULL, digits = 3)

Value

A list of class "f_model_comparison" containing:

model1_name

The name of the first model.

model2_name

The name of the second model.

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.

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.

digits

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

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")

Author

Sander H. van Delden plantmind@proton.me

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-squared: Proportion of variance explained by the model.

  • Adjusted R-squared: R-squared 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.

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.

See Also

AIC, BIC, anova, logLik, r.squaredGLMM

Examples

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

# Example with GLMs.
# \donttest{
model1 <- glm(am ~ wt, data = mtcars, family = binomial)
model2 <- glm(am ~ wt + hp, data = mtcars, family = binomial)
comparison <- f_model_comparison(model1, model2)
print(comparison)
# }

# Example with automatic detection of nested models.
model1 <- lm(mpg ~ wt, data = mtcars)
model2 <- lm(mpg ~ wt + hp, data = mtcars)
comparison <- f_model_comparison(model1, model2)
print(comparison)

Run the code above in your browser using DataLab