Skip to contents

stack_metrics() calculates basic model metrics like MSE for the models passed in, then stacks them in a dataframe for comparison. This supports lm, glm, and lmer models, and different metrics are calculated for each. This does not perform model selection based on a given criteria, but it makes the tedious task of, say, comparing R-squared across several models very easy.

Usage

stack_metrics(...)

Arguments

...

lm, glm, or lmer models to summarize and combine.

Value

A tibble() that includes a variety of evaluation metrics.

Examples

# lm example -------------------------------------------
lm_1 = lm(mpg ~ cyl + disp + hp, data = mtcars)
lm_2 = lm(mpg ~ hp + drat + wt, data = mtcars)
lm_3 = lm(mpg ~ ., data = mtcars)
lm_combined = stack_metrics(lm_1, lm_2, lm_3)
lm_combined
#> # A tibble: 3 × 6
#>   model                                r.squared adj.r.squared   MSE  RMSE   MAE
#>   <chr>                                    <dbl>         <dbl> <dbl> <dbl> <dbl>
#> 1 lm(formula = mpg ~ cyl + disp + hp,…     0.768         0.743  8.17  2.86  2.32
#> 2 lm(formula = mpg ~ hp + drat + wt, …     0.837         0.819  5.74  2.40  1.91
#> 3 lm(formula = mpg ~ ., data = mtcars)     0.869         0.807  4.61  2.15  1.72

# glm example ------------------------------------------
glm_1 = glm(vs ~ drat + hp, data = mtcars)
glm_2 = glm(vs ~ wt + qsec, data = mtcars)
glm_3 = glm(vs ~ ., data = mtcars)
glm_combined = stack_metrics(glm_1, glm_2, glm_3)
glm_combined
#> # A tibble: 3 × 4
#>   model                                        deviance   AIC   BIC
#>   <chr>                                           <dbl> <dbl> <dbl>
#> 1 glm(formula = vs ~ drat + hp, data = mtcars)     3.63  29.1  35.0
#> 2 glm(formula = vs ~ wt + qsec, data = mtcars)     2.04  10.8  16.6
#> 3 glm(formula = vs ~ ., data = mtcars)             1.58  18.6  36.2

# lme4 example -----------------------------------------
lmer_1 = lme4::lmer(Sepal.Length ~ (1 | Species), data = iris)
lmer_2 = lme4::lmer(Sepal.Length ~ (1 | Species) + Petal.Length, data = iris)
lmer_combined = stack_metrics(lmer_1, lmer_2)
lmer_combined
#> # A tibble: 2 × 4
#>   model                                                     deviance   AIC   BIC
#>   <chr>                                                        <dbl> <dbl> <dbl>
#> 1 lme4::lmer(formula = Sepal.Length ~ (1 | Species), data …     240.  246.  255.
#> 2 lme4::lmer(formula = Sepal.Length ~ (1 | Species) + Peta…     120.  128.  140.