Sebastian Tello
  • Home
  • CV
  • Contact
  • Research
  • Resources
  • RMDA
  • APP
5️⃣

Worksheet #5 CSDID

Este ejemplo sigue de Asjad Naqvi

csdid

Follow the latest developments in the DiD literature.

asjadnaqvi.github.io

Primero vamos a crear la data como lo hicimos la vez pasada

clear

local units = 30
local start = 1
local end 	= 60

local time = `end' - `start' + 1
local obsv = `units' * `time'
set obs `obsv'

egen id	   = seq(), b(`time')  
egen t 	   = seq(), f(`start') t(`end') 	

sort  id t
xtset id t


set seed 20211222

gen Y 	   		= 0		// outcome variable	
gen D 	   		= 0		// intervention variable
gen cohort      = .  	// treatment cohort
gen effect      = .		// treatment effect size
gen first_treat = .		// when the treatment happens for each cohort
gen rel_time	= .     // time - first_treat

levelsof id, local(lvls)
foreach x of local lvls {
	local chrt = runiformint(0,5)	
	replace cohort = `chrt' if id==`x'
}


levelsof cohort , local(lvls)  
foreach x of local lvls {
	
	local eff = runiformint(2,10)
		replace effect = `eff' if cohort==`x'
			
	local timing = runiformint(`start',`end' + 20)	// 
	replace first_treat = `timing' if cohort==`x'
	replace first_treat = . if first_treat > `end'
		replace D = 1 if cohort==`x' & t>= `timing' 
}

replace rel_time = t - first_treat
replace Y = id + t + cond(D==1, effect * rel_time, 0) + rnormal()

xtline Y, overlay legend(off)
‣
Figura de la Data

Ahora aplicamos el csdid comando

Y ahora tratemos con el nuevo comando CSDID2 que se puede obtener en este link: https://github.com/friosavila/csdid2

InstagramBluesky
ssc install csdid, replace
ssc install drdid
ssc install event_plot, replace

* csdid Y [ind vars], [ivar(varname)] time(varname) gvar(varname) [options]

* Y	outcome variable
* ivar	panel id
* time	time variable
* gvar	timing of first treatment (0 for untreated groups)

* Tenemos que crear el gvar 
gen gvar = first_treat
recode gvar (. = 0)

* ## CSdid1

csdid Y, ivar(id) time(t) gvar(gvar) notyet

estat event, window(-10 10) estore(cs)

event_plot cs, default_look graph_opt(xtitle("Periods since the event") ytitle("Average effect") ///
	title("csdid") xlabel(-10(1)10)) stub_lag(Tp#) stub_lead(Tm#) together

csdid Y, ivar(id) time(t) gvar(gvar) asinr agg(simple)
csdid Y, ivar(id) time(t) gvar(gvar) asinr agg(group)
* ## CSdid2
csdid2 , clear

csdid2 Y, ivar(id) time(t) gvar(gvar) notyet
*Use the not-treated groups as control 

estat event

estat event, wboot plot

csdid2 Y, ivar(id) time(t) gvar(gvar) asinr agg(simple)
csdid2 Y, ivar(id) time(t) gvar(gvar) notyet agg(simple)

csdid2 Y, ivar(id) time(t) gvar(gvar) asinr agg(group)
csdid2 Y, ivar(id) time(t) gvar(gvar) notyet agg(group)


ssc install event_plot, replace

estat event, revent(-10/10) estore(cs) 

event_plot cs, default_look graph_opt(xtitle("Periods since the event") ytitle("Average effect") ///
	title("csdid") xlabel(-10(1)10)) stub_lag(tp#) stub_lead(tm#) together