edunford / tidysynth

A tidy implementation of the synthetic control method in R
Other
98 stars 14 forks source link

Fix synthetic_control when no placebos and the treated country doesn't come first in the data #21

Closed etiennebacher closed 2 years ago

etiennebacher commented 2 years ago

This PR closes #20. It fixes a bug that appears when synthetic_control() has generate_placebos = FALSE and when the treated country doesn't come first in the data (see the example below).

@edunford I didn't see a NEWS file, is there a place where I can add this so that users know that it is fixed in the Github version?

Currently:

library(tidysynth)

smoking2 <- smoking %>%
  dplyr::filter(state %in% c("Rhode Island", "Tennessee", "Connecticut", "California",
                             "Nevada", "Indiana", "Arkansas"))
smoking2 %>%
  synthetic_control(
    outcome = cigsale,
    unit = state,
    time = year,
    i_unit = "California",
    i_time = 1988,
    generate_placebos = FALSE
  )
#> # A tibble: 2 × 6
#>   .id      .placebo .type    .outcome          .original_data     .meta   
#>   <chr>       <dbl> <chr>    <list>            <list>             <list>  
#> 1 Arkansas        1 treated  <tibble [19 × 2]> <tibble [31 × 7]>  <tibble>
#> 2 Arkansas        1 controls <tibble [19 × 7]> <tibble [186 × 7]> <tibble>

Created on 2022-08-31 by the reprex package (v2.0.1)

New behavior:

library(tidysynth)

smoking2 <- smoking %>%
  dplyr::filter(state %in% c("Rhode Island", "Tennessee", "Connecticut", "California",
                             "Nevada", "Indiana", "Arkansas"))
smoking2 %>%
  synthetic_control(
    outcome = cigsale,
    unit = state,
    time = year,
    i_unit = "California",
    i_time = 1988,
    generate_placebos = FALSE
  )
#> # A tibble: 2 × 6
#>   .id        .placebo .type    .outcome          .original_data     .meta   
#>   <chr>         <dbl> <chr>    <list>            <list>             <list>  
#> 1 California        0 treated  <tibble [19 × 2]> <tibble [31 × 7]>  <tibble>
#> 2 California        0 controls <tibble [19 × 7]> <tibble [186 × 7]> <tibble>

Created on 2022-08-31 by the reprex package (v2.0.1)

edunford commented 2 years ago

This looks good! Thanks for the fix and test. I don't have a NEWS file yet since I actually haven't updated the original CRAN version of the package. But I should do so now given there has been a soft accumulation of bug fixes. I'll add a NEWS file when I do with your addition noted. Thanks again @etiennebacher!