Stata连享会 主页 || 视频 || 推文 || 知乎 || Bilibili 站
温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。
New!
lianxh
命令发布了:
随时搜索推文、Stata 资源。安装:
. ssc install lianxh
详情参见帮助文件 (有惊喜):
. help lianxh
连享会新命令:cnssc
,ihelp
,rdbalance
,gitee
,installpkg
⛳ Stata 系列推文:
作者:郑宇 (南京大学)
邮箱:cn_vip@outlook.com
目录
双重差分 (Difference-in-Differences,DID) 方法是实证经济学和其他学科政策评估的主力军。其关键假设是控制组和处理组之间的趋势是平行的,即平行 (或共同) 趋势假设 (Parallel Trend Assumption,PTA)。在两组表现出潜在的不平行或随机趋势时,经典的 DID 方法的估计结果往往并不可靠。本推文将要介绍的主成分双重差分 (Principal Components Difference-in-Differences Estimators,PCDID) 方法在具有非平行或非平稳趋势的场景中的估计结果将更加可信。
与合成控制、无混淆和矩阵完成估计等现有方法不同,PCDID 使用因子增强回归来估计与处理效果相关的特定参数。具体而言,PCDID 执行以下操作:
该方法类似于控制函数方法,因为因子代理与控制函数起着相同的作用。通过使用整个样本周期进行估计,PCDID在减少有限样本偏差和提高渐近效率方面具有优势。下面,我们将主要介绍实现 PCDID 估计方法的 Stata 新命令 pcdid
的用法。
. *命令安装
. ssc install pcdid // 安装程序文件
. net get pcdid.pkg // 下载作者提供的数据文件到当前工作路径
如果网速太慢,可以使用 cnssc
命令下载:
. ssc install cnssc, replace
. cnssc install pcdid
. cnssc get pcdid.pkg
*命令语法
pcdid depvar treatvar [didvars] [indepvars] [if] [,options]
其中,主要变量为:
depvar
:被解释变量;treatvar
:控制/处理组指示变量 (=0, 1);[didvars]
:处理变量 (离散或连续) ;[indepvars]
:其他协变量。主要选项为:
alpha
:执行平行趋势阿尔法检验;fproxy(#)
:设置使用的因子数,若不设置,默认由递归因子数测试自动确定;stationary
:高级选项,假设所有因子在递归因子数测试中都不变;kmax(#)
:高级选项,设置递归因子数测试中的最大因子数,默认为10;treatlist(string)
:将处理组限制为字符串表达式中指定的;nwlag(#)
:在计算 NW 标准误时设置自相关的最大滞后阶数,默认为 int(T^0.25)
pdall
:计算预测控制组结果所需的系数。若未指定此选项,则之后的 pdd 命令会将所有预测的控制组结果设置为零。下面用一个包含如下变量的长面板数据来介绍下此命令用法:
*首先,在使用 pcdid 之前须将数据声明为面板数据
xtset id time
*自动确定因子数量的 PCDID 模型
pcdid y treated treated_post x1 x2
*生成上述 pcdid 命令预测的 yhat
pdd yhat
*生成包含反事实结果的 yhat0
replace treated_post=0
pdd yhat0
*例 1:使用 3 个因子代理执行 pcdid 估计并执行平行趋势 alpha 检验
pcdid y treated treated_post x1 x2, alpha fp(3)
*例 2:假设政策干预后至少 3 期才收效,设 _post2=1,否则=0
pcdid y treated treated_post treated_post2 x1 x2
*例 3:假设 id=1 是一个处理组,以下命令使用 NW 滞后 3 阶对该处理组进行 pcdid 基本估计
pcdid y treated treated_post x1 x2, tr(id==1) nwlag(3)
*例 4:为所有控制组和处理组生成预测结果和残差
pcdid y treated treated_post x1 x2, pdall
pdd yhat
gen resid = y - yhat
replace treated_post = 0 //生成假设没有接受处理的反事实结果
pdd yhat0
replace treated_post = 0 //生成假设没有接受处理且 x1=1 的反事实结果
replace x1 = 1
pdd yhat01
line y yhat yhat0 yhat01 time if id==1 //绘制 id==1 的结果
pcdid
将因子代理保存在名为 fproxy.dta 的单独数据文件中,须确保与其他数据集和变量没有命名冲突。pcdid
在 e()
中存储以下内容:
Scalars
e(Ne) number of treated units
e(Nc) number of control units
e(T) number of time periods
e(nobs) number of observations
e(factnum) number of factors used
e(factnum0) number of I(0) factors determined by the recursive procedure
e(factnum1) number of I(1) factors determined by the recursive procedure
e(alphastat) alpha statistic
e(alphastatse) alpha statistic standard error
e(alphastatz) alpha statistic z-score
e(alphastatp) alpha statistic p-value
e(kmax) maximum number of factors set by user
e(nwlag) maximum lag order for Newey-West standard error
e(treatlistnum) =0 if e(treatlist) is empty, =1 otherwise
Macros
e(cmd) "pcdid"
e(id) id variable in xtset
e(time) time variable in xtset
e(depvar) dependent variable
e(treatvar) control/treated unit indicator variable
e(indeps) treatment variable(s) and other covariates
e(treatlist) string expression specified by the treatlist option
Matrices
e(b) coefficient
e(V) variance
e(mata) unit-specific coefficients for the alpha test
e(matb) unit-specific coefficients (treated units) for the pcdid estimator
e(matc) unit-specific coefficients (control units; for predictions only)
e(bmgc) number of treated units used in computing each MG coefficient
pcdid
前,必须用 xtset
命令使数据被 Stata 识别为面板数据;
由于 pcdid
命令的帮助文件中并未给出案例,这里我们使用网上的公开数据进行演示。
. webuse parallelt, clear
. *保留三组中的一组数据,并定义面板结构
. keep id2 t2 treated2 y2 z1 z2
. xtset id2 t2
. *由于此数据不包含 treated,因此手动生成
. bys id2: egen temp=sum(treated2)
. ren treated2 treated_post
. g treated=1 if temp!=0
. replace treated=0 if treated==.
. *下面用此数据集演示上述 2.2 节中的例 4
. pcdid y2 treated treated_post z1 z2, pdall
PCDID: Principal Components Number of obs = 10000
Diff-in-Diff Regression Number of groups = 1000
(by CK, *PCDID Approach*) (Treated = 520)
(Control = 480)
Obs per group = 10
Method: Mean-Group (PCDID-MG) Number of factors used = 2
----------------------------------------------------------------------------
y2 | Coefficient Std. err. z P>|z| [95% conf. interval]
-------------+--------------------------------------------------------------
treated_post | 0.413 0.019 21.92 0.000 0.376 0.450
z1 | 0.846 0.018 47.48 0.000 0.811 0.880
z2 | -0.661 0.017 -38.47 0.000 -0.694 -0.627
fproxy1 | -2.346 2.059 -1.14 0.254 -6.382 1.689
fproxy2 | -1.153 1.260 -0.92 0.360 -3.623 1.317
_cons | 7.874 0.016 490.75 0.000 7.843 7.905
---------------------------------------------------------------------------
# of treated groups in computing the MG coefficient on treated_post = 520
Number of factors determined by a recursive procedure:
I(0) factors = 1
I(1) factors = 1
Maximum factors set by user = 10
. pdd yhat
. g resid = y2-yhat
. replace treated_post = 0
. pdd yhat0
. replace treated_post = 0
. replace z1 = 1
. pdd yhat01
. line y2 yhat yhat0 yhat01 t2 if id==1
平行 (或共同) 趋势假设是双重差分法应用的重要前提。而在处理组与对照组表现出潜在的非平行和随机趋势时,经典的 DID 方法的估计结果往往并不可靠。而我们本次介绍的 pcdid
在偏差和效率方面具有优势,正是用来解决这一问题。
Chan M K, Kwok S S. The PCDID approach: difference-in-differences when trends are potentially unparallel and stochastic[J]. Journal of Business & Economic Statistics, 2021: 1-18. -PDF-
Note:产生如下推文列表的 Stata 命令为:
lianxh did, m
安装最新版lianxh
命令:
ssc install lianxh, replace
免费公开课
最新课程-直播课
专题 | 嘉宾 | 直播/回看视频 |
---|---|---|
⭐ 最新专题 | 文本分析、机器学习、效率专题、生存分析等 | |
研究设计 | 连玉君 | 我的特斯拉-实证研究设计,-幻灯片- |
面板模型 | 连玉君 | 动态面板模型,-幻灯片- |
面板模型 | 连玉君 | 直击面板数据模型 [免费公开课,2小时] |
⛳ 课程主页
⛳ 课程主页
关于我们
课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法
等
连享会小程序:扫一扫,看推文,看视频……
扫码加入连享会微信群,提问交流更方便
✏ 连享会-常见问题解答:
✨ https://gitee.com/lianxh/Course/wikis
New!
lianxh
和songbl
命令发布了:
随时搜索连享会推文、Stata 资源,安装命令如下:
. ssc install lianxh
使用详情参见帮助文件 (有惊喜):
. help lianxh