/tests/testthat/test_synthetic_control.R

https://github.com/edunford/tidysynth · R · 192 lines · 110 code · 50 blank · 32 comment · 1 complexity · aeb49a0a0695bfb564e8c529ccd429d0 MD5 · raw file

  1. context("Test synthetic_control() initialization of the synth pipeline")
  2. #'
  3. #' NOTE: if all the grab_ functions are operating correctly, than all the plot
  4. #' functions will.
  5. #'
  6. # Generate FAKE DATA for the TESTs ----------------------------------------
  7. set.seed(123)
  8. # Treatment unit
  9. treated <-
  10. tibble::tibble(unit= "a",
  11. time= 1990:2000,
  12. outcome = runif(11,-1,1),
  13. treatment_value = c(rep(0,6),1,2,3,4,5),
  14. intervention_outcome = outcome + treatment_value,
  15. x1 = rnorm(11,0,1),
  16. x2 = rnorm(11,0,1),
  17. x3 = rnorm(11,0,1))
  18. # Control units
  19. controls <-
  20. dplyr::bind_rows(
  21. tidyr::crossing(tibble::tibble(unit=c("b","c","d","e")),
  22. tibble::tibble(time=1990:2000))) %>%
  23. dplyr::mutate(outcome = runif(44,-1,1),
  24. x1 = rnorm(44,0,1),
  25. x2 = rnorm(44,0,1),
  26. x3 = rnorm(44,0,1))
  27. dat <- dplyr::bind_rows(treated,controls) %>% tidyr::replace_na(list(treatment_value=0))
  28. # Tests -------------------------------------------------------------------
  29. test_that("Test initialization of a synth pipeline using sythetic_control() works as expected with placebos",{
  30. synth_out <- synthetic_control(data=dat,outcome = outcome,time = time,
  31. unit = unit,i_unit = "a",i_time=1995,
  32. generate_placebos = T)
  33. # Correct class
  34. # expect_is(synth_out,class="synth_tbl")
  35. # Correct columns
  36. expect_equal(colnames(synth_out),expected = c(".id",".placebo",".type", ".outcome", ".original_data", ".meta"))
  37. # There is a treated and controls unit
  38. expect_equal(unique(synth_out$.type),expected = c("treated","controls"))
  39. # Original data is stored correctly
  40. expect_identical(synth_out$.original_data[[1]], expected = treated)
  41. # all placebo units are contained in the data.
  42. expect_equal(unique(synth_out$.id), expected = c("a","b","c","d","e"))
  43. # .placebo indicator reflects the actual number of placebo entries
  44. expect_true(sum(synth_out$.placebo)==8)
  45. # check meta
  46. expect_equal(synth_out$.meta[[1]],
  47. expected=tibble::tibble(unit_index="unit",
  48. time_index = "time",
  49. treatment_unit = "a",
  50. treatment_time = 1995,
  51. outcome = "outcome"))
  52. expect_equal(synth_out$.meta[[9]],
  53. expected=tibble::tibble(unit_index="unit",
  54. time_index = "time",
  55. treatment_unit = "e",
  56. treatment_time = 1995,
  57. outcome = "outcome"))
  58. # Outcome initialization is correct (for the treated unit)
  59. expect_is(synth_out$.outcome[[1]],class="tbl_df")
  60. expect_equal(colnames(synth_out$.outcome[[1]]),expected = c("time_unit","a"))
  61. expect_equivalent(synth_out$.outcome[[1]],
  62. expected = treated %>%
  63. dplyr::select(time_unit=time,a=outcome) %>%
  64. dplyr::filter(time_unit <= 1995))
  65. # Outcome initialization is correct (for a placebo unit)
  66. expect_is(synth_out$.outcome[[9]],class="tbl_df")
  67. expect_equal(colnames(synth_out$.outcome[[9]]),expected = c("time_unit","e"))
  68. expect_equivalent(synth_out$.outcome[[9]],
  69. expected = controls %>%
  70. dplyr::filter(unit == "e") %>%
  71. dplyr::select(time_unit=time,e=outcome) %>%
  72. dplyr::filter(time_unit <= 1995))
  73. # Check control unit (for treated unit)
  74. expect_is(synth_out$.outcome[[2]],class="tbl_df")
  75. expect_equal(colnames(synth_out$.outcome[[2]]),expected = c("time_unit","b","c","d","e"))
  76. expect_equivalent(synth_out$.outcome[[2]],
  77. expected = controls %>%
  78. dplyr::select(time_unit = time,outcome,unit) %>%
  79. tidyr::pivot_wider(names_from = unit,values_from = outcome) %>%
  80. dplyr::filter(time_unit <= 1995))
  81. # Check control unit (for a placebo unit)
  82. expect_is(synth_out$.outcome[[10]],class="tbl_df")
  83. expect_equal(colnames(synth_out$.outcome[[10]]),expected = c("time_unit","a","b","c","d"))
  84. expect_equivalent(synth_out$.outcome[[10]],
  85. expected = dat %>%
  86. dplyr::filter(unit!="e") %>%
  87. dplyr::select(time_unit = time,outcome,unit) %>%
  88. tidyr::pivot_wider(names_from = unit,values_from = outcome) %>%
  89. dplyr::filter(time_unit <= 1995))
  90. })
  91. # Generate test for the relevant grab function
  92. test_that("Test grab_outcomes works",{
  93. synth_out <- synthetic_control(data=dat,outcome = outcome,time = time,
  94. unit = unit,i_unit = "a",i_time=1995,
  95. generate_placebos = T)
  96. # Grabs the outcome
  97. expect_equivalent(synth_out %>% grab_outcome(),
  98. synth_out$.outcome[[1]])
  99. # Grab function works for placebos
  100. expect_equivalent(synth_out %>% grab_outcome(placebo=T) %>% .[7,5] %>% round(.,3),
  101. tibble::tibble(b=0.226))
  102. })
  103. test_that("Test initialization of a synth pipeline using sythetic_control() works as expected without placebos",{
  104. synth_out <- synthetic_control(data=dat,outcome = outcome,time = time,
  105. unit = unit,i_unit = "a",i_time=1995,
  106. generate_placebos = F)
  107. # Correct class
  108. # expect_is(synth_out,class="synth_tbl")
  109. # Correct columns
  110. expect_equal(colnames(synth_out),expected = c(".id",".placebo",".type", ".outcome", ".original_data", ".meta"))
  111. # There is a treated and controls unit
  112. expect_equal(unique(synth_out$.type),expected = c("treated","controls"))
  113. # Original data is stored correctly
  114. expect_identical(synth_out$.original_data[[1]], expected = treated)
  115. # all placebo units are contained in the data.
  116. expect_error(expect_equal(unique(synth_out$.id), expected = c("a","b","c","d","e")))
  117. expect_equal(unique(synth_out$.id), expected = c("a"))
  118. expect_true(sum(synth_out$.placebo)==0)
  119. # Outcome initialization is correct (for the treated unit)
  120. expect_equal(colnames(synth_out$.outcome[[1]]),expected = c("time_unit","a"))
  121. expect_is(synth_out$.outcome[[1]],class="tbl_df")
  122. expect_equivalent(synth_out$.outcome[[1]],
  123. expected = treated %>%
  124. dplyr::select(time_unit=time,a=outcome) %>%
  125. dplyr::filter(time_unit <= 1995))
  126. expect_equal(synth_out$.meta[[1]],
  127. expected=tibble::tibble(unit_index="unit",
  128. time_index = "time",
  129. treatment_unit = "a",
  130. treatment_time = 1995,
  131. outcome = "outcome"))
  132. # There shouldn't be a plcebo unit
  133. expect_error(synth_out$.outcome[[9]])
  134. # Check control unit (for treated unit)
  135. expect_is(synth_out$.outcome[[2]],class="tbl_df")
  136. expect_equal(colnames(synth_out$.outcome[[2]]),expected = c("time_unit","b","c","d","e"))
  137. expect_equivalent(synth_out$.outcome[[2]],
  138. expected = controls %>%
  139. dplyr::select(time_unit = time,outcome,unit) %>%
  140. tidyr::pivot_wider(names_from = unit,values_from = outcome) %>%
  141. dplyr::filter(time_unit <= 1995))
  142. })