Stata连享会 主页 || 视频 || 推文 || 知乎 || Bilibili 站
温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。
New!
lianxh
命令发布了:
随时搜索推文、Stata 资源。安装:
. ssc install lianxh
详情参见帮助文件 (有惊喜):
. help lianxh
连享会新命令:cnssc
,ihelp
,rdbalance
,gitee
,installpkg
⛳ Stata 系列推文:
作者:秦利宾 (厦门大学)
邮箱:qlb150@163.com
编者按:本文主要摘译自下文,特此致谢!
Source:deHaan, E. 2021. Using and Interpreting Fixed Effects Models. Working Paper. -PDF- -SUMHDFE-
目录
在金融经济学研究中,固定效应的应用非常普遍,但是仍有许多研究者对固定效应的理解有限。deHaan (2021) 主要解释了以下问题:
特别地,deHaan (2021) 主要关注于那些
关于 deHaan (2021) 的具体内容,本文不做详细介绍,有兴趣的读者可以自行阅读原文。本文主要介绍 deHaan (2021) 附录 A 中关于 Good Practices when Using Fixed Effects 的内容,以及 Sergio Correia (reghdfe
命令的作者)、Ed deHaan、Ties de Kok 等三位作者为此编写的 Stata 包 SUMHDFE。
以下是使用固定效应时的一些注意事项:
研究者在使用固定效应模型时,应当考虑以下信息:
其中,(i) 到 (iv) 为混合样本的统计结果;(v) 为同时考虑 FE 所有分组时,变量组内只有一个观测值的数量;(vi) 到 (vii) 为变量在 FE 组内没有变动的观测值数量;(viii) 为同时考虑 FE 所有分组时,变量组内标准差;(ix) 为变量组内标准差除以混合样本标准差,用以度量 FE 导致的标准差减少程度。
sumhdfe
命令是 reghdfe
命令的扩展,因此需要事先安装 6+ 以上版本的 reghdfe
命令和 ftools
命令。同时为了输出表格文件,我们也需要安装 rtfutil
命令。需要再次强调的是,sumhdfe
命令在第 5 版本的 reghdfe
命令上是不能正常运行的,即通过 ssc install reghdfe, replace
语句安装 reghdfe
命令是不可行的。
作者给出的安装语句如下,但是有时候也可能安装不成功。为此,我们有提供了两种备用安装方式,一种是将相关文件下载到本地,通过 net install pkgname, replace from(directory)
语句安装;另一种是通过连享会命令 lxhget pkgname, replace install
方式安装。
*作者提供的安装方式
*Uninstall any old versions of ftools, reghdfe, sumhdfe
cap ado uninstall ftools
cap ado uninstall reghdfe
cap ado uninstall sumhdfe
*Install the most recent version of ftools, reghdfe, and sumhdfe
net install ftools, from("https://raw.githubusercontent.com/sergiocorreia/ftools/master/src/")
net install reghdfe, from("https://raw.githubusercontent.com/sergiocorreia/reghdfe/master/src/")
net install sumhdfe, from("https://raw.githubusercontent.com/ed-dehaan/sumhdfe/master/src/")
*To generate rtf files you also need to install rtfutil
ssc install rtfutil
*本地安装
*ftools 链接:https://github.com/sergiocorreia/ftools
*reghdfe 链接:https://github.com/sergiocorreia/reghdfe
*sumhdfe 链接:https://github.com/ed-dehaan/sumhdfe
cap ado uninstall ftools
cap ado uninstall reghdfe
cap ado uninstall sumhdfe
net install ftools, from("D:\连享会\ftools-master\src")
net install reghdfe, from("D:\连享会\reghdfe-master\src")
net install sumhdfe, from("D:\连享会\sumhdfe-main\src")
*连享会命令安装
cap ado uninstall ftools
cap ado uninstall reghdfe
cap ado uninstall sumhdfe
cnssc install lxhget, replace
lxhget ftools.pkg, replace install
lxhget reghdfe.pkg, replace install
lxhget sumhdfe.pkg, replace install
sumhdfe
有两种使用方式,一是在 reghdfe
命令后使用;二是单独使用。具体命令语法如下:
*use "https://raw.githubusercontent.com/ed-dehaan/sumhdfe/master/sumhdfe_demo_data.dta", clear
lxhuse sumhdfe_demo_data.dta, clear
reghdfe y x1 x2, a(firm year)
sumhdfe
*use "https://raw.githubusercontent.com/ed-dehaan/sumhdfe/master/sumhdfe_demo_data.dta", clear
lxhuse sumhdfe_demo_data.dta, clear
sumhdfe y x1 x2, a(firm year)
其中,a(firm year)
表示要吸收的固定效应为 firm
和 year
。详细介绍可参考 help sumhdfe
和连享会推文「reghdfe:多维面板固定效应估计」。
在运行上述命令语句之后,Stata 会输出以下四张表:
具体来看,Panel A 为全样本的描述性统计结果。其中,N 包括组内只有单个观测值 (singleton) 的样本,这与 reghdfe
结果中的 N 不一样。
Panel A: Summary statistics of regression variables (including singleton observations)
---------------------------------------------------
Variable | N mean sd
-----------------+---------------------------------
y | 623 152.4039 502.1975
x1 | 623 .6832415 79.73515
x2 | 623 4.416166 9.809758
---------------------------------------------------
. *reghdfe 命令结果
. reghdfe y x1 x2, a(firm year)
(dropped 38 singleton observations)
(MWFE estimator converged in 17 iterations)
HDFE Linear regression Number of obs = 585
Absorbing 2 HDFE groups F( 2, 396) = 760.07
Prob > F = 0.0000
R-squared = 0.9706
Adj R-squared = 0.9566
Within R-sq. = 0.7933
Root MSE = 104.9299
------------------------------------------------------------------------------
y | Coefficient Std. err. t P>|t| [95% conf. interval]
-------------+----------------------------------------------------------------
x1 | 4.962 0.217 22.89 0.000 4.536 5.388
x2 | 10.006 0.584 17.13 0.000 8.857 11.154
_cons | 103.555 5.062 20.46 0.000 93.604 113.507
------------------------------------------------------------------------------
Absorbed degrees of freedom:
-----------------------------------------------------+
Absorbed FE | Categories - Redundant = Num. Coefs |
-------------+---------------------------------------|
firm | 160 0 160 |
year | 28 1 27 |
-----------------------------------------------------+
Panel B 为固定效应的统计结果。从公司固定效应 (firm) 中可以看出,这里有 189 个公司,其中 28 个公司只有一个观测值 (singletons)。单个公司观测值数量的范围为 1 到 8 个,均值为 3.30 个。从年度固定效应 (year) 中可以看出,这里有 39 年数据,其中 8 年都只有一个观测值 (singletons)。公司和年度固定效应联合组中,有 2 个只有一个观测值。因此,这里共有 38 个 singletons 会从 reghdfe
的输出结果中删除,正如上表中呈现的结果 (dropped 38 singleton observations)。
Panel B: Summary statistics of fixed effects
--------------------------------------------------------------------------------------------
| Number of ... | Observations per group
Fixed Effect | Observations Groups Singletons | Min. Avg. Max.
-----------------+------------------------------------------+-------------------------------
firm | 623 189 28 | 1 3.30 8
year | 623 39 8 | 1 15.97 51
-----------------+------------------------------------------+-------------------------------
Joint singletons | . . 2 | . . .
Total singletons | . . 38 | . . .
--------------------------------------------------------------------------------------------
Note: there are 38 singletons (6.1% of all observations)
Panel C 为固定效应组内无任何变化的统计结果。变量
Panel C: Variables that are constant within a fixed effect group
-------------------------------------------------------------------------------
| Number of ... | firm* | year*
Variable | Obs Singl | #Groups #Obs | #Groups #Obs
-----------------+--------------------+--------------------+-------------------
y | 623 38 | 0 0 | 0 0
x1 | 623 38 | 58 217 | 4 28
x2 | 623 38 | 0 0 | 0 0
-------------------------------------------------------------------------------
Note: columns with * were computed excluding singleton observations
Panel D 为变量被固定效应吸收的变化。在混合样本中,
Panel D: Residual variation after partialling-out
-------------------------------------------------------------------------------------------------
| | Std. Dev. | R2 by fixed effect | R2
Variable | N* | Pooled Within* Ratio (%) | firm year | Overall
-----------------+----------+---------------------------------+------------------------+---------
y | 585 | 502.1975 190.0668 37.85 | 0.816 0.113 | 0.866
x1 | 585 | 79.73515 22.68065 28.44 | 0.871 0.132 | 0.924
x2 | 585 | 9.809758 8.419488 85.83 | 0.254 0.055 | 0.308
-------------------------------------------------------------------------------------------------
Note: columns with * were computed excluding singleton observations
. *计算固定效应中变量标准差
. egen miss = rowmiss(y x1 x2) // 三个变量存在缺失值个数
. qui: reghdfe x1 if miss == 0, a(firm year) res(x1_res)
. qui: sum x1_res
. dis `r(sd)'
22.680647
. *计算固定效应对变量的解释能力
. //需要注意的是,这里不能使用 reghdfe 命令,
. //因为该命令会自动删掉 singletons
. qui: reg x1 i.firm if miss == 0
. est store m1
. qui: reg x1 i.year if miss == 0
. est store m2
. qui: reg x1 i.firm i.year if miss == 0
. est store m3
. esttab m1 m2 m3, r2 indicate("Frim FE = *.firm" "Year FE = *.year") nogaps
------------------------------------------------------------
(1) (2) (3)
x1 x1 x1
------------------------------------------------------------
_cons 106.9*** 166.3* 51.39
(6.98) (2.17) (1.20)
Frim FE Yes No Yes
Year FE No Yes Yes
------------------------------------------------------------
N 623 623 623
R-sq 0.871 0.132 0.924
------------------------------------------------------------
当然上述四张表也可以通过 sumhdfe_export
命令,导出为 .rtf
和 .tex
格式文档。例如,导出 .rtf
格式,具体命令如下。其中,panels(a b c d)
为导出四张表,也可以通过 panels(a b)
只导出 Panel A 和 Panel B。
. sumhdfe y x1 x2, a(firm year)
. sumhdfe_export using table.rtf, panels(a b c d)
sumhdfe tables saved in table.rtf
histogram(#)
选项可以绘制组内观测值个数直方图。例如,sumhdfe, histogram(1)
绘制的是固定效应 a(firm year)
中 firm 组中观测值个数直方图,见下图。可以看出,公司固定效应组内观测值个数分布范围为 2 到 8,这与 Panel B 的结果保持一致,并且组内观测值个数为 2 的公司最多。需要注意的是,histogram(1)
括号中 1
可以是 firm
,即公司固定效应,当然也可以是 2
或 year
,即年份固定效应。
. reghdfe y x1 x2, a(firm year)
. sumhdfe, histogram(1)
除此之外,我们还可以绘制变量
. lxhuse sumhdfe_demo_data.dta, clear
. qui: reghdfe y x1 x2, a(firm year)
. qui: reghdfe x1 if e(sample), a(firm year) resid
. twoway (histogram x1, fcolor(green%75) lcolor(none)) ///
> (histogram _reghdfe_resid, fcolor(navy%70) lcolor(none)), ///
> legend(on order(1 "x1" 2 "within-FE x1"))
Note:产生如下推文列表的 Stata 命令为:
lianxh 固定效应 控制变量, 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