View source: R/fmodel_compare.R
f_model_comparison | R Documentation |
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.
f_model_comparison(model1, model2, nested = NULL, digits = 3)
model1 |
The first model object. Supported classes include: |
model2 |
The second model object. Supported classes include: |
nested |
Logical. If |
digits |
Integer. The number of decimal places to round the output metrics. Defaults to |
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.
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. |
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")
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.
For mixed models, the function relies on the 'r.squaredGLMM' function from the 'MuMIn' package for R^2 calculation.
The idea of this function (not the code), I got from Dustin Fife's function 'model.comparison' in the super cool 'flexplot package'.
Sander H. van Delden plantmind@proton.me
AIC
, BIC
, anova
, logLik
, r.squaredGLMM
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.