rcompanion (version 2.4.35)

compareLM: Compare fit statistics for lm models

Description

Produces a table of fit statistics for multiple lm models.

Usage

compareLM(fits, ...)

Value

A list of two objects: The series of model calls, and a data frame of statistics for each model.

Arguments

fits

A series of model object names, separated by commas.

...

Other arguments passed to list.

Author

Salvatore Mangiafico, mangiafico@njaes.rutgers.edu

Details

Produces a table of fit statistics for multiple lm models: AIC, AICc, BIC, p-value, R-squared, and adjusted R-squared.

Smaller values for AIC, AICc, and BIC indicate a better balance of goodness-of-fit of the model and the complexity of the model. The goal is to find a model that adequately explains the data without having too many terms.

BIC tends to choose models with fewer parameters relative to AIC.

In the table, Shapiro.W and Shapiro.p are the W statistic and p-value for the Shapiro-Wilks test on the residuals of the model.

For comparisons with AIC, etc., to be valid, both models must have the same data, without transformations, use the same dependent variable, and be fit with the same method. They do not need to be nested.

The function will fail if a model formula is longer than 500 characters.

References

https://rcompanion.org/handbook/I_10.html, https://rcompanion.org/rcompanion/e_05.html

See Also

compareGLM, pairwiseModelAnova, accuracy

Examples

Run this code
### Compare among polynomial models
data(BrendonSmall)
BrendonSmall$Calories = as.numeric(BrendonSmall$Calories)

BrendonSmall$Calories2 = BrendonSmall$Calories * BrendonSmall$Calories
BrendonSmall$Calories3 = BrendonSmall$Calories * BrendonSmall$Calories * 
                         BrendonSmall$Calories
BrendonSmall$Calories4 = BrendonSmall$Calories * BrendonSmall$Calories * 
                         BrendonSmall$Calories * BrendonSmall$Calories
model.1 = lm(Sodium ~ Calories, data = BrendonSmall)
model.2 = lm(Sodium ~ Calories + Calories2, data = BrendonSmall)
model.3 = lm(Sodium ~ Calories + Calories2 + Calories3, data = BrendonSmall)
model.4 = lm(Sodium ~ Calories + Calories2 + Calories3 + Calories4,
             data = BrendonSmall)
compareLM(model.1, model.2, model.3, model.4)

Run the code above in your browser using DataLab