多期DID之安慰剂检验、平行趋势检验

发布时间:2020-07-03 阅读 78261

Stata 连享会   主页 || 视频 || 推文

温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。

课程详情 https://gitee.com/arlionn/Course   |   lianxh.cn

课程主页 https://gitee.com/arlionn/Course

作者:朱学贵 (合肥工业大学)
E-Mail: xgzhuctb@163.com


目录


本文介绍多期 DID 中安慰剂检验的实现步骤。

DID 相关推文:连享会 - DID 倍分法专题

在传统DID模型中,所有单位的政策时间一致,安慰剂检验只需在所有单位中随机抽取固定数量的若干单位作为实验组便可。但是,在多期DID中每个单位的政策时间不同,该种方法便不再适用。

解决办法就是:为每个样本对象随机抽取样本期作为其政策时间。比如,本文中提供了我国30个省2000-2018年的数据,在多期DID中就需要为这30个省中每个省随机抽取2000-2018中的某一个年份作为它的政策时间。

首先,让我们来看一下原始政策时间下的多期DID估计情况:

多期DID估计

cd ×××××××××
use 数据0.dta, clear
xtset id year
* 生成单位时间处理变量
gen DT =  ( ///
    (id == 1  & year >= 2005) | /// 
    (id == 2  & year >= 2005) | /// 
    (id == 3  & year >= 2006) | /// 
    (id == 4  & year >= 2006) | /// 
    (id == 5  & year >= 2006) | /// 
    (id == 6  & year >= 2006) | /// 
    (id == 7  & year >= 2006) | /// 
    (id == 8  & year >= 2006) | /// 
    (id == 9  & year >= 2005) | /// 
    (id == 10 & year >= 2003) | /// 
    (id == 11 & year >= 2004) | /// 
    (id == 12 & year >= 2006) | /// 
    (id == 13 & year >= 2006) | /// 
    (id == 14 & year >= 2006) | /// 
    (id == 15 & year >= 2005) | /// 
    (id == 16 & year >= 2006) | /// 
    (id == 17 & year >= 2006) | /// 
    (id == 18 & year >= 2006) | /// 
    (id == 19 & year >= 2002) | /// 
    (id == 20 & year >= 2006) | /// 
    (id == 21 & year >= 2003) | /// 
    (id == 22 & year >= 2006) | /// 
    (id == 23 & year >= 2006) | /// 
    (id == 24 & year >= 2006) | /// 
    (id == 25 & year >= 2006) | /// 
    (id == 27 & year >= 2005) | /// 
    (id == 28 & year >= 2006) | /// 
    (id == 29 & year >= 2006) | /// 
    (id == 30 & year >= 2006))

多期DID估计:

xtreg y DT x1-x6, fe

结果:


Fixed-effects (within) regression               Number of obs     =        570
Group variable: id                              Number of groups  =         30

R-sq:                                           Obs per group:
     within  = 0.5624                                         min =         19
     between = 0.0578                                         avg =       19.0
     overall = 0.2567                                         max =         19

                                                F(7,533)          =      97.87
corr(u_i, Xb)  = -0.3584                        Prob > F          =     0.0000

------------------------------------------------------------------------------
           y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          DT |   .0792811   .0104967     7.55   0.000     .0586611     .099901
          x1 |   .7772515   .4184583     1.86   0.064    -.0447783    1.599281
          x2 |   .0796133   .0644417     1.24   0.217    -.0469776    .2062043
          x3 |   .2698871   .0823515     3.28   0.001     .1081137    .4316605
          x4 |   .1934262   .1931832     1.00   0.317    -.1860676    .5729201
          x5 |  -.7536294   .2024724    -3.72   0.000    -1.151371   -.3558877
          x6 |  -1.154312   .2885104    -4.00   0.000    -1.721069   -.5875551
       _cons |   .8809552   .0650585    13.54   0.000     .7531526    1.008758
-------------+----------------------------------------------------------------
     sigma_u |  .10451545
     sigma_e |  .06805517
         rho |  .70224943   (fraction of variance due to u_i)
------------------------------------------------------------------------------
F test that all u_i=0: F(29, 533) = 26.25                    Prob > F = 0.0000

平行趋势检验

* 平行趋势检验
gen current =  ((id == 1 & year == 2005) | /// 
    (id == 2  & year == 2005) | /// 
    (id == 3  & year == 2006) | /// 
    (id == 4  & year == 2006) | /// 
    (id == 5  & year == 2006) | /// 
    (id == 6  & year == 2006) | /// 
    (id == 7  & year == 2006) | /// 
    (id == 8  & year == 2006) | /// 
    (id == 9  & year == 2005) | /// 
    (id == 10 & year == 2003) | /// 
    (id == 11 & year == 2004) | /// 
    (id == 12 & year == 2006) | /// 
    (id == 13 & year == 2006) | /// 
    (id == 14 & year == 2006) | /// 
    (id == 15 & year == 2005) | /// 
    (id == 16 & year == 2006) | /// 
    (id == 17 & year == 2006) | /// 
    (id == 18 & year == 2006) | /// 
    (id == 19 & year == 2002) | /// 
    (id == 20 & year == 2006) | /// 
    (id == 21 & year == 2003) | /// 
    (id == 22 & year == 2006) | /// 
    (id == 23 & year == 2006) | /// 
    (id == 24 & year == 2006) | /// 
    (id == 25 & year == 2006) | /// 
    (id == 27 & year == 2005) | /// 
    (id == 28 & year == 2006) | /// 
    (id == 29 & year == 2006) | /// 
    (id == 30 & year == 2006))

gen pre6 = f6.current
gen pre5 = f5.current
gen pre4 = f4.current
gen pre3 = f3.current
gen pre2 = f2.current
gen pre1 = f.current
gen post1 = l.current
gen post2 = l2.current
gen post3 = l3.current
gen post4 = l4.current
gen post5 = l5.current
gen post6 = l6.current
gen post7 = l7.current

replace pre1 = 0 if pre1 == .
replace pre2 = 0 if pre2 == .
replace pre3 = 0 if pre3 == .
replace pre4 = 0 if pre4 == .
replace pre5 = 0 if pre5 == .
replace pre6 = 0 if pre6 == .
replace post1 = 0 if post1 == .
replace post2 = 0 if post2 == .
replace post3 = 0 if post3 == .
replace post4 = 0 if post4 == .
replace post5 = 0 if post5 == .
replace post6 = 0 if post6 == .
replace post7 = 0 if post7 == .


xtreg y pre3 pre2 pre1 current post1 post2 post3 post4 post5 post6 post7 x1-x6, fe

coefplot, keep(pre3 pre2 pre1 current post1 post2 post3 post4 post5 post6 post7) vertical addplot(line @b @at) yline(0) levels(95)
平行趋势检验
平行趋势检验

安慰剂检验

生成备用矩阵

mat b = J(500,1,0)
mat se = J(500,1,0)
mat p = J(500,1,0)

抽样过程-方案1:
在变量year中随机抽取30个数据依次作为这30个省份的政策时间

forvalues i=1/500{
	use 数据0.dta, clear
	xtset id year
	sample 30, count
	keep year
	mkmat year, matrix(sampleyear) //向量转化为矩阵,方便调用
	use 数据.dta,clear
	xtset id year
	gen treat = 0
  * 生成单位时间处理变量
	foreach j of numlist 1/30 {
		replace trea = 1 if (id == `j' & year >= sampleyear[`j',1])
	}
	qui xtreg y treat x1-x6, fe
  * 存储并计算所需回归结果
	mat b[`i',1] = _b[treat]
	mat se[`i',1] = _se[treat]
	mat p[`i',1] = 2*ttail(e(df_r),abs(_b[treat]/_se[treat]))
}

抽样过程-方案2:
与方案1不同,这里首先将数据按照省份分组,然后在每个省份组内的year变量中随机抽取一个年份作为其政策时间。该种方法更为合理,推荐使用。

forvalues i = 1/500{
	use data.dta, clear
	xtset id Year
	bsample 1, strata(id) //根据**id**分组,每组随机抽取一个年份
	keep Year
	save matchyear.dta, replace
	mkmat Year, matrix(sampleyear)
	use data.dta, clear
	xtset id Year
	gen DID = 0
	foreach j of numlist 1/25 {
		replace DID = 1 if (id == `j' & Year >= sampleyear[`j',1])
	}
	qui xtreg IS DID ALED FIN2 FDI AFIN, re
	mat b[`i',1] = _b[DID]
	mat se[`i',1] = _se[DID]
	scalar df_r = e(N) - e(df_m) -1
	mat p[`i',1] = 2*ttail(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 估计系数

twoway (scatter pvalue1 coef1, xline(0 -0.03, lwidth(0.2) lp(shortdash)) xlabel(-0.05(0.01)0.1, grid) xtitle(估计系数) ytitle(p值) msymbol(smcircle_hollow) mcolor(orange) legend(off)) (kdensity coef1, title(安慰剂检验))
安慰剂检验
安慰剂检验


编者注: 本文来自「计量工作室」公众号,已经获得作者授权。文中的数据可以关注公众号后,后台回复 20200628 获取。

相关课程

连享会-直播课 上线了!
http://lianxh.duanshu.com

免费公开课:


课程一览

支持回看,所有课程可以随时购买观看。

专题 嘉宾 直播/回看视频
最新专题 DSGE, 因果推断, 空间计量等
Stata数据清洗 游万海 直播, 2 小时,已上线
研究设计 连玉君 我的特斯拉-实证研究设计-幻灯片-
面板模型 连玉君 动态面板模型-幻灯片-
面板模型 连玉君 直击面板数据模型 [免费公开课,2小时]

Note: 部分课程的资料,PPT 等可以前往 连享会-直播课 主页查看,下载。


关于我们

  • Stata连享会 由中山大学连玉君老师团队创办,定期分享实证分析经验。直播间 有很多视频课程,可以随时观看。
  • 连享会-主页知乎专栏,300+ 推文,实证分析不再抓狂。
  • 公众号推文分类: 计量专题 | 分类推文 | 资源工具。推文分成 内生性 | 空间计量 | 时序面板 | 结果输出 | 交乘调节 五类,主流方法介绍一目了然:DID, RDD, IV, GMM, FE, Probit 等。
  • 公众号关键词搜索/回复 功能已经上线。大家可以在公众号左下角点击键盘图标,输入简要关键词,以便快速呈现历史推文,获取工具软件和数据下载。常见关键词:课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法

连享会主页  lianxh.cn
连享会主页 lianxh.cn

连享会小程序:扫一扫,看推文,看视频……

扫码加入连享会微信群,提问交流更方便

✏ 连享会学习群-常见问题解答汇总:
https://gitee.com/arlionn/WD