Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.by argument for the group_*() functions #6914

Closed
tvqt opened this issue Aug 16, 2023 · 1 comment
Closed

.by argument for the group_*() functions #6914

tvqt opened this issue Aug 16, 2023 · 1 comment

Comments

@tvqt
Copy link

tvqt commented Aug 16, 2023

I was reading through this StackOverflow question from a few years ago, and learned of the group_map function. It seems like it might be the ugly duckling of the dplyr function family.

It would be neat if the group_*() functions (group_map(), group_modify() and group_walk()) included the .by syntax similar to the other dplyr functions!

For example, in the case of:

library(estimatr)

mtcars %>%
  group_by(am, vs) %>%
  group_map(~tidy(lm_robust(mpg ~ hp, data = .x))) %>%
  ungroup() # question asker forgot to include this (!)

It could instead be:

library(estimatr)

mtcars |> group_map(~tidy(lm_robust(mpg ~ hp, data = .x)), .by = c(am, vs))

Keep up the great work! :-)

@DavisVaughan
Copy link
Member

These are still a bit experimental as they don't quite feel exactly right, so we probably won't add the .by argument to them anytime soon

A lot of times you can accomplish the same thing with reframe(), i.e.

library(estimatr)
library(dplyr)

mtcars |> 
  as_tibble() |>
  reframe(
    tidy(lm_robust(mpg ~ hp, data = pick(mpg, hp))), 
    .by = c(am, vs)
  )
#> # A tibble: 8 × 11
#>      am    vs term       estimate std.error statistic p.value conf.low conf.high
#>   <dbl> <dbl> <chr>         <dbl>     <dbl>     <dbl>   <dbl>    <dbl>     <dbl>
#> 1     1     0 (Intercep…  26.4      2.14        12.3  2.49e-4  20.5     32.4    
#> 2     1     0 hp          -0.0368   0.00909     -4.05 1.54e-2  -0.0621  -0.0116 
#> 3     1     1 (Intercep…  37.3      6.09         6.13 1.67e-3  21.7     53.0    
#> 4     1     1 hp          -0.111    0.0866      -1.28 2.55e-1  -0.334    0.111  
#> 5     0     1 (Intercep…  30.9      1.57        19.7  6.18e-6  26.9     34.9    
#> 6     0     1 hp          -0.0994   0.0157      -6.33 1.45e-3  -0.140   -0.0590 
#> 7     0     0 (Intercep…  23.2      3.27         7.10 3.28e-5  15.9     30.5    
#> 8     0     0 hp          -0.0422   0.0164      -2.56 2.82e-2  -0.0788  -0.00552
#> # ℹ 2 more variables: df <dbl>, outcome <chr>

I'm going to close this one because it is something we are passively thinking about, but not anything we plan to change in the medium term

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants