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

Worksheet #6 BJS

*## Creamos la Data

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)


* ### BJS Inputation
cap ado uninstall ftools
net install ftools, from(https://github.com/sergiocorreia/ftools/raw/master/src/)
ssc install gtools
 gtools, upgrade
cap ado uninstall reghdfe
net install reghdfe, from("https://raw.githubusercontent.com/sergiocorreia/reghdfe/master/src/")


ssc install did_imputation, replace



did_imputation Y i t first_treat, horizons(0/10) pretrend(10)

* No calcula los coeficients del "pre-periodo" porque el minimo es 30 unidades de control.
* Cambiemos el minimo a 0 unidades

did_imputation Y i t first_treat, horizons(0/10) pretrend(10) minn(0)

* ahora si tenemos los pre-dummies. 

event_plot, default_look graph_opt(xtitle("Periods since the event") ytitle("Average effect") ///
	title("BJS") xlabel(-10(1)10)) stub_lag(tau#) stub_lead(pre#) together

 event_plot, default_look graph_opt(xtitle("Days since the event") ytitle("Coefficients"))
InstagramBluesky