第十五章 短面板
15.13短面板的stata命令及实例
clear all
cd "F:\stata经济数据分析"
dir
use traffic.dta, clear
* 1 面板数据的设定
* xtset panelvar timevar
* encode country, gen(cntry) //假如panelvar是字符串,转换为数字型
xtset state year
* 显示数据结构
* xtdes // 显示面板数据的结构,是否为平衡面板
* xtsum // 显示组内、组间与整体的统计指标
* xttab varname // 显示组内、组间与整体的分布频率,tab是指tabulate
* xtline varname // 对每位个体分别显示该变量的时间序列图,如果希望将所有个体的时间序列图叠加在一起,可以加上选择项 overlay
xtdes
xtsum
xtsum fatal beertax spircons unrate perinck state year
xtline fatal
xtline fatal, overlay
* 2 混合回归
* reg y x1 x2 x3,vce(cluster id)
reg fatal beertax spircons unrate perinck, vce(cluster state)
estimates store OLS
reg fatal beertax spircons unrate perinck
* 3 固定效应
* FE格式:xtreg y x1 x2 x3,fe r
* LSDV法:reg y x1 x2 x3 i.id ,r
xtreg fatal beertax spircons unrate perinck,fe r
estimates store FE_robust
xtreg fatal beertax spircons unrate perinck,fe
estimates store FE
reg fatal beertax spircons unrate perinck i.state,vce(cluster state)
estimates store LSDV
* 附带一阶差分结果
xtserial fatal beertax spircons unrate perinck, output
estimates store FD
* 双向固定效应
tab year, gen(year)
xtreg fatal beertax spircons unrate perinck year2-year7, fe r
estimates store FE_TW
test year2 year3 year3 year4 year5 year6 year7
xtreg fatal beertax spircons unrate perinck i.year , fe r
* 4 随机效应
* xtreg y x1 x2 x3, re r theta // 随机效应FGLS
* xtreg y x1 x2 x3, mle // 随机效应MLE
* 随机效应FGLS
xtreg fatal beertax spircons unrate perinck, re r theta
estimates store RE
xttest0
* 随机效应MLE
xtreg fatal beertax spircons unrate perinck, mle nolog
estimates store MLE
* 5 组间估计量
* 组间估计BE :xreg y x1 x2 x3, be
xtreg fatal beertax spircons unrate perinck, be
estimates store MLE
* 6 固定效应还是随机效应:豪斯曼检验
* stata code for hausman
* xreg y x1 x2 x3, fe
* estimates store FE
* xreg y x1 x2 x3, re
* estimates store RE
* hausman FE RE, constant sigmamore
hausman FE RE, constant sigmamore
* 注:vce(cluster id) 无法执行该命令
quietly xtreg fatal beertax spircons unrate perinck, re
scalar theta = e(theta)
global yandxforhausman fatal beertax spircons unrate perinck
sort state
foreach x of varlist $yandxforhausman{
by state : egen mean`x'=mean(`x')
gen md`x' = `x' - mean`x'
gen red`x'= `x' - theta*mean`x'
}
quietly reg redfatal redbeertax redspircons redunrate redperinck mdbeertax mdspircons mdunrate mdperinck, vce(cluster state)
test mdbeertax mdspircons mdunrate mdperinck
quietly xtreg fatal beertax spircons unrate perinck, re r
xtoverid
estimates table OLS FE_robust FE_TW RE BE, b se