Como hacer event studies en STATA
Hay varias maneras de hacer un event study. Hay dos maneras que uso hoy por hoy, una es usando el comando -- y otra es usando el comando --.
El comando coefplot lo uso, cuando simplemente estoy editando la data, y creando do-files y quiero algo rapido.
El comando regsave lo uso cuando quiero crear event studies para publicacion y presentaciones.
https://www.dropbox.com/s/opb5jgyqr7dbicf/BRFSS_tenncare.dta?dl=0
Contexto
Para este ejemplo usaremos en context de el paper “Effects of losing public health insurance on preventative care, health, and emergency department use: Evidence from the TennCare disenrollment”. En este paper se estudia el efecto de una reforma que ocurrio en el estado de Tennessee en el 2005. Esta reforma hizo que 170,000 personas perdieran su seguro de salud (publico). El paper investiga los efectos de esta reforma en outcomes de salud preventiva, salud de las personas y visitas a emergencias. La DiD comparara a TN antes y despues del 2005 contra los otros estados de el sur. La reforma solo afecto a las personas sin hijos, asi que solo usaremos esa poblacion. La variable TN es equivalente a “Treat” en nuestro caso. En este ejercicio usaremos este contexto, y trataremos de hacer un event study de el outcome “cover” que indica si la persona tiene seguro de salud o no.
Corriendo el DiD
* Abrimos la base de datos
global udep "/Users/laptop/Dropbox/1_Classes/UDEP LSE DD Lecture/Data"
use "$udep/BRFSS_tenncare.dta", clear
global controls "black hispanic_me other female education1 education2 education3 education4 education5 age maritaltype2 maritaltype3 maritaltype4 maritaltype5 maritaltype6"
global weights "[aw=_finalwt]"
set more off
* Corremos el DiD para todos, y de ahi por separado las personas que tienen hijos y las que no
estimates clear
eststo: reg cover i.tn##i.post3 i.statefip i.year i.imonth $controls $weights if fullsample==1 & year>=2000 & year<=2010
eststo: reg cover i.tn##i.post3 i.statefip i.year i.imonth $controls $weights if fullsample==1 & year>=2000 & year<=2010 & nokid==1
eststo: reg cover i.tn##i.post3 i.statefip i.year i.imonth $controls $weights if fullsample==1 & year>=2000 & year<=2010 & nokid==0
esttab *, keep (1.tn#1.post3) se
Usando Coefplot
El ejercicio es crear el event study de el DiD para las personas que no tiene hijos.
* Primero se tiene que instalar
ssc install coefplot, replace
*ssc install blindschemes
* Abrimos la base de datos
global udep "/Users/laptop/Dropbox/1_Classes/UDEP LSE DD Lecture/Data"
use "$udep/BRFSS_tenncare.dta", clear
global controls "black hispanic_me other female education1 education2 education3 education4 education5 age maritaltype2 maritaltype3 maritaltype4 maritaltype5 maritaltype6"
global weights "[aw=_finalwt]"
fvset base 2000 year
reg cover i.tn##i.year i.statefip i.imonth $controls $weights if fullsample==1 & year>=2000 & year<=2010 & nokid==1
coefplot, vertical base keep(1.tn#*.year)
* Esta es la manera mas facil de crear un event study para ver en general como
* Se ve.
* Ahora agregemosle mas cosas:
coefplot, vertical base keep(1.tn#*.year) xlabel(,angle(45))
coefplot, vertical base keep(1.tn#*.year) xlabel(,angle(45)) xline(4.5, lcolor(black) lwidth(thin) lpattern(dash)) yline(0, lcolor(black) lwidth(thin) lpattern(dash))
coefplot, vertical base keep(1.tn#*.year) xlabel(,angle(45)) xline(4.5, lcolor(black) lwidth(thin) lpattern(dash)) yline(0, lcolor(black) lwidth(thin) lpattern(dash)) scheme(plotplainblind)
coefplot, vertical base keep(1.tn#*.year) xlabel(,angle(45)) xline(4.5, lcolor(black) lwidth(thin) lpattern(dash)) yline(0, lcolor(black) lwidth(thin) lpattern(dash)) scheme(plotplainblind) recast(connected)
coefplot, vertical base keep(1.tn#*.year) xlabel(,angle(45)) xline(4.5, lcolor(black) lwidth(thin) lpattern(dash)) yline(0, lcolor(black) lwidth(thin) lpattern(dash)) scheme(plotplainblind) recast(connected) ciopts(recast(rline) lpattern(shortdash_dot)) title( )
# delimit ;
global labelcoef "
1.tn#1999.year="1999"
1.tn#2000.year="2000"
1.tn#2001.year="2001"
1.tn#2002.year="2002"
1.tn#2003.year="2003"
1.tn#2004.year="2004"
1.tn#2005.year="2005"
1.tn#2006.year="2006"
1.tn#2007.year="2007"
1.tn#2008.year="2008"
1.tn#2009.year="2009"
1.tn#2010.year="2010"
";
global spacecoef "
1.tn#1999.year
1.tn#2000.year
1.tn#2001.year
1.tn#2002.year
1.tn#2003.year
1.tn#2004.year
1.tn#2005.year
1.tn#2006.year
1.tn#2007.year
1.tn#2008.year
1.tn#2009.year
1.tn#2010.year
";
# delimit cr
coefplot, vertical base keep(1.tn#*.year) xlabel(,angle(45)) xline(4.5, lcolor(black) lwidth(thin) lpattern(dash)) yline(0, lcolor(black) lwidth(thin) lpattern(dash)) scheme(plotplainblind) recast(connected) ciopts(recast(rline) lpattern(shortdash_dot)) title( ) order($spacecoef) coeflabels($labelcoef)
Usando regsave
Regsave es un comando que permite salvar coeficientes para usarlos despues ya sea en graficos o en tablas. Esto lo podemos utilizar para tener mas flexibilidad y hasta se puede tener dos event studies al mismo tiempo.
* #### Primero se tiene que instalar
net install regsave, from("https://raw.githubusercontent.com/reifjulian/regsave/master") replace
*ssc intall blindschemes
* #### Abrimos la base de datos
global udep "/Users/laptop/Dropbox/1_Classes/UDEP LSE DD Lecture/Data"
use "$udep/BRFSS_tenncare.dta", clear
* ### Declaramos las globales
global controls "black hispanic_me other female education1 education2 education3 education4 education5 age maritaltype2 maritaltype3 maritaltype4 maritaltype5 maritaltype6"
global weights "[aw=_finalwt]"
* ## Corremos la regresion
fvset base 2000 year
reg cover i.tn##i.year i.statefip i.imonth $controls $weights if fullsample==1 & year>=2000 & year<=2010 & nokid==1
* ## Usamos RegSave
regsave using "$udep/eventstudy.dta", replace addlabel(y,`e(depvar)',spec,1) ci level(95)
* replace se cambia a "append" si se quiere agregar mas.
* ### Abriendo al Data
use "$udep/eventstudy.dta", clear
drop if var=="_cons"
gen year=.
forvalues i=2000/2010 {
replace year=`i' if var=="1o.tn#`i'b.year"
replace year=`i' if var=="1.tn#`i'.year"
}
keep if year!=.
set scheme plotplainblind
twoway ///
(connected coef year, sort lpattern(solid) lcolor(blue)) ///
(line ci_upper year , sort lpattern(dot) lcolor(blue)) ///
(line ci_lower year , sort lpattern(dot) lcolor(blue)) ///
, xline(2005) yline(0) legend(off) xtitle("")
* Si hubieran mas event studies, uno puede poner mas.
Ejemplos Avanzados con Regsave
twoway /// (connected coef var if y=="selfpay_pct_mhdiag_ahrq", sort lcolor(black) mcolor(black)) (rline ci_upper ci_lower var if y=="selfpay_pct_mhdiag_ahrq", sort lpattern(dot) lcolor(black)) (pcarrowi 0.08 12.5 .06 13 (12) "Self-Pay", lcolor(black) mcolor(black)) /// (connected coef var if y=="anyinsurance_pct_mhdiag_ahrq", sort lcolor(green) mcolor(green)) (rline ci_upper ci_lower var if y=="anyinsurance_pct_mhdiag_ahrq", sort lpattern(dot)) (pcarrowi 0.08 9 .05 9 (12) "Any Insurance", lcolor(green) mcolor(green)) /// (connected coef var if y=="medicaid_pct_mhdiag_ahrq", sort lpattern(solid) lcolor(reddish) mcolor(reddish)) (rline ci_upper ci_lower var if y=="medicaid_pct_mhdiag_ahrq", sort lpattern(dot)) (pcarrowi -0.1 12 -.08 13 (6) "Medicaid", lcolor(reddish) mcolor(reddish)) /// if var<17 & var>=6, scheme(plotplainblind) legend(off) /// xlabel(6(1)16, angle(forty_five) valuelabel) xtitle(" ") xline(11, lpattern(dash) lcolor(black%75)) /// ylabel(-0.2(0.1)0.2) yline(0, lpattern(dash) lcolor(black%75))
clear set obs 3 gen m = -15 in 1 replace m = -14 in 2 gen l = 1
twoway (scatter l m, c(n) msym(i) lwidth(none) lcolor(white) /// text(14 -14 "Two Parents", size(medium) j(right) place(west) bmargin(zero) margin(zero)) /// text(13 -14 "N=335,752", size(medium) j(right) place(west) bmargin(zero) margin(zero)) /// text(9 -14 "Single Mother", size(medium) j(right) place(west) bmargin(zero) margin(zero)) /// text(8 -14 "N=112,150", size(medium) j(right) place(west) bmargin(zero) margin(zero)) /// text(4 -14 "Single Fathers", size(medium) j(right) place(west) bmargin(zero) margin(zero)) /// text(3 -14 "N=29,482", size(medium) j(right) place(west) bmargin(zero) margin(zero)) /// legend(off) ylabel(none, nogrid) ytitle("") ysc(off range(1 16)) /// xsc(range(-16 -14) lstyle(none)) xlabel(-16(2)-14, nogmin tlcolor(white) labcolor(white) glc(white)) xtitle("", color(white)) /// title("" , color(white) size(medlarge)) fxsize(20) /// graphregion(fcolor(white) color(white) icolor(white) margin(zero)) /// saving("$resultsf/labels.gph", replace))
twoway (scatter rank coef) (rcap ci_lower ci_upper rank, sort horizontal) /// (scatter rank coef if rank==30, mcolor(purple) msymbol(circle)) (rcap ci_lower ci_upper rank if rank==30, sort lcolor(purple) horizontal) /// (scatter rank coef if rank==1, mcolor(purple) msymbol(circle)) (rcap ci_lower ci_upper rank if rank==1, sort lcolor(purple) horizontal) /// (scatter rank coef if rank==2, mcolor(purple) msymbol(circle)) (rcap ci_lower ci_upper rank if rank==2, sort lcolor(purple) horizontal) /// , ylabel(#30, valuelabel angle(horizontal)) xline(0) legend(off) ytitle("") /// title("Transitions causes by Parent's Eligibility Expansions") subtitle(Sample: Full Sample) scheme(plotplainblind)