IndrajeetPatil/broomExtra

combine glance and performance outputs in a single dataframe?

Closed this issue · 0 comments

library(broom)
library(performance)
library(broomExtra)
#> Registered S3 method overwritten by 'broom.mixed':
#>   method      from 
#>   tidy.gamlss broom
#> 
#> Attaching package: 'broomExtra'
#> The following objects are masked from 'package:broom':
#> 
#>     augment, glance, tidy
library(tidyverse)

easystats_to_glance_names <- function(x, ...) {
  dplyr::rename_all(
    .tbl = x,
    .funs = dplyr::recode,
    r2 = "r.squared",
    r2.adjusted = "adj.r.squared"
  )
} 


mod <- stats::lm(formula = wt ~ am * cyl, data = mtcars)

df_broom <- glance(mod) %>% rename_all(., tolower)
df_performance <- 
  model_performance(mod, metrics = "all") %>% 
  easystats_to_tidy_names() %>%
  easystats_to_glance_names()

dplyr::bind_cols(
  df_broom,
  dplyr::select(df_performance, -dplyr::intersect(names(df_broom), names(df_performance))),
)
#> # A tibble: 1 x 13
#>   r.squared adj.r.squared sigma statistic p.value    df loglik   aic   bic
#>       <dbl>         <dbl> <dbl>     <dbl>   <dbl> <dbl>  <dbl> <dbl> <dbl>
#> 1     0.724         0.694 0.541      24.4 5.69e-8     3  -23.6  57.3  64.6
#> # ... with 4 more variables: deviance <dbl>, df.residual <int>, nobs <int>,
#> #   rmse <dbl>