# Controle Sintético no R # Prof. Raphael B. Corbi/ Monitor: Alan Leal # Leiamos os pacotes necessários para a análise: if(!require(tidyverse)){install.packages("tidyverse");require(tidyverse)} if(!require(Synth)){install.packages("Synth");require(Synth)} devtools::install_github("edunford/tidysynth") require(tidysynth) # Lendo agora os dados: load("/Users/alanleal/Downloads/dataverse_files/data.ME.26.12.21.RData") data_long <- data_long %>% filter(cntry_code %in% c( "BRA", "EST", "LTU", "LVA", "SVK", "HUN", "CHL", "TUR", "CRI", "MEX", "COL", "IND")) # Vamos usar um método alternativo usando o pacote tidysynth: mega_sports_out <- data_long %>% # initial the synthetic control object synthetic_control(outcome = gdppcgr, # outcome unit = cntry_code, # unit index in the panel data time = year, # time index in the panel data i_unit = "BRA", # unit where the intervention occurred i_time = 2014, # time period when the intervention occurred generate_placebos=T # generate placebo synthetic controls (for inference) ) %>% # Generate the aggregate predictors used to fit the weights # average log income, retail price of cigarettes, and proportion of the # population between 15 and 24 years of age from 1980 - 1988 generate_predictor(time_window = 1993:2014, industry=mean(industry,na.rm=TRUE), export=mean(export,na.rm=TRUE), lifeexp=mean(lifeexp,na.rm=TRUE), pop=mean(pop,na.rm=TRUE)) %>% # Generate the fitted weights for the synthetic control generate_weights(optimization_window = 1993:2014, # time to use in the optimization task margin_ipop = .02,sigf_ipop = 7,bound_ipop = 6 # optimizer options ) %>% # Generate the synthetic control generate_control() # Pré e pós-intervenção: mega_sports_out %>% plot_trends() # Plotando as diferenças no controle e sintético: mega_sports_out %>% plot_differences() mega_sports_out %>% plot_weights() # mega_sports_out %>% grab_balance_table() mega_sports_out %>% grab_loss() %>% filter(.placebo==0) %>% select(variable_mspe)