Created
January 29, 2021 00:42
-
-
Save grantmcdermott/e7e2538aed23fbb9700c8017d1524fce to your computer and use it in GitHub Desktop.
Testing out the new tidysynth package on Cunningham (2021, chap 10.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Libraries --------------------------------------------------------------- | |
library(tidysynth) | |
library(haven) | |
# Data -------------------------------------------------------------------- | |
texas = read_dta('https://raw.github.com/scunning1975/mixtape/master/texas.dta') | |
# Set & run the SC -------------------------------------------------------- | |
texas_synth = | |
texas %>% | |
## Initialize the synthetic control object | |
synthetic_control( | |
outcome = bmprison, # outcome | |
unit = state, # unit index in the panel data | |
time = year, # time index in the panel data | |
i_unit = 'Texas', # unit where the intervention occurred | |
i_time = 1994, # time period when the intervention occurred | |
generate_placebos = TRUE # generate placebo synthetic controls (for inference) | |
) %>% | |
## Generate the aggregate predictors used to fit the weights... | |
generate_predictor( | |
time_window = 1988, | |
bmprison = mean(bmprison, na.rm = T) | |
) %>% | |
## 1990 vars | |
generate_predictor( | |
time_window = 1990, | |
alcohol = mean(alcohol, na.rm=T), | |
perc1519 = mean(perc1519, na.rm=T)) %>% | |
## 1990 - 1991 vars | |
generate_predictor( | |
time_window = 1990:1991, | |
aidscapita = mean(aidscapita, na.rm = T) | |
) %>% | |
## 1990:1992 vars | |
generate_predictor( | |
time_window = 1990:1992, | |
black = mean(black, na.rm = T) | |
) %>% | |
## Generate the fitted weights for the synthetic control | |
generate_weights( | |
optimization_window = 1985:1993 ## time to use in the optimization task | |
) %>% | |
# Generate the synthetic control | |
generate_control() | |
# Plots ------------------------------------------------------------------- | |
## Fig. 10.9 | |
plot_trends(texas_synth) | |
## Fig. 10.10 | |
plot_differences(texas_synth) | |
## Donor weights | |
plot_weights(texas_synth) | |
## Fig 10.12 | |
plot_placebos(texas_synth) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment