Stata:Mundlak方法的DID-jwdid

发布时间:2022-12-07 阅读 1498

Stata连享会   主页 || 视频 || 推文 || 知乎 || Bilibili 站

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

New! lianxh 命令发布了:
随时搜索推文、Stata 资源。安装:
. ssc install lianxh
详情参见帮助文件 (有惊喜):
. help lianxh
连享会新命令:cnssc, ihelp, rdbalance, gitee, installpkg

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

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

⛳ Stata 系列推文:

PDF下载 - 推文合集

作者:杜静玄 (雪城大学)
邮箱jdu115@syr.edu

编者按:本文整理自 Two-Way FE: The come back to DID,特此致谢!


目录


1. 简介

Wooldridge 证明了双向固定效应 (TWFE) 与包括单位时间均值和时间段特定横截面平均值的混合 OLS (他称之为双向 Mundlak (TWM) 回归) 的结果具有等价性。该等价意味着异质趋势的标准策略可用于放宽共同趋势假设,尤其对渐进型 DID 有效。此外,双向 Mundlak 回归也很容易适应非线性模型,例如指数模型和 logit 和 probit 模型。Stata 的命令 jwdid 提供了基于 TWM 法的估计结果。

2. TWFE 与 TWM 的等价性

其中,xit 是 1×k 的向量,ci 是个体固定效应,ft 为时间固定效应。考虑双向 Mundlak 回归:

混合 OLS 使用 yit 对 1,xit,x¯i,x¯t 进行回归,得到 xit 的系数 βM。令 x¨it=xitx¯ix¯t+x¯,若 i=1Nt=1Nx¨itx¨it 是非奇异矩阵,β^M=β^FE,可以利用 FrischWaugh 定理证明。若 xit=zimt,则 x¯i=zim¯,x¯t=z¯mt,其中 zi 是时间不变的变量,mt 为单位恒定的变量。

在交错实施的设定中 (staggered intervention),TWFE 需要包括:digfst,s{g,...T},g{q,...T} 来估计处理效应 τ^gs。其中 dig 是处理组指示变量,fst 是时间变量。在 TWFE 中需要包括协变量的交互变量 fstxidigfstxi,在 Mundlak 里需要包括 xi,diqxi,...diTxi,在 TWM 中则需要进一步包括 diq,...diT。同时,混合 OLS 以及 RE 版本的 Mundalk 也是等价的。

3. 在 DID 设定下的应用

传统 TWFE 对于估计 ATT 存在较大限制,而关键是缺乏对于异质性效应的处理。Wooldridge 提出的方法与 Sun and Abraham (2020) 的类似,但和他们将队列与动态效应交互不同,Wooldridge 将队列效应与时间固定效应交互。同时,从该方法中获得的估计量与用插补法获得的第一阶段的相同,并且同插补法相比,它的标准误更容易获得。具体来说,应当估计以下二式之一:

Wooldridge 建议,除了加入个体 (或队列) 以及时间固定效应外,还应该将所有可能的队列和时间效应都加入。而以上两个式子则分别代表:选择从未处理组作为控制,或者选择尚未处理组作为控制。基于该饱和模型,估计出的 λs 与 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

相关课程

免费公开课

最新课程-直播课

专题 嘉宾 直播/回看视频
最新专题 文本分析、机器学习、效率专题、生存分析等
研究设计 连玉君 我的特斯拉-实证研究设计-幻灯片-
面板模型 连玉君 动态面板模型-幻灯片-
面板模型 连玉君 直击面板数据模型 [免费公开课,2小时]
  • Note: 部分课程的资料,PPT 等可以前往 连享会-直播课 主页查看,下载。

课程主页

课程主页

关于我们

  • Stata连享会 由中山大学连玉君老师团队创办,定期分享实证分析经验。
  • 连享会-主页知乎专栏,700+ 推文,实证分析不再抓狂。直播间 有很多视频课程,可以随时观看。
  • 公众号关键词搜索/回复 功能已经上线。大家可以在公众号左下角点击键盘图标,输入简要关键词,以便快速呈现历史推文,获取工具软件和数据下载。常见关键词:课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法

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

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

✏ 连享会-常见问题解答:
https://gitee.com/lianxh/Course/wikis

New! lianxhsongbl 命令发布了:
随时搜索连享会推文、Stata 资源,安装命令如下:
. ssc install lianxh
使用详情参见帮助文件 (有惊喜):
. help lianxh