一张图里画出5种异质性稳健DID的平行趋势与动态效应的完整code和示例
业务范围

你的位置:恒耀娱乐 > 业务范围 >

一张图里画出5种异质性稳健DID的平行趋势与动态效应的完整code和示例

发布日期:2024-10-14 15:44    点击次数:137

图片

图片

接着1.最新: 2024版异质性稳健DID最全指南! 更新太快脑袋跟不上看这里!2.不炒冷饭! 2024年最新“2”份DID使用检查清单, 前沿DID使用基本规范指南! 今天展示一个在一张图里画出5种异质性稳健DID方法的平行趋势与动态效应的完整code和示例。

该示例将指导你如何在单张图表中绘制五种不同估计方法下的异质性稳健双重差分估计值(DID)。不仅展示了事前平行趋势,还揭示了动态效应的演变过程。可以直观地观察不同处理组在政策或事件影响前后的变化趋势,从而更准确地评估政策或事件的因果效应。

关于平行趋势,1.平行趋势检验, 事件研究图绘制, 安慰剂检验的保姆级程序指南!2.标准DID中的平行趋势检验,动态效应, 安慰剂检验, 预期效应教程,3.平行趋势通不过, 该采取什么方法来更好地满足平行趋势呢?4.平行趋势的敏感性检验, 结果能容忍违反多大程度的平行趋势,5.某经济学权威刊物上平行趋势怎么这样, 真给我看迷糊了! 到底如何对pre-trend检测, 讨论和处理呢?6.在平行趋势检验中对政策前后系列年份进行缩尾处理?7.三重差分DDD估计中平行趋势检验如何操作呢?8.2篇TOP5: 当前平行趋势检验方法有问题,新的平行趋势检验方法已经出现,9.前沿: 平行趋势没有通过却成功发在了AER上!10.只有4期数据, 为啥平行趋势检验时有6期呢? DID与连续变量交互系数如何解释? 11.历史上首篇DID中修改平行趋势而被撤稿的TOP5文章!直接通过一个程序在一张图里画出5种异质性稳健DID的平行趋势和政策动态效应。模拟示例分析:事件研究中的因果效应估计方法本文通过一系列方法的模拟示例,向读者展示了如何估计事件研究中的因果效应。同时,介绍了如何利用event_plot命令绘制系数及其置信区间,以直观展示研究结果。作者:Kirill Borusyak在进行执行代码之前,需要准备以下统计命令:did_imputation(Borusyak等,2021):可通过SSC平台获取。did_multiplegt(de Chaisemartin和D'Haultfoeuille,2020):同样可在SSC平台下载。eventstudyinteract(San和Abraham,2020):SSC上也有提供。csdid(Callaway和Sant'Anna,2020):SSC平台亦可下载。

图片

图片

下面的代码是上图完整的示例code,里面包括了自己模拟的数据,因此可以完整运行下来。

*群友可直接前往社群下载。// 生成模拟数据,Generate a complete panel of 300 units observed in 15 periodsclear alltimer clearset seed 10global T = 15global I = 300set obs `=$I*$T'gen i = int((_n-1)/$T )+1 // unit idgen t = mod((_n-1),$T )+1 // calendar periodtsset i t// Randomly generate treatment rollout years uniformly across Ei=10..16 (note that periods t>=16 would not be useful since all units are treated by then)gen Ei = ceil(runiform()*7)+$T -6 if t==1 // year when unit is first treatedbys i (t): replace Ei = Ei[1]gen K = t-Ei // "relative time", i.e. the number periods since treated (could be missing if never-treated)gen D = K>=0 & Ei!=. // treatment indicator// Generate the outcome with parallel trends and heterogeneous treatment effectsgen tau = cond(D==1, (t-12.5), 0) // heterogeneous treatment effects (in this case vary over calendar periods)gen eps = rnormal() // error termgen Y = i + 3*t + tau*D + eps // the outcome (FEs play no role since all methods control for them)//save five_estimators_data, replace//  did_imputation估计,Estimation with did_imputation of Borusyak et al. (2021)did_imputation Y i t Ei, allhorizons pretrend(5)event_plot, default_look graph_opt(xtitle("Periods since the event") ytitle("Average causal effect") ///title("Borusyak et al. (2021) imputation estimator") xlabel(-5(1)5))estimates store bjs // storing the estimates for later// Estimation with did_multiplegt of de Chaisemartin and D'Haultfoeuille (2020)did_multiplegt Y i t D, robust_dynamic dynamic(5) placebo(5) breps(100) cluster(i) event_plot e(estimates)#e(variances), default_look graph_opt(xtitle("Periods since the event") ytitle("Average causal effect") ///title("de Chaisemartin and D'Haultfoeuille (2020)") xlabel(-5(1)5)) stub_lag(Effect_#) stub_lead(Placebo_#) togethermatrix dcdh_b = e(estimates) // storing the estimates for latermatrix dcdh_v = e(variances)// csdid估计, Estimation with csdid of Callaway and Sant'Anna (2020)gen gvar = cond(Ei==., 0, Ei) // group variable as required for the csdid commandcsdid Y, ivar(i) time(t) gvar(gvar) notyetestat event, estore(cs) // this produces and stores the estimates at the same timeevent_plot cs, default_look graph_opt(xtitle("Periods since the event") ytitle("Average causal effect") xlabel(-14(1)5) ///title("Callaway and Sant'Anna (2020)")) stub_lag(Tp#) stub_lead(Tm#) together//  eventstudyinteract估计,Estimation with eventstudyinteract of Sun and Abraham (2020)sum Eigen lastcohort = Ei==r(max) // dummy for the latest- or never-treated cohortforvalues l = 0/5 {gen L`l'event = K==`l'}forvalues l = 1/14 {gen F`l'event = K==-`l'}drop F1event // normalize K=-1 (and also K=-15) to zeroeventstudyinteract Y L*event F*event, vce(cluster i) absorb(i t) cohort(Ei) control_cohort(lastcohort)event_plot e(b_iw)#e(V_iw), default_look graph_opt(xtitle("Periods since the event") ytitle("Average causal effect") xlabel(-14(1)5) ///title("Sun and Abraham (2020)")) stub_lag(L#event) stub_lead(F#event) togethermatrix sa_b = e(b_iw) // storing the estimates for latermatrix sa_v = e(V_iw)//  TWFE估计,TWFE OLS estimation (which is correct here because of treatment effect homogeneity). Some groups could be binned.reghdfe Y F*event L*event, a(i t) cluster(i)event_plot, default_look stub_lag(L#event) stub_lead(F#event) together graph_opt(xtitle("Days since the event") ytitle("OLS coefficients") xlabel(-14(1)5) ///title("OLS"))estimates store ols // saving the estimates for later// Construct the vector of true average treatment effects by the number of periods since treatmentmatrix btrue = J(1,6,.)matrix colnames btrue = tau0 tau1 tau2 tau3 tau4 tau5qui forvalues h = 0/5 {sum tau if K==`h'matrix btrue[1,`h'+1]=r(mean)}// 一张图里展示所有估计值的事前趋势与事后动态效应,Combine all plots using the stored estimates// Combine all plots using the stored estimatesevent_plot btrue# bjs dcdh_b#dcdh_v cs sa_b#sa_v ols, ///stub_lag(tau# tau# Effect_# Tp# L#event L#event) stub_lead(pre# pre# Placebo_# Tm# F#event F#event) plottype(scatter) ciplottype(rcap) ///together perturb(-0.325(0.13)0.325) trimlead(5) noautolegend ///graph_opt(title("Event study estimators in a simulated panel (300 units, 15 periods)", size(medlarge)) ///xtitle("Periods since the event") ytitle("Average causal effect") xlabel(-5(1)5) ylabel(0(1)3) ///legend(order(1 "True value" 2 "Borusyak et al." 4 "de Chaisemartin-D'Haultfoeuille" ///6 "Callaway-Sant'Anna" 8 "Sun-Abraham" 10 "OLS") rows(3) region(style(none))) ////// the following lines replace default_look with something more elaboratexline(-0.5, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///) ///lag_opt1(msymbol(+) color(cranberry)) lag_ci_opt1(color(cranberry)) ///lag_opt2(msymbol(O) color(cranberry)) lag_ci_opt2(color(cranberry)) ///lag_opt3(msymbol(Dh) color(navy)) lag_ci_opt3(color(navy)) ///lag_opt4(msymbol(Th) color(forest_green)) lag_ci_opt4(color(forest_green)) ///lag_opt5(msymbol(Sh) color(dkorange)) lag_ci_opt5(color(dkorange)) ///lag_opt6(msymbol(Oh) color(purple)) lag_ci_opt6(color(purple)) graph export "five_estimators_example.png", replace关于多期DID或交叠DID: 1.DID相关前沿问题“政策交错执行+堆叠DID+事件研究”, 附完整slides,2.交错(渐进)DID中, 用TWFE估计处理效应的问题, 及Bacon分解识别估计偏误,3.典范! 这篇AER在一图表里用了所有DID最新进展方法, 审稿人直接服了!4.最新Sun和Abraham(2020)和TWFE估计多期或交错DID并绘图展示结果!详细解读code!5.多期DID或渐进DID或交叠DID, 最新Stata执行命令整理如下供大家学习,6.多期DID前沿方法大讨论, e.g., 进入-退出型DID, 异质性和动态性处理效应DID, 基期选择问题等,7.交叠DID中平行趋势检验, 事件研究图绘制, 安慰剂检验的保姆级程序指南!8.欣慰! 营养午餐计划终于登上TOP5! 交叠DID+异质性稳健DID!9.用事件研究法开展政策评估的过程, 手把手教学文章!10.从双重差分法到事件研究法, 双重差分滥用与需要注意的问题,11.系统梳理DID最新进展: 从多期DID的潜在问题到当前主流解决方法和代码! 12.标准DID中的平行趋势检验,动态效应, 安慰剂检验, 预期效应教程,13.DID从经典到前沿方法的保姆级教程, 释放最完整数据和代码!下面这些短链接文章属于合集,可以收藏起来阅读,不然以后都找不到了。

7年,计量经济圈近2000篇不重类计量文章,

可直接在公众号菜单栏搜索任何计量相关问题,

Econometrics Circle

数据系列:空间矩阵 | 工企数据 | PM2.5 | 市场化指数 | CO2数据 |  夜间灯光 | 官员方言  | 微观数据 | 内部数据计量系列:匹配方法 | 内生性 | 工具变量 | DID | 面板数据 | 常用TOOL | 中介调节 | 时间序列 | RDD断点 | 合成控制 | 200篇合辑 | 因果识别 | 社会网络 | 空间DID数据处理:Stata | R | Python | 缺失值 | CHIP/ CHNS/CHARLS/CFPS/CGSS等 |干货系列:能源环境 | 效率研究 | 空间计量 | 国际经贸 | 计量软件 | 商科研究 | 机器学习 | SSCI | CSSCI | SSCI查询 | 名家经验

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报。

友情链接:

Powered by 恒耀娱乐 @2013-2022 RSS地图 HTML地图