stata一套多期did的代码,免费答疑群stata毕设答疑4群681893191
use data,clear
replace Time=. if Time==3333
rename Time pt
g Post=0
replace Post=1 if pt==2016&year>=2016
replace Post=1 if pt==2017&year>=2017
replace Post=1 if pt==2018&year>=2018
replace Post=1 if pt==2019&year>=2019
replace Post=1 if pt==2020&year>=2020
replace Post=1 if pt==2021&year>=2021
replace Post=1 if pt==2022&year>=2022
gen DID=Treat*Post
*cap drop missingv
*gen missingv=missing(TobinQ1,DID,TobinQ2,Size,Liquid,Lev,ATO,ROA,ListAge,Dual,TOP1)
*keep if missingv==0
global y "TobinQ1"
global y1 "TobinQ2"
global x "DID"
global m "ESG"
global control " Size Liquid Lev ATO ROA ListAge Dual Top1"
set matsize 5000
*描述性统计
outreg2 using msxtj.doc, replace sum(log) keep($y $x $control)
*相关性分析
logout, save(相关性分析) word replace: pwcorr_a ($y $x $control)
*多重共线性
reg $y $x $control
estat vif
logout,save(多重共线性检验) word replace: estat vif
*基准回归
eststo clear
eststo:reghdfe $y $x $control ,absorb(id year) vce(cluster id)
eststo:reghdfe $y1 $x $control ,absorb(id year) vce(cluster id)
esttab using 基准回归.rtf, replace b(3) r2 ar2 star(* 0.1 ** 0.05 *** 0.01) nogap title(基准回归分析)
*平行趋势检验
gen distance=year-pt
replace distance = -4 if distance <= -4
tab distance,missing
forvalues i=1/4{
gen d_`i' = 0
replace d_`i' = 1 if Treat== 1 & distance== -`i'
}
forvalues i=1/6 {
gen d`i' = 0
replace d`i' = 1 if Treat== 1 & distance== `i'
}
gen d0 = 0
replace d0 = 1 if Treat== 1 & distance== 0
reghdfe $y $control d_3 d_2 d_1 d0 d1 d2 d3 d4 d5 d6 ,absorb(id year) vce(cluster id)
graph set window fontface "Times New Roman"
graph set window fontfacesans "宋体"
coefplot, keep( d_3 d_2 d_1 d0 d1 d2 d3 d4 d5 d6) coeflabels( d_3="-3" d_2="-2" d_1="-1" d0="0" d1="1" d2="2" d3="3" d4="4" d5="5" d6="6" d7="7" d8="8" ) yline(0,lcolor(edkblue*0.8)) ylabel(0(0.5)1.5,labsize(medsmall) format(%02.1f)) vertical addplot(line @b @at) ytitle("{stSans:系}" "{stSans:数}", size(medsmall) orientation(h)) xtitle("{stSans:绿色债券的平行趋势检验}", size(medsmall)) yline(0) levels(90) scheme(s2color) ciopts(recast(rcap) lpattern(dash)) graphregion(fcolor(none))
*安慰剂检验
use data,clear
cap erase "simulations.dta"
permute DID beta=_b[DID] se=_se[DID] df=e(df_r),reps(500) seed(123) saving("simulations.dta"):reghdfe $y $x $control ,absorb(id year) vce(cluster id)
use "simulations.dta",clear
gen t_value=beta/se
gen p_value=2*ttail(df,abs(beta/se))
dpplot beta,xtitle("Estimator",size(*0.8)) xlabel(,format(%4.3f) labsize(small)) ytitle("Density",size(*0.8)) ylabel(,nogrid format(%4.3f) labsize(small)) note("") caption("") graphregion(fcolor(white))
*安慰剂检验
mat b = J(1000,1,0)
mat se = J(1000,1,0)
mat p = J(1000,1,0)
forvalues i=1/1000{
use data.dta, clear
qui xtset id year
keep if DID==1
sample 1, count by(id)
keep id year
rename year policy_year
save match_id_year.dta, replace
merge 1:m id using data.dta
qui xtset id year
gen treatment = (_merge == 3)
gen period = (year >= policy_year)
gen did = treatment*period
*append using cao_control.dta
reghdfe $y did $control,absorb(id year) vce(cluster id)
mat b[`i',1] = _b[did]
mat se[`i',1] = _se[did]
mat p[`i',1] = 2*ttail(e(df_r), abs(_b[did]/_se[did]))
}
* 矩阵转化为向量
svmat b, names(coef)
svmat se, names(se)
svmat p, names(pvalue)
* 删除空值并添加标签
drop if pvalue1 == .
label var pvalue1 p值
label var coef1 估计系数
keep coef1 se1 pvalue1
gen tvalue = coef1/se1
save placebo.dta, replace
kdensity coef1, normal scheme(qleanmono)
use data,clear
g Post_1=0
replace Post_1=1 if pt==2016&year>=2015
replace Post_1=1 if pt==2017&year>=2016
replace Post_1=1 if pt==2018&year>=2017
replace Post_1=1 if pt==2019&year>=2018
replace Post_1=1 if pt==2020&year>=2019
replace Post_1=1 if pt==2021&year>=2020
replace Post_1=1 if pt==2022&year>=2021
gen DID_1=Treat*Post_1
g Post1=0
replace Post1=1 if pt==2016&year>=2017
replace Post1=1 if pt==2017&year>=2018
replace Post1=1 if pt==2018&year>=2019
replace Post1=1 if pt==2019&year>=2020
replace Post1=1 if pt==2020&year>=2021
replace Post1=1 if pt==2021&year>=2022
replace Post1=1 if pt==2022&year>=2023
gen DID1=Treat*Post1
eststo clear
psmatch2 $x $control,outcome($y) n(1)
eststo :reghdfe $y DID_1 $control ,absorb(id year) vce(cluster id)
eststo :reghdfe $y DID1 $control,absorb(id year) vce(cluster id)
esttab using 稳健性检验.rtf, replace b(3) r2 ar2 star(* 0.1 ** 0.05 *** 0.01) nogap
*中介
eststo clear
eststo:reghdfe $y $x $control ,absorb(id year) vce(cluster id)
eststo:reghdfe $m $x $control ,absorb(id year) vce(cluster id)
eststo:reghdfe $y $x $m $control ,absorb(id year) vce(cluster id)
esttab using 中介.rtf, replace b(3) r2 ar2 star(* 0.1 ** 0.05 *** 0.01) nogap
*异质性
eststo clear
eststo:reghdfe $y $x $control if SOE==0,absorb(id year) vce(cluster id)
eststo:reghdfe $y $x $control if SOE==1,absorb(id year) vce(cluster id)
esttab using 异质性.rtf, replace b(3) r2 ar2 star(* 0.1 ** 0.05 *** 0.01) nogap
|