MatthieuStigler / multiDiff

Multi-period diff and diff
Other
3 stars 0 forks source link

mdd_group_means: add arg to compute just before/after!? #17

Closed MatthieuStigler closed 1 year ago

MatthieuStigler commented 1 year ago

For now mdd_group_means computes for all period before after, would be nice to have arg to just compute pre/post? Note that only makes sense for "common" design.

Current code:

library(dplyr)
#> 
#> 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
library(multiDiff)

set.seed(123)
dat_sim <- sim_dat_common(N = 10, timing_treatment = 6:10,beta =1.2) 
dat_sim$pre_post <- ifelse(dat_sim$Time<=5, 0, 1)

dat_sim |> 
  group_by(pre_post, treat_group) |> 
  summarise(y=mean(y))
#> `summarise()` has grouped output by 'pre_post'. You can override using the
#> `.groups` argument.
#> # A tibble: 4 × 3
#> # Groups:   pre_post [2]
#>   pre_post treat_group       y
#>      <dbl> <chr>         <dbl>
#> 1        0 control      0.0124
#> 2        0 treated     -0.446 
#> 3        1 control      0.600 
#> 4        1 treated      1.37

dat_sim |> 
  mdd_data_format() |> 
  mdd_group_means() |> 
  mutate(pre_post = ifelse(Time<=5, 0, 1)) |> 
  group_by(pre_post, .group) |> 
  summarise(y=mean(y))
#> `summarise()` has grouped output by 'pre_post'. You can override using the
#> `.groups` argument.
#> # A tibble: 4 × 3
#> # Groups:   pre_post [2]
#>   pre_post .group        y
#>      <dbl> <chr>     <dbl>
#> 1        0 control  0.0124
#> 2        0 treated -0.446 
#> 3        1 control  0.600 
#> 4        1 treated  1.37

Created on 2023-08-19 with reprex v2.0.2

MatthieuStigler commented 1 year ago

done!

See now:

library(multiDiff)

set.seed(123)
dat_sim_common <- sim_dat_common(N = 10) |> 
  mdd_data_format() 
dat_sim_stag <- sim_dat_staggered(N = 10) |> 
  mdd_data_format() 
dat_sim_gen <- sim_dat(N = 10) |> 
  mdd_data_format() 

mdd_group_means(mdd_dat = dat_sim_common)
#> # A tibble: 20 × 3
#>    .group   Time       y
#>    <chr>   <int>   <dbl>
#>  1 control     1  1.36  
#>  2 control     2  0.773 
#>  3 control     3  0.107 
#>  4 control     4 -0.391 
#>  5 control     5 -1.79  
#>  6 control     6  0.666 
#>  7 control     7  1.88  
#>  8 control     8  0.722 
#>  9 control     9  0.0710
#> 10 control    10 -0.342 
#> 11 treated     1  1.19  
#> 12 treated     2  1.20  
#> 13 treated     3  0.650 
#> 14 treated     4 -0.0854
#> 15 treated     5 -1.19  
#> 16 treated     6  1.66  
#> 17 treated     7  1.87  
#> 18 treated     8  1.92  
#> 19 treated     9 -0.0208
#> 20 treated    10  0.397
mdd_group_means(mdd_dat = dat_sim_common, by_treat_period = FALSE)
#> # A tibble: 20 × 3
#>    .group   Time       y
#>    <chr>   <int>   <dbl>
#>  1 control     1  1.36  
#>  2 control     2  0.773 
#>  3 control     3  0.107 
#>  4 control     4 -0.391 
#>  5 control     5 -1.79  
#>  6 control     6  0.666 
#>  7 control     7  1.88  
#>  8 control     8  0.722 
#>  9 control     9  0.0710
#> 10 control    10 -0.342 
#> 11 treated     1  1.19  
#> 12 treated     2  1.20  
#> 13 treated     3  0.650 
#> 14 treated     4 -0.0854
#> 15 treated     5 -1.19  
#> 16 treated     6  1.66  
#> 17 treated     7  1.87  
#> 18 treated     8  1.92  
#> 19 treated     9 -0.0208
#> 20 treated    10  0.397

mdd_group_means(mdd_dat = dat_sim_stag)
#> # A tibble: 105 × 3
#>    .group                         Time       y
#>    <chr>                         <int>   <dbl>
#>  1 0_0_0_0_0_0_0_0_0_0_0_0_0_0_0     1  1.44  
#>  2 0_0_0_0_0_0_0_0_0_0_0_0_0_0_0     2  1.17  
#>  3 0_0_0_0_0_0_0_0_0_0_0_0_0_0_0     3  1.75  
#>  4 0_0_0_0_0_0_0_0_0_0_0_0_0_0_0     4 -0.826 
#>  5 0_0_0_0_0_0_0_0_0_0_0_0_0_0_0     5  0.0648
#>  6 0_0_0_0_0_0_0_0_0_0_0_0_0_0_0     6  0.769 
#>  7 0_0_0_0_0_0_0_0_0_0_0_0_0_0_0     7 -0.0139
#>  8 0_0_0_0_0_0_0_0_0_0_0_0_0_0_0     8 -1.30  
#>  9 0_0_0_0_0_0_0_0_0_0_0_0_0_0_0     9 -2.99  
#> 10 0_0_0_0_0_0_0_0_0_0_0_0_0_0_0    10 -0.107 
#> # ℹ 95 more rows
mdd_group_means(mdd_dat = dat_sim_stag, by_treat_period = TRUE)
#> # A tibble: 14 × 3
#>    .group                        Time       y
#>    <chr>                         <chr>  <dbl>
#>  1 0_0_0_0_0_0_0_0_0_0_0_0_0_0_0 Post  -0.524
#>  2 0_0_0_0_0_0_0_0_0_0_0_0_0_0_0 Pre    1.45 
#>  3 0_0_0_0_0_0_0_0_0_0_0_0_0_0_1 Post  -0.455
#>  4 0_0_0_0_0_0_0_0_0_0_0_0_0_0_1 Pre    0.775
#>  5 0_0_0_0_0_0_0_0_0_0_0_0_0_1_1 Post   0.657
#>  6 0_0_0_0_0_0_0_0_0_0_0_0_0_1_1 Pre    1.88 
#>  7 0_0_0_0_0_0_0_0_0_1_1_1_1_1_1 Post  -0.546
#>  8 0_0_0_0_0_0_0_0_0_1_1_1_1_1_1 Pre   -0.392
#>  9 0_0_0_0_0_0_0_1_1_1_1_1_1_1_1 Post   0.721
#> 10 0_0_0_0_0_0_0_1_1_1_1_1_1_1_1 Pre    0.483
#> 11 0_0_0_0_1_1_1_1_1_1_1_1_1_1_1 Post   0.517
#> 12 0_0_0_0_1_1_1_1_1_1_1_1_1_1_1 Pre    0.471
#> 13 0_0_0_1_1_1_1_1_1_1_1_1_1_1_1 Post   2.91 
#> 14 0_0_0_1_1_1_1_1_1_1_1_1_1_1_1 Pre    2.73

mdd_group_means(mdd_dat = dat_sim_gen)
#> # A tibble: 150 × 3
#>    .group                         Time     y
#>    <chr>                         <int> <dbl>
#>  1 0_0_0_0_0_0_0_0_0_0_1_0_1_0_0     1 -4.52
#>  2 0_0_0_0_0_0_0_0_0_0_1_0_1_0_0     2 -1.65
#>  3 0_0_0_0_0_0_0_0_0_0_1_0_1_0_0     3 -3.15
#>  4 0_0_0_0_0_0_0_0_0_0_1_0_1_0_0     4 -2.66
#>  5 0_0_0_0_0_0_0_0_0_0_1_0_1_0_0     5 -2.07
#>  6 0_0_0_0_0_0_0_0_0_0_1_0_1_0_0     6 -4.32
#>  7 0_0_0_0_0_0_0_0_0_0_1_0_1_0_0     7 -3.60
#>  8 0_0_0_0_0_0_0_0_0_0_1_0_1_0_0     8 -2.85
#>  9 0_0_0_0_0_0_0_0_0_0_1_0_1_0_0     9 -4.48
#> 10 0_0_0_0_0_0_0_0_0_0_1_0_1_0_0    10 -2.60
#> # ℹ 140 more rows
mdd_group_means(mdd_dat = dat_sim_gen, by_treat_period = TRUE)
#> # A tibble: 20 × 3
#>    .group                        Time       y
#>    <chr>                         <chr>  <dbl>
#>  1 0_0_0_0_0_0_0_0_0_0_1_0_1_0_0 Post  -2.98 
#>  2 0_0_0_0_0_0_0_0_0_0_1_0_1_0_0 Pre   -2.82 
#>  3 0_0_0_0_0_0_0_0_1_0_0_0_0_0_1 Post   0.526
#>  4 0_0_0_0_0_0_0_0_1_0_0_0_0_0_1 Pre    0.254
#>  5 0_0_0_0_0_0_1_0_0_0_0_0_0_0_1 Post  -0.402
#>  6 0_0_0_0_0_0_1_0_0_0_0_0_0_0_1 Pre    0.113
#>  7 0_0_0_0_0_1_0_0_0_1_0_0_1_0_0 Post  -0.540
#>  8 0_0_0_0_0_1_0_0_0_1_0_0_1_0_0 Pre    2.37 
#>  9 0_0_1_1_0_0_0_1_1_0_1_0_0_0_1 Post   0.899
#> 10 0_0_1_1_0_0_0_1_1_0_1_0_0_0_1 Pre    0.161
#> 11 0_1_0_0_0_0_0_1_1_0_1_0_1_0_0 Post   1.06 
#> 12 0_1_0_0_0_0_0_1_1_0_1_0_1_0_0 Pre    0.631
#> 13 0_1_0_0_1_0_0_0_1_0_0_0_0_0_1 Post  -1.27 
#> 14 0_1_0_0_1_0_0_0_1_0_0_0_0_0_1 Pre   -0.595
#> 15 1_0_0_1_0_0_0_0_0_0_0_0_1_0_0 Post  -2.51 
#> 16 1_0_0_1_0_0_0_0_0_0_0_0_1_0_0 Pre   -1.57 
#> 17 1_1_0_0_0_1_0_0_1_0_0_0_0_1_0 Post   1.17 
#> 18 1_1_0_0_0_1_0_0_1_0_0_0_0_1_0 Pre    3.28 
#> 19 1_1_0_1_0_0_0_0_0_0_0_0_0_0_0 Post   0.443
#> 20 1_1_0_1_0_0_0_0_0_0_0_0_0_0_0 Pre    0.593

Created on 2023-08-19 with reprex v2.0.2