首页 > 编程知识 正文

多期did安慰剂检验stata代码,多期did安慰剂检验 0real changes made

时间:2023-05-05 15:12:50 阅读:194671 作者:1088

首先申明本文是在学习连老师和一个公众号后写出的代码,进行一个讲解,也将他们的原文链接放在下面。写这篇只是为了下次再做类似研究时候能够立马想起来。
1.
https://www.lianxh.cn/news/0a63a4fb8eb70.html
2.
https://mp.weixin.qq.com/s/yl2s1_i-pI123iOaIHUWgQ

clear安慰剂检验***********第一步,设置矩阵大小:set matsize 5000***记得设置这个矩阵大小mat b = J(500,1,0) mat se = J(500,1,0) mat p = J(500,1,0)*****先把循环内的语句都单独跑一次,所有跑完才放入循环中forvalues i = 1/500{***********第二步,设置为伪处理组:use "C:UsersDellDesktop1.24.dta", clear***这里是打开自己的数据库xtset city wavekeep if wave == 2018sample 15, count***2018和15都是自定义的keep city wave y***要把时间和城市维度和y都keep出来save "C:UsersDellDesktopmatchcity.dta", replace***选择一个文件夹进行循环,要循环500次。merge m:m cityid using "C:UsersDellDesktop1.24.dta"gen treat = (_merge == 3) save "C:UsersDellDesktopmatchcity`i'.dta", replace***********第三步,设置为伪虚拟变量:use "C:UsersDellDesktop1.24.dta", clearbsample 15, strata(city)keep city wave ysave "C:UsersDellDesktopmatchcity.dta", replacemkmat wave, matrix(sampleyear)***********第四步,设置为政策的伪虚拟变量:use "C:UsersDellDesktopmatchcity`i'.dta", cleargen time = 0foreach j of numlist 1/110{replace time = 1 if (cityid == `j' & wave >= sampleyear [`j',1])} gen DID=time*treat***********第五步,回归:qui xtreg y DID 控制变量 i.wave i.city,femat b[`i',1] = _b[time]mat se[`i',1] = _se[time]scalar df_r = e(N) - e(df_m) -1mat p[`i',1] = 2*ttail(df_r,abs(_b[time]/_se[time]))}set more off***********第六步,绘图:svmat b, names(coef)svmat se, names(se)svmat p, names(pvalue)drop if pvalue1 == .label var pvalue1 "p值"label var coef1 "估计系数"****这里可以根据自己需要进行修改twoway (scatter pvalue1 coef1 , ///xlabel(-0.2(0.1)0.25, grid) ///yline(0.1,lp(shortdash)) ///xline(0.1,lp(shortdash)) ///xtitle(估计系数,orientation(horizontal) size(Medium large)) ///ytitle("p""值",orientation(horizontal)size(Medium large)) ///msymbol(smcircle_hollow) ///mcolor(blue) legend(off)) ///(kdensity coef1)forvalue i=1/500{erase matchcity`i'.dta }///

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。