* 0. 设面板
xtset firm_id year, yearly // firm_id 是字符串要先 encode
* 1. 查 within 变异
xtsum x y controls
看 x 的 Within SD 是否 > 0;若接近 0,FE 没法做,直接改用 RE 或 POLS。
二、基准估计(三种写法等价)
* 法 1:官方 xtreg
xtreg y x controls, fe robust // 最常用
* 法 2:吸收哑变量(快)
reghdfe y x controls, absorb(firm_id) vce(cluster firm_id)
* 法 3:LSDV 显式哑变量(可导出 R²)
areg y x controls, absorb(firm_id) vce(cluster firm_id)
结果窗口第一行 F test that all u_i=0 的 p 值 < 0.01 ⇒ 企业 FE 显著存在,POLS 无效。
报告时只给 within-R²(xtreg 右上角),不要给 overall R²。
三、严格诊断
序列相关
xtserial y x controls // Wooldridge 检验
p < 0.1 ⇒ 加 cluster(firm_id) 或改用 Driscoll-Kraay 标准误。
异方差
xttest3 // modified Wald
若拒绝原假设,用 vce(robust) 或 vce(cluster firm_id) 已足够。
弱工具(若做 IV-FE)
xtivreg2 y controls (x = z), fe ffirst
看 F stat > 10 才可继续。
四、结果输出模板
eststo fe1: xtreg y x controls, fe
eststo fe2: xtreg y x controls, fe vce(cluster firm_id)
esttab fe1 fe2, se star(* 0.1 ** 0.05 *** 0.01) ///
stats(r2_w N firms, fmt(3 0 0)) label
论文正文只汇报第 2 列(聚类稳健),within-R² 放在脚注。
五、常见坑
随时间不变的变量(行业、省份、股权性质)会被 FE 吃掉,系数显示“omitted”;若想保留,用:
交互 FE:reghdfe y x##c.industry_trend, absorb(firm_id year)
或 Correlated Random Effects(CRE):xtreg y x w w_bar controls, re
T 很小(<5)时,cluster(firm_id) 偏差大,可:
双向聚类 vce(cluster firm_id year)
或用 bootstrap 50 次
政策冲击变量只在年份层面变化,FE 会把它与 year FE 完全共线;此时用
reghdfe y x post##treat, absorb(firm_id year) (DID-FE 混合)