# Stata：双重差分的固定效应模型-(DID)

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

## 1. 生成数据

• 生成企业数量
``````set obs 400
gen firm=_n
``````
• 时间跨度设定为24个季度（6年）
``````expand 24
bysort firm: gen t=_n
``````
• 设定事件冲击发生在第14期
``````gen d=(t>=14)
label var d "=1 if post-treatment"
``````
• 设定处理组和对照组
``````gen r=rnormal()
qui sum r, d
bysort firm: gen i=(r>=r(p50)) if _n==1
bysort firm: replace i=i[_n-1] if i==. & _n!=1
drop r
label var i "=1 if treated group, =0 if untreated group"
``````
• 设定随机变量
``````gen e = rnormal()
label var e "normal random variable"
``````

## 2. 验证模型

``````gen y = 0.3 + 0.19*i + 1.67*d + 0.56*i*d + e
``````

### 2.1 混合回归 (POLS)

#### 错误设定模型

``````. reg y i d

Source |       SS       df       MS              Number of obs =    9600
-----------+------------------------------           F(  2,  9597) = 4406.07
Model |  9073.16808     2  4536.58404           Prob > F      =  0.0000
Residual |  9881.26843  9597  1.02962055           R-squared     =  0.4787
Total |  18954.4365  9599  1.97462616           Root MSE      =  1.0147

----------------------------------------------------------------------------
y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-----------+----------------------------------------------------------------
i |   .4349154   .0208277    20.88   0.000     .3940888     .475742
d |   1.902249   .0207848    91.52   0.000     1.861506    1.942991
_cons |    .192176   .0168782    11.39   0.000     .1590912    .2252609
----------------------------------------------------------------------------
``````

#### 正确设定模型

``````. reg y i d  i.i#i.d
. eststo pooled

----------------------------------------------------------------------------
y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-----------+----------------------------------------------------------------
i |    .174383   .0280267     6.22   0.000     .1194448    .2293213
d |   1.647874   .0276935    59.50   0.000     1.593589    1.702159
i#d |
1 1  |   .5684342   .0413982    13.73   0.000     .4872851    .6495834
_cons |   .3087643   .0187486    16.47   0.000     .2720131    .3455154
----------------------------------------------------------------------------

``````

Stata连享会 专题课程 || 精彩推文

### 2.2 areg 回归

``````. areg y i d i.i#i.d, absorb(firm)
. eststo areg

----------------------------------------------------------------------------
y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-----------+----------------------------------------------------------------
i |          0  (omitted)
d |   1.647874   .0276586    59.58   0.000     1.593657    1.702091
i#d |
1 1  |   .5684342    .041346    13.75   0.000     .4873869    .6494815
_cons |   .3868007   .0139183    27.79   0.000     .3595177    .4140837
-----------+----------------------------------------------------------------
firm |      F(399, 9198) =      1.156   0.019         (400 categories)
``````

### 2.3 面板回归

``````. xtset firm t, quarter
``````

#### 错误设定模型

``````. xtreg y i d

----------------------------------------------------------------------------
y |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-----------+----------------------------------------------------------------
i |   .4349154   .0212192    20.50   0.000     .3933266    .4765042
d |   1.902249   .0207677    91.60   0.000     1.861545    1.942953
_cons |    .192176   .0170907    11.24   0.000     .1586789    .2256731
-----------+----------------------------------------------------------------
sigma_u |  .04121238
sigma_e |  1.0138689
rho |  .00164959   (fraction of variance due to u_i)
-----------------------------------------------------------------------------
``````
``````. xtreg y i d, fe

----------------------------------------------------------------------------
y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-----------+----------------------------------------------------------------
i |          0  (omitted)
d |   1.902249   .0207677    91.60   0.000     1.861539    1.942958
_cons |   .3868007   .0140598    27.51   0.000     .3592403    .4143611
-----------+----------------------------------------------------------------
sigma_u |  .30216053
sigma_e |  1.0138689
rho |  .08157474   (fraction of variance due to u_i)
------------------------------------------------------------------------------
``````

#### 正确设定模型——随机效应 (RE)

``````. xtreg y i d i.i#i.d
. eststo xtreg_re

------------------------------------------------------------------------
y |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------+----------------------------------------------------------------
i |    .174383   .0284493     6.13   0.000     .1186234    .2301427
d |   1.647874   .0276586    59.58   0.000     1.593664    1.702084
i#d |
1 1  |   .5684342    .041346    13.75   0.000     .4873976    .6494709
_cons |   .3087643   .0190313    16.22   0.000     .2714636    .3460649
-------+----------------------------------------------------------------
sigma_u |  .05056003
sigma_e |   1.003664
rho |  .00253126   (fraction of variance due to u_i)
------------------------------------------------------------------------
``````

#### 正确设定模型——固定效应 (FE)

``````. xtreg y i d i.i#i.d, fe
. eststo xtreg_fe

------------------------------------------------------------------------
y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------+----------------------------------------------------------------
i |          0  (omitted)
d |   1.647874   .0276586    59.58   0.000     1.593657    1.702091
i#d |
1 1  |   .5684342    .041346    13.75   0.000     .4873869    .6494815
_cons |   .3868007   .0139183    27.79   0.000     .3595177    .4140837
-------+----------------------------------------------------------------
sigma_u |  .22793566
sigma_e |   1.003664
rho |   .0490464   (fraction of variance due to u_i)
------------------------------------------------------------------------
F test that all u_i=0:     F(399, 9198) =     1.16           Prob > F = 0.0194

``````

### 2.4 结果输出对比

``````estout *, title("Actual parameter values are i = .19, d = 1.67, and i*d = .56") ///
cells(b(star fmt(%9.3f)) se(par))   ///
stats(N N_g, fmt(%9.0f %9.0g) label(N Groups))      	///
legend collabels(none) varlabels(_cons Constant) keep(i d 1.i#1.d)
``````
``````--------------------------------------------------------------------
pooled            areg        xtreg_re        xtreg_fe
--------------------------------------------------------------------
i           0.174***        0.000           0.174***        0.000
(0.028)             (.)         (0.028)             (.)
d           1.648***        1.648***        1.648***        1.648***
(0.028)         (0.028)         (0.028)         (0.028)
1.i#1.d     0.568***        0.568***        0.568***        0.568***
(0.041)         (0.041)         (0.041)         (0.041)
--------------------------------------------------------------------
N            9600            9600            9600            9600
Groups                                        400             400
--------------------------------------------------------------------
* p<0.05, ** p<0.01, *** p<0.001

``````

## 相关课程

http://lianxh.duanshu.com

### 课程一览

Stata数据清洗 游万海 直播, 2 小时，已上线

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

#### 关于我们

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

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