f_model_comparison {rfriend}R Documentation

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)

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.

Details

Calculate various metrics to assess model fit:

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.

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.

Supported Model Classes

The function supports the following model classes:

Note

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_comparison(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_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)


[Package rfriend version 1.0.0 Index]