1

The broomExtra package allows for easy augmentation of many different broom like packages in one place. It also offers the grouped_ variants of the broom functions (tidy, augment, glance). However, is this a shortcut to use purrr::map() and then the appropriate broom variant (tidy, augment, or glance)? In this case, consider tidy(). (Similar logic would be considered for the others).

  library(dplyr)  #mutate, group_by
#> Warning: package 'dplyr' was built under R version 4.1.2
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
  
# linear mixed effects model
broomExtra::grouped_tidy(
  data = mutate(MASS::Aids2, interval = death - diag),
  grouping.vars = sex,
  ..f = lme4::lmer,
  formula = interval ~ age + (1 | status),
  control = lme4::lmerControl(optimizer = "bobyqa"),
  tidy.args = list(conf.int = TRUE, conf.level = 0.99)
)
#> # A tibble: 8 x 9
#>   sex   effect   group    term   estimate std.error statistic conf.low conf.high
#>   <fct> <chr>    <chr>    <chr>     <dbl>     <dbl>     <dbl>    <dbl>     <dbl>
#> 1 F     fixed    <NA>     (Inte~   623.     161.         3.88   209.     1037.  
#> 2 F     fixed    <NA>     age       -4.34     2.61      -1.66   -11.1       2.38
#> 3 F     ran_pars status   sd__(~   169.      NA         NA       NA        NA   
#> 4 F     ran_pars Residual sd__O~   415.      NA         NA       NA        NA   
#> 5 M     fixed    <NA>     (Inte~   553.      62.5        8.84   392.      714.  
#> 6 M     fixed    <NA>     age       -3.60     0.696     -5.17    -5.39     -1.80
#> 7 M     ran_pars status   sd__(~    79.8     NA         NA       NA        NA   
#> 8 M     ran_pars Residual sd__O~   355.      NA         NA       NA        NA

#This is a shorthand to using the map function, I think: 
library(tidyr)
library(purrr)
library(magrittr)
#> 
#> Attaching package: 'magrittr'
#> The following object is masked from 'package:purrr':
#> 
#>     set_names
#> The following object is masked from 'package:tidyr':
#> 
#>     extract
library(broom.mixed) #also captured in broomExtra 
#> Warning: package 'broom.mixed' was built under R version 4.1.2

f <- MASS::Aids2 %>%
  mutate(interval = death - diag) %>%
  group_by(sex) %>%
  tidyr::nest()

g <- f %>%
  dplyr::mutate(
    fit_lmer4 = purrr::map(
      data, 
      ~ lme4::lmer(
        interval ~ age + (1 | status), 
        data = ., 
        control = lme4::lmerControl(optimizer = "bobyqa")
      )
    ),
    tidy_lemr4 = purrr::map(
      fit_lmer4, ~
        tidy(., conf.int = TRUE, conf.level = 0.99)          #from broom.mixed (also captured n broomExtar)
    )
  ) %>%
  unnest(tidy_lemr4)

g
#> # A tibble: 8 x 11
#> # Groups:   sex [2]
#>   sex   data  fit_lmer4 effect group term  estimate std.error statistic conf.low
#>   <fct> <lis> <list>    <chr>  <chr> <chr>    <dbl>     <dbl>     <dbl>    <dbl>
#> 1 M     <tib~ <lmerMod> fixed  <NA>  (Int~   553.      62.5        8.84   392.  
#> 2 M     <tib~ <lmerMod> fixed  <NA>  age      -3.60     0.696     -5.17    -5.39
#> 3 M     <tib~ <lmerMod> ran_p~ stat~ sd__~    79.8     NA         NA       NA   
#> 4 M     <tib~ <lmerMod> ran_p~ Resi~ sd__~   355.      NA         NA       NA   
#> 5 F     <tib~ <lmerMod> fixed  <NA>  (Int~   623.     161.         3.88   209.  
#> 6 F     <tib~ <lmerMod> fixed  <NA>  age      -4.34     2.61      -1.66   -11.1 
#> 7 F     <tib~ <lmerMod> ran_p~ stat~ sd__~   169.      NA         NA       NA   
#> 8 F     <tib~ <lmerMod> ran_p~ Resi~ sd__~   415.      NA         NA       NA   
#> # ... with 1 more variable: conf.high <dbl>

Created on 2022-02-04 by the reprex package (v2.0.1)

The answer seems to be yes, but I want to verify that the grouped_ broom functions provide a shortcut to this syntax. (By the way, examination of the function doesn't give insight into this)

grouped_tidy
function (data, grouping.vars, ..f, ..., tidy.args = list()) {
   tidy_group <- function(.x, .y) {
        model <- ..f(.y = ..., data = .x)
        exec(broomExtra::tidy, model, !!!tidy.args)
    }
    grouped_cleanup(data, enquos(grouping.vars), tidy_group)
}
phargart
  • 655
  • 3
  • 14
  • The broomExtra [vignette](https://cran.r-project.org/web/packages/broomExtra/readme/README.html) does not seem to speak to this question. – phargart Feb 04 '22 at 21:25
  • Also cited here: https://github.com/IndrajeetPatil/broomExtra/issues/30 – phargart Feb 04 '22 at 21:26
  • What do you mean by "is it a shortcut for purrr::map"? It could theoretically be implemented using `split` + `map` + `rbind`, but it is not. It uses `dplyr::group_modify`. You can see the `grouped_cleanup` code with `broomExtra:::grouped_cleanup`. It's just 3 dplyr verbs. However, `dplyr::group_modify` is defined using `dplyr::group_map`, which uses `purrr::map2`, so in some way it is a shortcut for a `purrr` map function, but there's of course more to it than just that. – IceCreamToucan Feb 05 '22 at 15:48

0 Answers0