查看原文
其他

DID双重差分中安慰剂检验思路及绘图的操作代码分享!

计量经济圈 计量经济圈 2022-08-29

凡是搞计量经济的,都关注这个号了

稿件:econometrics666@126.com

所有计量经济圈方法论丛的code程序, 宏微观数据库和各种软件都放在社群里.欢迎到计量经济圈社群交流访问.

继①134篇各种方法的code, 代码和程序文章合集, 必须收藏!,②今年最诚意的主流计量方法与Stata操作的视频教程, 一定要收藏学习!,《经济研究》期刊上所有文章按照"计量方法"进行分类汇总,有选择性地学习计量方法,④120篇DID双重差分方法的文章合集, 包括代码,程序及解读, 建议收藏!,我们这一期专门分享一下使用DID方法时常见的安慰剂检验思路和操作代码。当然,对于安慰剂检验,我们已经分享过相关文章,也提供过相关代码,只不过这一次我们针对DID安慰剂检验分享专门的操作代码,供各位中青年学者研究和学习。


下面这篇JDE文章研究的是省直管县对这些县域经济增长的影响。我们截取了作者使用DID双重差分方法过程中关于安慰剂检验的部分以及操作代码,而且还给出了相应的数据予以复制其中的Figure。
Pei Li, Yi Lu, Jin Wang, 2016, Does flattening government improve economic performance? Evidence from China, Journal of Development Economics, 123, 18-37.
This paper examines a causal relationship between the flattening of a government hierarchy and economic performance by exploiting a panel data set on government reorganization in China from 1995 to 2012. Delayering has led to increases in revenue and inter-governmental transfers for county governments, but the associated enlarged span of control makes it difficult for upper-level governments to coordinate and monitor local ones. This has led to a reduction in county governments' total public expenditure and pro-growth expenditure, as well as an increase in land corruption. Overall, the flattening of the government hierarchy has a negative effect on economic performance.

 Figure 4:  Placebo PMC Status  

forvalues i=1(1)500 {
use placebo,clear
save pmc_data,replace
use pmc_data,clear
keep county year
drop if year==2012|year==1995
save dummy_data,replace
use dummy_data,clear

create the false dataset
bysort year: keep if _n==1
sample 1, count
keep year
save temp,replace
use dummy_data,clear
merge m:1 year using temp
keep if _merge==3
drop _merge
sample 58, count
keep year county
rename year pmc_year
save pmc_time,replace
keep county
save pmc_drop,replace

update new dummy_data file
use dummy_data, clear
merge m:1 year using temp
keep if _merge==1
drop _merge
merge m:1 county using pmc_drop
keep if _merge==1
drop _merge
save dummy_data,replace

update new temp file
bysort year: keep if _n==1
sample 1, count
keep year
save temp,replace

update new pmc_time file
use dummy_data,clear
merge m:1 year using temp
keep if _merge==3
drop _merge
sample 113, count
keep year county
rename year pmc_year
save pmc_temp,replace
append using pmc_time
save pmc_time,replace
use pmc_temp
keep county
save pmc_drop,replace
use dummy_data, clear
merge m:1 year using temp
keep if _merge==1
drop _merge
merge m:1 county using pmc_drop
keep if _merge==1
drop _merge
save dummy_data,replace

update new temp file
bysort year: keep if _n==1
sample 1, count
keep year
save temp,replace

update new pmc_time file
use dummy_data,clear
merge m:1 year using temp
keep if _merge==3
drop _merge
sample 75, count
keep year county
rename year pmc_year
save pmc_temp,replace
append using pmc_time
save pmc_time,replace
use pmc_temp
keep county
save pmc_drop,replace
use dummy_data, clear
merge m:1 year using temp
keep if _merge==1
drop _merge
merge m:1 county using pmc_drop
keep if _merge==1
drop _merge
save dummy_data,replace

update new temp file
bysort year: keep if _n==1
sample 1, count
keep year
save temp,replace

update new pmc_time file
use dummy_data,clear
merge m:1 year using temp
keep if _merge==3
drop _merge
sample 15, count
keep year county
rename year pmc_year
save pmc_temp,replace
append using pmc_time
save pmc_time,replace
use pmc_temp
keep county
save pmc_drop,replace
use dummy_data, clear
merge m:1 year using temp
keep if _merge==1
drop _merge
merge m:1 county using pmc_drop
keep if _merge==1
drop _merge
save dummy_data,replace

update new temp file
bysort year: keep if _n==1
sample 1, count
keep year
save temp,replace

update new pmc_time file
use dummy_data,clear
merge m:1 year using temp
keep if _merge==3
drop _merge
sample 254, count
keep year county
rename year pmc_year
save pmc_temp,replace
append using pmc_time
save pmc_time,replace
use pmc_temp
keep county
save pmc_drop,replace
use dummy_data, clear
merge m:1 year using temp
keep if _merge==1
drop _merge
merge m:1 county using pmc_drop
keep if _merge==1
drop _merge
save dummy_data,replace

update new temp file
bysort year: keep if _n==1
sample 1, count
keep year
save temp,replace

update new pmc_time file
use dummy_data,clear
merge m:1 year using temp
keep if _merge==3
drop _merge
sample 201, count
keep year county
rename year pmc_year
save pmc_temp,replace
append using pmc_time
save pmc_time,replace
use pmc_temp
keep county
save pmc_drop,replace
use dummy_data, clear
merge m:1 year using temp
keep if _merge==1
drop _merge
merge m:1 county using pmc_drop
keep if _merge==1
drop _merge
save dummy_data,replace

update new temp file
bysort year: keep if _n==1
sample 1, count
keep year
save temp,replace

update new pmc_time file
use dummy_data,clear
merge m:1 year using temp
keep if _merge==3
drop _merge
sample 153, count
keep year county
rename year pmc_year
save pmc_temp,replace
append using pmc_time
save pmc_time,replace
use pmc_temp
keep county
save pmc_drop,replace

update new dummy_data file
use dummy_data, clear
merge m:1 year using temp
keep if _merge==1
drop _merge
merge m:1 county using pmc_drop
keep if _merge==1
drop _merge
save dummy_data,replace

update new temp file
bysort year: keep if _n==1
sample 1, count
keep year
save temp,replace

update new pmc_time file
use dummy_data,clear
merge m:1 year using temp
keep if _merge==3
drop _merge
sample 87, count
keep year county
rename year pmc_year
save pmc_temp,replace
append using pmc_time
save pmc_time,replace
use pmc_temp
keep county
save pmc_drop,replace
use dummy_data, clear
merge m:1 year using temp
keep if _merge==1
drop _merge
merge m:1 county using pmc_drop
keep if _merge==1
drop _merge
save dummy_data,replace

Generate the PMC variable
use pmc_time
merge m:m county using pmc_data
drop _merge
replace pmc_year=2013 if pmc_year==.
gen false_pmc=(pmc_year-year<=0)
save pmc_data,replace

Generate the treatment variable
use pmc_time
keep county
save pmc_county,replace
use pmc_data
merge m:1 county using pmc_county 
gen treatment=(_merge==3)
drop _merge
save pmc_data,replace

Regression
gen ly=log(gdp_percapita)
gen t=year-1994
gen treatment_trend=treatment*t
xi: areg ly false_pmc treatment_trend cpe i.year i.year*county_city i.year*poor_county i.year*food_county i.year*provboundary_county i.year*slope i.year*altitude i.year*urban_rate00 i.year*fiscal_gap99,absorb(county) cluster(county)
parmest,format (estimate min95 max95 %8.2f p %8.3f) saving("temp.dta", replace)
use "temp.dta", clear
keep if parm=="false_pmc"
append using "pmc_simulations.dta"
save "pmc_simulations.dta", replace
}
erase "temp.dta"
use "pmc_simulations.dta", clear
drop if estimate==.
save "pmc_simulations.dta", replace

Graphs

Density Plot
dpplot estimate,xline(-0.039) xtitle("PMC effect estimate") saving("PDF_PMC_dpplot.gph", replace)
这篇文章的数据、代码都可以下载,能够较为完整地复制出文章中的结果,各位学者可以自行前往文章主页下载学习,而社群群友可以到社群自行下载即可。


借鉴上面这篇文章的范式(在方法上和实证设计上借鉴了上面这篇JDE文章),下面这篇中文文章的安慰剂检验及操作代码。

卢盛峰,董如玉,叶初升.“一带一路”倡议促进了中国高质量出口吗——来自微观企业的证据[J].中国工业经济,2021(03):80-98.
"一带一路"倡议促进了沿线国家和地区之间的互联互通,也推动了中国外贸出口的高速增长。然而,这一出口增长源自粗放式数量驱动还是高附加值产品质量驱动,即"一带一路"倡议是否促进了中国高质量出口?针对这一问题的研究将有利于进一步厘清"一带一路"倡议推动中国出口增长的内在动力。本文基于2010—2015年中国海关出口统计数据,通过构建双重差分模型评估了"一带一路"倡议对中国企业出口质量的因果效应,并识别了中间的潜在影响机制。研究结果表明:(1)"一带一路"倡议显著提高了国内沿线城市企业出口产品的平均质量,即显著促进了中国企业的高质量出口;(2)考虑内生性问题、样本选择性偏误及进一步安慰剂检验的结果都表明上述结论具有高度稳健性;(3)在影响机制方面,"一带一路"倡议改善了沿线城市的政策环境,提高了地方政府对企业创新的政策支持力度,最终促进了中国企业的高质量出口。本文的研究对于进一步理解中国"一带一路"倡议的经济效果,以及对中国出口高质量发展的政策实践都具有借鉴意义。


图2:安慰剂检验
cd C:\Users\user\Desktop\数据\placebo
set more off
forvalues i=1(1)500 {
use dodata,clear
save ydyl_data,replace
use ydyl_data,clear
keep citycode _日期

bysort citycode _日期: gen temp_code=_n
keep if temp_code==1
drop temp_code

drop if _日期==2010
save dummy_data,replace
use dummy_data,clear

create the false dataset
bysort _日期: keep if _n==1
sample 1, count
keep _日期
save temp,replace
use dummy_data,clear
merge m:1 _日期 using temp
keep if _merge==3
drop _merge
sample 36, count
keep _日期 citycode
rename _日期 ydyl_日期
save ydyl_time,replace

Generate the false_ydyl_after variable
use ydyl_time
merge 1:m citycode using ydyl_data
gen false_citycode=(_merge==3)
drop _merge
egen tt=mean(ydyl_日期)
replace ydyl_日期=tt
drop tt
gen false_after=(_日期-ydyl_日期>=0)
gen false_ydyl_after=false_citycode*false_after
save ydyl_data,replace

Regression

reghdfe ln_quality false_ydyl_after Capital_City_t Yangtz_City_t ratio_fiscal_t ratio_gdp_t lnstudent_t ratio_city_t Capital_City_t2 Yangtz_City_t2 ratio_fiscal_t2 ratio_gdp_t2 lnstudent_t2 ratio_city_t2 Capital_City_t3 Yangtz_City_t3 ratio_fiscal_t3 ratio_gdp_t3 lnstudent_t3 ratio_city_t3 , absorb(i._日期 _企业编码) vce(cluster province)

parmest,format (estimate min95 max95 %8.2f p %8.3f) saving("temp.dta", replace)
use "temp.dta", clear
keep if parm=="false_ydyl_after"
append using "ydyl_simulations.dta"
save "ydyl_simulations.dta", replace
}
erase "temp.dta"
use "ydyl_simulations.dta", clear
drop if estimate==.
save "ydyl_simulations.dta", replace

Graphs

Density Plot
dpplot estimate,xline(0.129) xtitle("虚假估计系数") ytitle("估计系数密度分布")  saving("PDF_ydyl_dpplot.gph", replace)
这篇文章的代码可以下载(不过数据是缺失的),各位学者可以自行前往文章主页下载学习,而社群群友可以到社群自行下载即可。

陈钊,申洋.限购政策的空间溢出与土地资源配置效率[J].经济研究,2021,56(06):93-109.


下面这些短链接文章属于合集,可以收藏起来阅读,不然以后都找不到了。

2.5年,计量经济圈近1000篇不重类计量文章,

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

Econometrics Circle




数据系列空间矩阵 | 工企数据 | PM2.5 | 市场化指数 | CO2数据 |  夜间灯光 | 官员方言  | 微观数据 | 内部数据计量系列匹配方法 | 内生性 | 工具变量 | DID | 面板数据 | 常用TOOL | 中介调节 | 时间序列 | RDD断点 | 合成控制 | 200篇合辑 | 因果识别 | 社会网络 | 空间DID数据处理Stata | R | Python | 缺失值 | CHIP/ CHNS/CHARLS/CFPS/CGSS等 |干货系列能源环境 | 效率研究 | 空间计量 | 国际经贸 | 计量软件 | 商科研究 | 机器学习 | SSCI | CSSCI | SSCI查询 | 名家经验计量经济圈组织了一个计量社群,有如下特征:热情互助最多前沿趋势最多、社科资料最多、社科数据最多、科研牛人最多、海外名校最多。因此,建议积极进取和有强烈研习激情的中青年学者到社群交流探讨,始终坚信优秀是通过感染优秀而互相成就彼此的。


您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存