***Aula prática de RDD - Luiz Scorzafave - 2017 *******modelos não paramétricos - Sharp RDD******************** ******************************************************************************** ** RDROBUST Stata Package ** Do-file for Empirical Illustration ** Authors: Sebastian Calonico, Matias D. Cattaneo, Max H. Farrell and Rocio Titiunik ******************************************************************************** *****************INSTALANDO OS PACOTES NECESSÁRIOS************************** net install rdrobust, from(https://sites.google.com/site/rdpackages/rdrobust/stata) replace net install rdplot, from(https://sites.google.com/site/rdpackages/rdplot/stata) replace net install rddensity, from(https://sites.google.com/site/rdpackages/rddensity/stata) replace ******************************************************************************** clear all cap log close set more off cd "C:\FEA_OwnCloud\guilherme\FEARP\mestrado\avaliação\rdd" ******************************************************************************** ** Summary Stats ******************************************************************************** log using "aula_rdd_2017.log", replace use rdrobust_senate.dta, clear sum vote margin class termshouse termssenate population, sep(2) ******************************************************************************** ** rdplot with confidence intervals ******************************************************************************** rdplot vote margin, ci(95) graph_options(title("RD Plot: U.S. Senate Election Data") /// ytitle(Vote Share in Election at time t+2) /// xtitle(Vote Share in Election at time t) /// graphregion(color(white))) graph export "figure1.pdf", replace rdplot vote margin, binselect(es) ci(95) /// Cattaneo tem várias opções para binselect graph_options(title("RD Plot: U.S. Senate Election Data") /// ytitle(Vote Share in Election at time t+2) /// xtitle(Vote Share in Election at time t) /// graphregion(color(white))) graph export "figure2.pdf", replace /*interpretando a saída do rdplot Cutoff c = 0 | Left of c Right of c Number of obs = 1297 ----------------------+---------------------- Kernel = Uniform Number of obs | 595 702 Eff. Number of obs | 595 702 Order poly. fit (p) | 4 4 --> ordem do polinômio BW poly. fit (h) | 100,000 100,000 --> janela usada para ajustar o polínômio (tudo) Number of bins scale | 1,000 1,000 --> fator de escala para usar multiplos de bins, o defaul é 1 Outcome: vote. Running variable: margin. --------------------------------------------- | Left of c Right of c ----------------------+---------------------- Bins selected | 8 9 -->número de bins estimados Average bin length | 12,500 11,111 --> tamanho dos bins Median bin length | 12,500 11,111 ----------------------+---------------------- IMSE-optimal bins | 8 9 --> número de bins que minimiza o erro quadrático médio integrado Mimicking Var. bins | 15 35 --> ???? ----------------------+---------------------- Rel. to IMSE-optimal: | Implied scale | 1,000 1,000 WIMSE var. weight | 0,500 0,500 --> parâmetros de referência para a qualidade do tamanho do bin WIMSE bias weight | 0,500 0,500 --------------------------------------------- */ rdplot vote margin, nbins (8 9) ci(95) graph_options(title("RD Plot: U.S. Senate Election Data - 17 bins") /// ytitle(Vote Share in Election at time t+2) /// xtitle(Vote Share in Election at time t) /// graphregion(color(white))) graph export "figure3.pdf", replace rdplot vote margin, nbins (24 27) ci(95) graph_options(title("RD Plot: U.S. Senate Election Data - 17 bins") /// ytitle(Vote Share in Election at time t+2) /// xtitle(Vote Share in Election at time t) /// graphregion(color(white))) graph export "figure4.pdf", replace rdplot vote margin, p(3) ci(95) shade /// polinômio cúbico com intervalo de confiança rdplot vote margin, p(2) ci(95) shade /// polinômio quadrático com intervalo de confiança ******************************************************************************** ** rdrobust ******************************************************************************** rdrobust vote margin ******************************************************************************** ** rdrobust backward compatibility ******************************************************************************** rdrobust vote margin, h(16.79369) b(27.43745) ******************************************************************************** ** rdplot to show rdrobust estimate ******************************************************************************** qui rdrobust vote margin rdplot vote margin if -e(h_l)<= margin & margin <= e(h_r), /// binselect(esmv) kernel(triangular) h(`e(h_l)' `e(h_r)') p(1) /// graph_options(title("RD Plot: U.S. Senate Election Data") /// ytitle(Vote Share in Election at time t+2) /// xtitle(Vote Share in Election at time t) /// graphregion(color(white))) graph export "figure5.pdf", replace ******************************************************************************** ** rdrobust with covariates within the same window (i.e., using same bandwidths) ******************************************************************************** qui rdrobust vote margin local len = `e(ci_r_rb)' - `e(ci_l_rb)' rdrobust vote margin, covs(class termshouse termssenate) /// h(`e(h_l)' `e(h_r)') b(`e(b_l)' `e(b_r)') display "CI length change: " round(((`e(ci_r_rb)'-`e(ci_l_rb)')/`len'-1)*100,.01) "%" ******************************************************************************** ** rdrobust with covariates with data-driven optimal bandwidths ******************************************************************************** qui rdrobust vote margin local len = `e(ci_r_rb)' - `e(ci_l_rb)' rdrobust vote margin, covs(class termshouse termssenate) display "CI length change: " round(((`e(ci_r_rb)'-`e(ci_l_rb)')/`len'-1)*100,.01) "%" ******************************************************************************** ** rdrobust with useless covariate ******************************************************************************** qui rdrobust vote margin local len = `e(ci_r_rb)' - `e(ci_l_rb)' rdrobust vote margin, covs(population) display "CI length change: " round(((`e(ci_r_rb)'-`e(ci_l_rb)')/`len'-1)*100,.01) "%" sjlog close, replace ******************************************************************************** ** rdrobust check covariate "balanced" ******************************************************************************** local covs "class termshouse termssenate population" local num: list sizeof covs mat balance = J(`num',2,.) local row = 1 foreach z in `covs' { qui rdrobust `z' margin mat balance[`row',1] = round(e(tau_cl),.001) mat balance[`row',2] = round(e(pv_rb),.001) local ++row } mat rownames balance = `covs' mat colnames balance = "RD Effect" "Robust p-val" mat lis balance ******************************************************************************** ** rdbwselect with all estimates ******************************************************************************** rdbwselect vote margin, all ******************************************************************************** ** Other examples ******************************************************************************** rdrobust vote margin, kernel(uniform) vce(cluster state) rdrobust vote margin, bwselect(certwo) vce(hc3) rdrobust vote margin, h(12 15) b(18 20) rdrobust vote margin, covs(class) bwselect(cerrd) scaleregul(0) rho(1) rdbwselect vote margin, kernel(uniform) vce(cluster state) all rdbwselect vote margin, covs(class) bwselect(msetwo) vce(hc2) all **** verificar se as outras variáveis explicativas não dão pulo no ponto de **** quebra da variável de atribuição rdrobust population margin rdrobust termshouse margin ********* placebo *********** rdrobust vote margin, c(20) rdrobust vote margin, c(-20) ************** RDDENSITY ***************** *** verificar se não tem pulo na densidade da variável de atribuição no ponto de quebra ******************************************************************************** ** rddensity: default options ******************************************************************************** net install lpdensity, from(https://sites.google.com/site/nppackages/lpdensity/stata) replace rddensity margin, plot ******************************************************************************** ** rddensity: with plot and graph_options rddensity margin, plot /// graph_options(graphregion(color(white)) /// xtitle("Margin of Victory") ytitle("Density") legend(off)) ******************************************************************************** ** rddensity: all statistics & default options ******************************************************************************** rddensity margin, all ******************************************************************************** ** rddensity: default statistic, restricted model & plugin standard errors ******************************************************************************** rddensity margin, fitselect(restricted) vce(plugin) ******************************************************************************** ** rdbwdensity: default options ******************************************************************************** rdbwdensity margin ******************************************** ** Modelo Paramétrico ******************************************** gen trat=0 replace trat=1 if margin>0 gen tm=trat*margin gen mar2=margin^2 gen mar3=margin^3 gen mar4=margin^4 gen tm2=trat*mar2 gen tm3=trat*mar3 gen tm4=trat*mar4 * RDD linear reg vote trat margin tm est store rdd_par1 * RDD quadrático reg vote trat margin tm mar2 tm2 est store rdd_par2 * RDD cúbico reg vote trat margin tm mar2 tm2 mar3 tm3 est store rdd_par3 estout rdd1 rdd2 rdd3 * RDD quarta reg vote trat margin tm mar2 tm2 mar3 tm3 mar4 tm4 est store rdd_par4 estout rdd1 rdd2 rdd3 rdd4 ssc install binscatter binscatter vote margin , rd(0) reportreg binscatter vote margin , rd(0) reportreg n(40) genxq(bin)