👇 连享会 · 推文导航 | www.lianxh.cn
🍎 Stata:Stata基础 | Stata绘图 | Stata程序 | Stata新命令 📘 论文:数据处理 | 结果输出 | 论文写作 | 数据分享 💹 计量:回归分析 | 交乘项-调节 | IV-GMM | 时间序列 | 面板数据 | 空间计量 | Probit-Logit | 分位数回归 ⛳ 专题:SFA-DEA | 生存分析 | 爬虫 | 机器学习 | 文本分析 🔃 因果:DID | RDD | 因果推断 | 合成控制法 | PSM-Matching 🔨 工具:工具软件 | Markdown | Python-R-Stata 🎧 课程:最新专题 | 计量专题 | 关于连享会
🍓 课程推荐:连享会:2025 寒假前沿班
嘉宾:杨海生,中山大学
时间:2025 年 1 月 13-24 日
咨询:王老师 18903405450(微信)
作者:杜静玄 (雪城大学)
邮箱:jdu115@syr.edu
温馨提示: 文中链接在微信中无法生效。请点击底部「阅读原文」。或直接长按/扫描如下二维码,直达原文:
编者按:本文整理自 Two-Way FE: The come back to DID,特此致谢!
目录
1. 简介
2. TWFE 与 TWM 的等价性
3. 在 DID 设定下的应用
4. Stata 应用
5. 参考文献
6. 相关推文
1. 简介
Wooldridge 证明了双向固定效应 (TWFE) 与包括单位时间均值和时间段特定横截面平均值的混合 OLS (他称之为双向 Mundlak (TWM) 回归) 的结果具有等价性。该等价意味着异质趋势的标准策略可用于放宽共同趋势假设,尤其对渐进型 DID 有效。此外,双向 Mundlak 回归也很容易适应非线性模型,例如指数模型和 logit 和 probit 模型。Stata 的命令 jwdid
提供了基于 TWM 法的估计结果。
2. TWFE 与 TWM 的等价性
其中, 是 的向量, 是个体固定效应, 为时间固定效应。考虑双向 Mundlak 回归:
混合 OLS 使用 对 进行回归,得到 的系数 。令 ,若 是非奇异矩阵,,可以利用 定理证明。若 ,则 ,其中 是时间不变的变量, 为单位恒定的变量。
在交错实施的设定中 (staggered intervention),TWFE 需要包括: 来估计处理效应 。其中 是处理组指示变量, 是时间变量。在 TWFE 中需要包括协变量的交互变量 ,,在 Mundlak 里需要包括 ,在 TWM 中则需要进一步包括 。同时,混合 OLS 以及 RE 版本的 Mundalk 也是等价的。
3. 在 DID 设定下的应用
传统 TWFE 对于估计 ATT 存在较大限制,而关键是缺乏对于异质性效应的处理。Wooldridge 提出的方法与 Sun and Abraham (2020) 的类似,但和他们将队列与动态效应交互不同,Wooldridge 将队列效应与时间固定效应交互。同时,从该方法中获得的估计量与用插补法获得的第一阶段的相同,并且同插补法相比,它的标准误更容易获得。具体来说,应当估计以下二式之一:
Wooldridge 建议,除了加入个体 (或队列) 以及时间固定效应外,还应该将所有可能的队列和时间效应都加入。而以上两个式子则分别代表:选择从未处理组作为控制,或者选择尚未处理组作为控制。基于该饱和模型,估计出的 与 Callaway and Sant’Anna (2022) 的 ATT 是一样的。
4. Stata 应用
首先安装命令 ssc install jwdid, replace
。该命令由 Fernando Rios-Avila 教授根据 Wooldridge 的文章编写,包含了含协变量的回归。他还提供了如何得到与 Callaway and Sant'Anna (2021) 相似估计量的建议。其中,jwdid
命令采用 TWFE 方法且使用从未接受处理组为选项,jwdid_estat
命令可用于得到一系列标准化加总的结果,一般在估计之后实现。
该命令与 csdid
很相似,但需要使用者加入个体固定效应 ivar
,年份固定效应 tvar
与群体固定效应 gvar
。回归结果如下:
. lxhuse mpdta, clear
. jwdid lemp, ivar(countyreal) tvar(year) gvar(first_treat)
HDFE Linear regression Number of obs = 2,500
Absorbing 2 HDFE groups F( 7, 499) = 3.82
Statistics robust to heteroskedasticity Prob > F = 0.0005
R-squared = 0.9933
Adj R-squared = 0.9915
Within R-sq. = 0.0101
Number of clusters (countyreal) = 500 Root MSE = 0.1389
(Std. err. adjusted for 500 clusters in countyreal)
-------------------------------------------------------------------------------------------
| Robust
lemp | Coefficient std. err. t P>|t| [95% conf. interval]
--------------------------+----------------------------------------------------------------
first_treat#year#c.__tr__ |
2004 2004 | -0.019 0.022 -0.87 0.387 -0.063 0.025
2004 2005 | -0.078 0.030 -2.57 0.010 -0.138 -0.018
2004 2006 | -0.136 0.035 -3.84 0.000 -0.206 -0.066
2004 2007 | -0.105 0.034 -3.09 0.002 -0.171 -0.038
2006 2006 | 0.003 0.020 0.13 0.900 -0.037 0.042
2006 2007 | -0.039 0.024 -1.63 0.103 -0.086 0.008
2007 2007 | -0.043 0.018 -2.34 0.020 -0.079 -0.007
|
_cons | 5.778 0.002 3742.17 0.000 5.775 5.781
-------------------------------------------------------------------------------------------
Absorbed degrees of freedom:
-----------------------------------------------------+
Absorbed FE | Categories - Redundant = Num. Coefs |
-------------+---------------------------------------|
countyreal | 500 500 0 *|
year | 5 0 5 |
-----------------------------------------------------+
* = FE nested within cluster; treated as redundant for DoF computation
每个系数都对应于组别 G 在时间 T 时的处理效应。然而与 csdid
相反的,这是由于使用尚未处理组作为控制组造成的。也可以使用从未处理组作为控制组,即在命令后加入 never
:
. jwdid lemp, ivar(countyreal) tvar(year) gvar(first_treat) group never
HDFE Linear regression Number of obs = 2,500
Absorbing 2 HDFE groups F( 12, 499) = 2.87
Statistics robust to heteroskedasticity Prob > F = 0.0008
R-squared = 0.0288
Adj R-squared = 0.0213
Within R-sq. = 0.0001
Number of clusters (countyreal) = 500 Root MSE = 1.4926
(Std. err. adjusted for 500 clusters in countyreal)
-------------------------------------------------------------------------------------------
| Robust
lemp | Coefficient std. err. t P>|t| [95% conf. interval]
--------------------------+----------------------------------------------------------------
first_treat#year#c.__tr__ |
2004 2004 | -0.011 0.023 -0.45 0.653 -0.056 0.035
2004 2005 | -0.070 0.031 -2.26 0.024 -0.132 -0.009
2004 2006 | -0.137 0.037 -3.75 0.000 -0.209 -0.065
2004 2007 | -0.101 0.035 -2.92 0.004 -0.169 -0.033
2006 2004 | 0.007 0.023 0.28 0.781 -0.040 0.053
2006 2005 | 0.004 0.031 0.12 0.905 -0.058 0.066
2006 2006 | -0.001 0.034 -0.02 0.981 -0.067 0.066
2006 2007 | -0.037 0.036 -1.04 0.297 -0.108 0.033
2007 2004 | 0.031 0.015 2.02 0.044 0.001 0.060
2007 2005 | 0.028 0.020 1.41 0.158 -0.011 0.066
2007 2006 | -0.003 0.025 -0.13 0.893 -0.052 0.045
2007 2007 | -0.029 0.027 -1.11 0.270 -0.082 0.023
|
_cons | 5.774 0.067 86.15 0.000 5.642 5.906
-------------------------------------------------------------------------------------------
Absorbed degrees of freedom:
-----------------------------------------------------+
Absorbed FE | Categories - Redundant = Num. Coefs |
-------------+---------------------------------------|
first_treat | 4 0 4 |
year | 5 1 4 |
-----------------------------------------------------+
该命令也可加入控制变量,以 lpop
为例:
. jwdid lemp lpop, ivar(countyreal) tvar(year) gvar(first_treat) group
HDFE Linear regression Number of obs = 2,500
Absorbing 2 HDFE groups F( 22, 499) = 364.06
Statistics robust to heteroskedasticity Prob > F = 0.0000
R-squared = 0.8732
Adj R-squared = 0.8717
Within R-sq. = 0.8695
Number of clusters (countyreal) = 500 Root MSE = 0.5404
(Std. err. adjusted for 500 clusters in countyreal)
-----------------------------------------------------------------------------------------------------
| Robust
lemp | Coefficient std. err. t P>|t| [95% conf. interval]
------------------------------------+----------------------------------------------------------------
first_treat#year#c.__tr__ |
2004 2004 | -0.021 0.022 -0.98 0.329 -0.064 0.021
2004 2005 | -0.082 0.027 -2.99 0.003 -0.136 -0.028
2004 2006 | -0.138 0.031 -4.48 0.000 -0.198 -0.077
2004 2007 | -0.110 0.032 -3.39 0.001 -0.173 -0.046
2006 2006 | 0.003 0.019 0.13 0.893 -0.035 0.040
2006 2007 | -0.045 0.022 -2.05 0.041 -0.088 -0.002
2007 2007 | -0.046 0.018 -2.56 0.011 -0.081 -0.011
|
first_treat#year#c.__tr__#c._x_lpop |
2004 2004 | 0.005 0.018 0.26 0.792 -0.030 0.039
2004 2005 | 0.025 0.018 1.40 0.161 -0.010 0.060
2004 2006 | 0.051 0.021 2.41 0.016 0.009 0.092
2004 2007 | 0.011 0.027 0.42 0.673 -0.041 0.064
2006 2006 | 0.039 0.016 2.36 0.018 0.007 0.071
2006 2007 | 0.038 0.022 1.69 0.091 -0.006 0.082
2007 2007 | -0.020 0.016 -1.22 0.221 -0.052 0.012
|
lpop | 1.065 0.022 48.82 0.000 1.023 1.108
|
first_treat#c.lpop |
2004 | 0.051 0.038 1.35 0.178 -0.023 0.125
2006 | -0.041 0.047 -0.87 0.386 -0.134 0.052
2007 | 0.056 0.039 1.42 0.157 -0.022 0.133
|
year#c.lpop |
2004 | 0.011 0.008 1.46 0.145 -0.004 0.026
2005 | 0.021 0.008 2.56 0.011 0.005 0.037
2006 | 0.011 0.011 0.97 0.330 -0.011 0.032
2007 | 0.021 0.012 1.77 0.077 -0.002 0.044
|
_cons | 2.162 0.070 30.89 0.000 2.024 2.299
-----------------------------------------------------------------------------------------------------
Absorbed degrees of freedom:
-----------------------------------------------------+
Absorbed FE | Categories - Redundant = Num. Coefs |
-------------+---------------------------------------|
first_treat | 4 0 4 |
year | 5 1 4 |
-----------------------------------------------------+
若想获得 CS2021 版本的加总,在 estat
命令中有四个选项:简单,按记录,按组别以及按事件。不过目前尚未可以获得事前趋势检验。
. estat simple
Contrasts of predictive margins Number of obs = 2,500
Model VCE: Robust Subpop. no. obs = 291
Expression: Linear prediction, predict()
1._at: __tr__ = 0
2._at: __tr__ = 1
------------------------------------------------
| df chi2 P>chi2
-------------+----------------------------------
_at | 1 16.41 0.0001
------------------------------------------------
--------------------------------------------------------------
| Delta-method
| Contrast std. err. [95% conf. interval]
-------------+------------------------------------------------
_at |
(2 vs 1) | -0.051 0.012 -0.075 -0.026
--------------------------------------------------------------
. estat calendar
Contrasts of predictive margins Number of obs = 2,500
Model VCE: Robust Subpop. no. obs = 291
Expression: Linear prediction, predict()
----------------------------------------------------
| df chi2 P>chi2
-----------------+----------------------------------
_at@__calendar__ |
(2 vs 1) 2004 | 1 0.96 0.3280
(2 vs 1) 2005 | 1 8.94 0.0028
(2 vs 1) 2006 | 1 6.49 0.0108
(2 vs 1) 2007 | 1 12.19 0.0005
Joint | 4 19.19 0.0007
----------------------------------------------------
------------------------------------------------------------------
| Delta-method
| Contrast std. err. [95% conf. interval]
-----------------+------------------------------------------------
_at@__calendar__ |
(2 vs 1) 2004 | -0.021 0.022 -0.064 0.021
(2 vs 1) 2005 | -0.082 0.027 -0.135 -0.028
(2 vs 1) 2006 | -0.044 0.017 -0.078 -0.010
(2 vs 1) 2007 | -0.052 0.015 -0.082 -0.023
------------------------------------------------------------------
. estat group
Contrasts of predictive margins Number of obs = 2,500
Model VCE: Robust Subpop. no. obs = 291
Expression: Linear prediction, predict()
--------------------------------------------------
| df chi2 P>chi2
---------------+----------------------------------
_at@__group__ |
(2 vs 1) 2004 | 1 14.46 0.0001
(2 vs 1) 2006 | 1 1.31 0.2524
(2 vs 1) 2007 | 1 6.54 0.0106
Joint | 3 20.18 0.0002
--------------------------------------------------
----------------------------------------------------------------
| Delta-method
| Contrast std. err. [95% conf. interval]
---------------+------------------------------------------------
_at@__group__ |
(2 vs 1) 2004 | -0.088 0.023 -0.133 -0.042
(2 vs 1) 2006 | -0.021 0.019 -0.058 0.015
(2 vs 1) 2007 | -0.046 0.018 -0.081 -0.011
----------------------------------------------------------------
. estat event
Contrasts of predictive margins Number of obs = 2,500
Model VCE: Robust Subpop. no. obs = 291
Expression: Linear prediction, predict()
-------------------------------------------------
| df chi2 P>chi2
--------------+----------------------------------
_at@__event__ |
(2 vs 1) 0 | 1 6.17 0.0130
(2 vs 1) 1 | 1 11.18 0.0008
(2 vs 1) 2 | 1 20.05 0.0000
(2 vs 1) 3 | 1 11.49 0.0007
Joint | 4 23.87 0.0001
-------------------------------------------------
---------------------------------------------------------------
| Delta-method
| Contrast std. err. [95% conf. interval]
--------------+------------------------------------------------
_at@__event__ |
(2 vs 1) 0 | -0.033 0.013 -0.059 -0.007
(2 vs 1) 1 | -0.057 0.017 -0.091 -0.024
(2 vs 1) 2 | -0.138 0.031 -0.198 -0.078
(2 vs 1) 3 | -0.110 0.032 -0.173 -0.046
---------------------------------------------------------------
该方法的优势在于可以采用非线性估计法,以 poisson
为例:
. gen emp=exp(lemp)
. jwdid emp lpop, ivar(countyreal) tvar(year) gvar(first_treat) method(poisson)
Poisson regression Number of obs = 2,500
Wald chi2(29) = 23470.49
Log pseudolikelihood = -143818.87 Prob > chi2 = 0.0000
(Std. err. adjusted for 500 clusters in countyreal)
-----------------------------------------------------------------------------------------------------
| Robust
emp | Coefficient std. err. z P>|z| [95% conf. interval]
------------------------------------+----------------------------------------------------------------
first_treat#year#c.__tr__ |
2004 2004 | -0.031 0.018 -1.77 0.077 -0.065 0.003
2004 2005 | -0.066 0.025 -2.60 0.009 -0.116 -0.016
2004 2006 | -0.133 0.024 -5.64 0.000 -0.179 -0.087
2004 2007 | -0.117 0.022 -5.23 0.000 -0.161 -0.073
2006 2006 | -0.009 0.024 -0.37 0.709 -0.057 0.039
2006 2007 | -0.068 0.025 -2.73 0.006 -0.117 -0.019
2007 2007 | -0.040 0.017 -2.39 0.017 -0.073 -0.007
|
first_treat#year#c.__tr__#c._x_lpop |
2004 2004 | 0.012 0.007 1.68 0.094 -0.002 0.026
2004 2005 | 0.021 0.012 1.77 0.077 -0.002 0.044
2004 2006 | 0.041 0.010 4.30 0.000 0.022 0.060
2004 2007 | 0.025 0.011 2.23 0.026 0.003 0.047
2006 2006 | 0.038 0.014 2.78 0.006 0.011 0.064
2006 2007 | 0.045 0.016 2.77 0.006 0.013 0.077
2007 2007 | -0.011 0.007 -1.53 0.127 -0.025 0.003
|
lpop | 1.040 0.017 62.01 0.000 1.007 1.072
|
first_treat#c.lpop |
2004 | -0.013 0.040 -0.33 0.742 -0.092 0.066
2006 | -0.081 0.033 -2.47 0.013 -0.145 -0.017
2007 | 0.016 0.025 0.65 0.514 -0.033 0.065
|
year#c.lpop |
2004 | -0.007 0.004 -1.47 0.141 -0.015 0.002
2005 | -0.008 0.006 -1.32 0.188 -0.019 0.004
2006 | -0.009 0.006 -1.40 0.162 -0.022 0.004
2007 | -0.004 0.005 -0.66 0.511 -0.014 0.007
|
first_treat |
2004 | 0.236 0.207 1.14 0.253 -0.169 0.641
2006 | 0.593 0.176 3.37 0.001 0.248 0.938
2007 | -0.144 0.122 -1.18 0.236 -0.383 0.094
|
year |
2004 | -0.011 0.021 -0.51 0.612 -0.051 0.030
2005 | 0.011 0.027 0.42 0.675 -0.041 0.064
2006 | 0.045 0.029 1.57 0.117 -0.011 0.102
2007 | 0.054 0.028 1.95 0.051 -0.000 0.109
|
_cons | 2.484 0.077 32.25 0.000 2.333 2.635
-----------------------------------------------------------------------------------------------------
5. 参考文献
Callaway, B., Goodman-Bacon, A. and Sant’Anna, P. H. (2021), Difference-in-differences with a continuous treatment. arXiv preprint arXiv:2107.02637. -PDF- Callaway, B. and Sant’Anna, P. H. (2020),Difference-in-differences with multiple time periods, Journal of Econometrics forthcoming. -PDF- Wooldridge, J. (2021), Two-way fixed effects, the two-way mundlak regression, and differencein-differences estimators, Available at SSRN 3906345. -PDF- Wooldridge, Jeffrey. (2022), Simple Approaches to Nonlinear Difference-in-Differences with Panel Data. Working paper. -PDF-
6. 相关推文
Note:产生如下推文列表的 Stata 命令为:
lianxh did, m
安装最新版lianxh
命令:
ssc install lianxh, replace
专题:专题课程 DID新进展:异质性多期DID估计的新方法-csdid ⏩ 因果推断专题-RDD-DID-IV-合成控制 专题:倍分法DID DID-倍分法:事前趋势检验的局限性和诊断 Stata:异质性稳健DID估计量方法汇总 DID偏误问题:多时期DID的双重稳健估计量(下)-csdid DID偏误问题:两时期DID的双重稳健估计量(上)-drdid Stata+R:合成DID原理及实现-sdid DID的陷阱和注意事项 Stata:事件研究法的稳健有效估计量-did_imputation DID最新进展:异质性处理条件下的双向固定效应DID估计量 (TWFEDD) 倍分法:交错DID与Stata操作 Stata-DID:不同处理时点不同持久期的倍分法(flexpaneldid) DIDM:多期多个体倍分法-did_multiplegt 面板PSM+DID如何做匹配? tfdiff:多期DID的估计及图示 多期DID之安慰剂检验、平行趋势检验 DID边际分析:让政策评价结果更加丰满 Big Bad Banks:多期 DID 经典论文介绍 多期DID:平行趋势检验图示 Stata:多期倍分法 (DID) 详解及其图示 倍分法DID详解 (二):多时点 DID (渐进DID) 倍分法DID详解 (一):传统 DID 倍分法DID详解 (三):多时点 DID (渐进DID) 的进一步分析
尊敬的老师 / 亲爱的同学们:
连享会致力于不断优化和丰富课程内容,以确保每位学员都能获得最有价值的学习体验。为了更精准地满足您的学习需求,我们诚挚地邀请您参与到我们的课程规划中来。请您在下面的问卷中,分享您 感兴趣的学习主题或您希望深入了解的知识领域 。您的每一条建议都是我们宝贵的资源,将直接影响到我们课程的改进和创新。我们期待您的反馈,因为您的参与和支持是我们不断前进的动力。感谢您抽出宝贵时间,与我们共同塑造更加精彩的学习旅程!https://www.wjx.cn/vm/YgPfdsJ.aspx# 再次感谢大家宝贵的意见!
New! Stata 搜索神器:
lianxh
和songbl
GIF 动图介绍
搜: 推文、数据分享、期刊论文、重现代码 ……
👉 安装:
. ssc install lianxh
. ssc install songbl
👉 使用:
. lianxh DID 倍分法
. songbl all
🍏 关于我们
连享会 ( www.lianxh.cn,推文列表) 由中山大学连玉君老师团队创办,定期分享实证分析经验。 直通车: 👉【百度一下: 连享会】即可直达连享会主页。亦可进一步添加 「知乎」,「b 站」,「面板数据」,「公开课」 等关键词细化搜索。