《傻瓜计量经济学与stata应用》第七章do文件


*==============================================================================*
*============================第7章 面板数据回归模型============================*
*==============================================================================*

*- 7.1 面板数据模型
    use mus08psidextract.dta, clear
    xtset id t              // 设定面板数据
    xtsum id t exp wks occ  // 既有组间比较也有组内比较

    *- 面板数据平稳性检验-(主要是长面板,单位根)
    webuse pennxrate
    xtset id year
    xtsum
    sum
    xtunitroot llc lnrxrate if oecd, demean lags(aic 10) kernel(bartlett nwest) // LLC检验。假设所有面板数据有相同自回归参数。使用AIC选择滞后阶数回归。
    xtunitroot ht lnrxrate, demean                     // HT检验
    xtunitroot breitung lnrxrate if g7, lags(3) robust // 对G7国家数据进行检验,Breitung检验
    xtunitroot ips lnrxrate, lags(aic 5)               // IPS检验,对全样本共151个国家进行检验
    xtunitroot fisher lnrxrate, dfuller lags(3) drift  // Fisher-ADF检验
    xtunitroot hadri lnrxrate, kernel(parzen 5)        // Hadri LM平稳性检验

    ***************例子***************
    webuse xtcoint
    xtset id time
    describe
    summarize
    xtserial productivity rddomestic rdforeign, output // 面板数据一阶自相关检验,原假设不存在一阶自相关
    xtcointtest kao productivity rddomestic rdforeign  // Kao协整检验
    xtcointtest pedroni productivity rddomestic rdforeign    // 为了进一步确认被解释变量与解释变量之间的协整关系,Predoni检验
    xtcointtest westerlund productivity rddomestic rdforeign // Westerlund协整检验。第一种:检验被解释变量与解释变量是否存在部分协整关系
    xtcointtest westerlund productivity rddomestic rdforeign, allpanels //Westerlund协整检验。第二种:检验被解释变量与解释变量是否存在全局协整
    xtwest depvar varlist  // Westerlund对协整检验的其他补充修正。需安装ssc install xtwest

*- 7.2 静态面板数据模型
    use mus08psidextract.dta, clear
    asdoc sum, replace // 描述性统计分析
    xtset id t // 设定面板数据
    xtdes      // 显示面板数据结构,是否为平衡面板。也可xtdescribe
    xtsum      // 显示组内、组间与整体的统计指标     
    xttab fem  // 显示性别变量fem的组内、组间与整体的分布频率
    xtline wks  // 对每个个体的x1变量分别显示其时间序列图。


    *-比较面板固定效应估计与最小二乘虚拟变量估计结果
    reg lwage exp wks occ ind south smsa ms union i.id
    outreg2 using mmxi, word replace
    xtreg lwage exp wks occ ind south smsa ms union, fe
    outreg2 using mmxi, word 

    *-时间固定效应
    use mus08psidextract.dta, clear
    xtreg lwage exp wks occ ind south smsa ms union i.t, fe        //i.t表时间固定效应。

    xtreg lwage exp wks occ ind south smsa ms union tdum2-tdum7,fe //与上一命令等价。使用时间虚拟变量控制时间固定效应。
                                                               //相较于指示性前缀i.,使用时间虚拟变量好处:不仅可指定年份,还可对时间固定效应检测
    test tdum2 tdum3 tdum4 tdum5 tdum6 tdum7  //原假设:没有固定效应。


    *-还可通过选项cluster() 控制聚类稳健标准误
    xtreg lwage exp wks occ ind south smsa ms union i.t, fe 
    outreg2 using mmxi, word replace
    xtreg lwage exp wks occ ind south smsa ms union tdum2-tdum7,fe
    outreg2 using mmxi, word
    xtreg lwage exp wks occ ind south smsa ms union i.t, fe cluster(id)
    outreg2 using mmxi, word
    xtreg lwage exp wks occ ind south smsa ms union tdum2-tdum7,fe cluster(id)
    outreg2 using mmxi, word

    *-随机效应模型
    use mus08psidextract.dta, clear
    xtreg lwage exp wks occ ind south smsa ms union fem ed blk, re  //re表随机效应模型GLS估计。原来在固定效应模型中不能估计的时间不变因素都可估计。 
    outreg2 using mmxi, word replace
    xtreg lwage exp wks occ ind south smsa ms union fem ed blk, mle //mle表随机效应模型的极大似然估计
    outreg2 using mmxi, word
    xtreg lwage exp wks occ ind south smsa ms union, re
    outreg2 using mmxi, word
    xtreg lwage exp wks occ ind south smsa ms union, mle //随机效应极大似然估计结果更接近固定效应模型而不是OLS
    outreg2 using mmxi, word
 
    *-组间估计
    xtreg lwage exp wks occ ind south smsa ms union fem ed blk, be  //be组间估计量。固定效应不可使用。随机效应也不常使用。

    *-混合面板回归
    reg lwage exp wks occ ind south smsa ms union fem ed blk, cluster(id) //假设前提:不存在个体效应。要对这一假设进行检验


    *-豪斯曼检验
    use mus08psidextract.dta, clear
    xtreg lwage exp wks occ ind south smsa ms union, fe  //豪斯曼检验有效性没有统一说法。通常选择个体固定效应和双向固定效应模型用的较多
    est store fe
    xtreg lwage exp wks occ ind south smsa ms union, re  
    est store re
    hausman fe re //一般固定效应在前


    *-面板随机系数模型
    webuse invest2
    xtset company time
    xtrc invest market stock       //不要求面板数据所有个体都具有相同的估计系数,也称为变系数模型。一般用于长面板。检验个体边际效应是否一致。
    xtrc invest market stock, beta //加入beta后,不仅会估计平均边际效应,还会估计每个个体的边际效应。

    reg invest stock market i.company#c.market i.company, vce(cluster company) //c.是连续变量的指示符号。#交互项。

*- 7.3 面板工具变量法
    webuse nlswork, clear
    summarize
    xtset idcode year
    xtreg ln_w tenure age c.age#c.age not_smsa, fe                  //c.age#c.age,age的平方项,c.是连续变量的指示性符号
    xtivreg ln_w age c.age#c.age not_smsa (tenure=union south), fe  //如果需要第一阶段结果,最加first选项。

    xtivreg ln_w age c.age#c.age not_smsa (tenure=union south) i.year, fe         //如果需要第一阶段结果,最加first选项。

    xtivreg ln_w age c.age#c.age not_smsa (tenure=union south), fe vce(bootstrap) //xtivreg不能使用robust稳健标准误,可以bootstrap再抽样法获得标准误

    xtivreg ln_w age c.age#c.age not_smsa 2.race(tenure=union birth_yrsouth), re //还可以使用广义两阶段最小二乘法随机效应工具变量法估计,
                                                                             //并加入出生年份为工具变量。2.race表黑人虚拟变量。同理,1.race表白人虚拟变量
                                                                        
    xtivreg ln_w age not_smsa (tenure=union birth_yrsouth), fd     //也可一阶差分工具变量估计。该命令不能使用i.或c.等指示性前缀。


*- 7.4 面板交互固定效应与多维固定效应模型
    ssc install regife, replace       //面板交互固定效应模型的估计命令
    ssc install reghdfe, replace      //需同时安装reghdfe(面板多维固定效应模型,控制多重固定效应并允许多重聚类)
    ssc install hdfe, replace         //hdfe(将根据一组固定的效果部分地输出变量,要么覆盖内存中的数据集,要么生成新的变量)。
                                  //hdfe是reghdfe模块的底层过程,它包含关于过程的更多细节
    ssc install ftools, replace       //安装工具包,执行命令reghdfe和regife中处理因子变量的必要工具

    webuse nlswork, clear
    xtset id year
    sum ln_w tenure id year
    keep if id<=100  //删除大部分样本,减少估计时间
    sum ln_w tenure id year

    regife ln_w tenure, ife(id year, 1) //考虑一维交互固定效应。ife(个体变量   时间变量, 固定效应的时间维数)

    regife ln_w tenure, ife(id year, 1) vce(bootstrap)    //可用bootstrap方法计算标准误。迭代计算时间长,样本大时慎用。

    regife ln_w tenure, ife(id year, 1) residuals(newvar) //保存残差项,以便用于相关检验。

    regife ln_w tenure, a(id) ife(id year, 1)            //控制交互固定效应的同时,同时吸收个体固定效应

    regife ln_w tenure, a(id year) ife(id year, 1)       //控制交互固定效应的同时,同时吸收个体和时间双固定效应

    regife ln_w tenure, a(fe_id=id fe_year=year) ife(ife_id=id ife_year=year, 1) //保存个体固定效应到新变量fe_id中;
                                                    //保存时间固定效应到新变量fe_year中,生成因子荷载和共同因子并保存到新变量ife_id,ife_year中
                                                    
    sum fe_id fe_year ife_id1 ife_year1 //查看个体固定效应、时间固定效应和交互固定效应
    list id year fe_id fe_year ife_id1 ife_year1 if _n<=24 //

    *- 进一步通过图形进行比较    
    scatter fe_id id        //个体固定效应随个体变化散点图
    graph save fei, replace
    scatter fe_year year    //时间固定效应随时间变化散点图
    graph save fet, replace    
    scatter ife_id1 id     //个体交互固定效应随个体变化散点图
    graph save ifei, replace
    scatter ife_year1 year //时间交互固定效应随时间变化散点图
    graph save ifet, replace
    graph combine fei.gph fet.gph ifei.gph ifet.gph

    *- 面板多维固定效应估计
    webuse nlswork, clear
    sum idcode year occ_code ln_w age ttl_exp tenure not_smsa south //
    reghdfe ln_w tenure age ttl_exp not_smsa south. absorb(idcode year occ_code) //研究工作年限tenure对工资的影响,同时考虑员工id,
                    //职业occ和时间year三个维度共同因素影响,使用面板多维固定效应模型。absorb()中为固定效应。使用命令reghdfe估计多维固定效应时,
                    //会自动删除只有一个观测值的个体,因为会导致效率下降。此外,该方法不会输出或存储id,occ,year三个维度固定效应结果。想查看
    quietly reghdfe ln_w tenure age ttl_exp not_smsa south, absorb(FE1=idcode FE2=year FE3=occ)    
                    //生产三个新变量FE123来存储固定效应
    sum FE1 FE2 FE3
    list FE1 FE2 FE3 if _n<24 //进一步了解固定效应特征

    reghdfe ln_w tenure age ttl_exp not_smsa south, absorb(idcode year occ i.occ# i.year) //加入职业和年份的交互固定效应                    

    *- 面板多维固定效应工具变量估计
    findit ivreghdfe     //
    findit ivreg2
    ssc install ranktest //用于做相关的检验

    webuse nlswork,clear
    ivreghdfe reghdfe ln_w tenure age ttl_exp not_smsa south(tenure=wks_ue), absorb(idcode year occ) //工具变量wks_ue为上一年失业的周数
    
*- 7.5 动态面板估计
    *- 差分GMM估计
    webuse abdata    //网络数据,工资对就业的影响
    xtset id year三个维度固定效应结果。想查看
    xtabond n l(0/1). w l(0/1). (k ys)yr1980-yr1984, lags(2)              //包含被解释变量滞后2期的动态面板数据模型。l(0/1). w表变量W的当期和滞后1期

    xtabond n l(0/1). w l(0/1). (k ys)yr1980-yr1984, lags(2) vce(robust)  //稳健标准误估计,系统默认的一步估计,也可采用两步估计法

    xtabond n l(0/1). w l(0/1). (k ys)yr1980-yr1984, lags(2) twostep      //两阶段GMM。默认一步估计GMM

    xtabond n l(0/1). w l(0/1). (k ys)yr1980-yr1984, lags(2) twostep vce(robust) //结果提示两阶段标准误有偏,建议使用文件标准误,故加入vce(robust)

    estat abond     //完成以上回归估计,进一步检查原模型是否存在自相关
    estat sargan    //动态面板模型使用了较多工具变量,容易产生过度识别问题。过度识别检验。sargan检验要求命令不能使用vce(robust) 选项 

    *- 系统GMM估计
    webuse nlswork,clear
    xtdpdsys n l(0/1). w l(0/1). (k ys)yr1980-yr1984, lags(2)

    xtdpdsys n l(0/1). w l(0/1). (k ys)yr1980-yr1984, lags(2) twostep vce(robust) //也可使用两步系统GMM估计法

    *- 差分与系统GMM估计
    ssc install xtabond2 //T很大时,最好通过xtabond2选项来限制工具变量的使用,避免太多的工具变量

    use mus08psidextract.dta, clear
    xtabond2 lwage l(1/2). lwage l(0/1). wks ms union occ south smsa ind, gmm(lwage, lag(2 4)) gmm(wks ms union, lag(2 3)) iv(occ south smsa ind) nolevel twostep robust       //

 
*- 7.6 长面板数据模型
    use mus08cigar.dta, clear
    xtset state year
    sum 

    *- 面板数据自相关检验
    search xtserial      //手动搜索,安装
    xtserial lnc lnp lnpmin lny year, output //year为时间趋势项.面板数据自相关检验命令

    *- 面板数据异方差检验
    ssc install xttest3
    use mus08cigar.dta, clear
    xtset state year
    qui xtreg lnc lnp lnpmin lny year, fe   //
    xttest3  //面板数据异方差检验,隐含假设,同方差

    *- 面板数据自相关检验
    ssc install xttest2  //截面个体相关性检验,只能在使用xtreg,fe,ivreg2或xtgls命令之后才能使用
    use mus08cigar.dta, clear
    xtset state year
    quietly xtreg lnc lnp lnpmin lny year, fe   //
    xttest2

    *- Driscoll-Kraay估计
    ssc install xtscc
    xtscc lnc lnp lnpmin lny year, fe lag(4)  //选项包括混合效应pooled,固定效应fe和随机效应re,默认为混合效应pooled,lag(#)表设定最大滞后自相关阶数。

    quietly reg lnc lnp lnpmin lny year, robust cluster(state) 
    est store robust
    quietly newey lnc lnp lnpmin lny year, lag(4) force
    est store newey
    quietly xtscc lnc lnp lnpmin lny year, lag(4) 
    est store dris_kraay
    est table *, b se t  //多模型比较

    *- 面板PCSE估计
    use mus08cigar.dta, clear
    xtpcse lnc lnp lnpmin lny i.state year, correlation(psar1) rhotype(tscorr) //假定扰动项默认情况下是异方差的,并且在面板不同个体之间同期截面相关(非地理因素)
           //correlation(psar1)代表面板设定一阶自相关。rhotype(calc)表计算自相关参数,少用。npl根据面板id进行加权处理的一阶自相关。hetonly只异方差无截面相关

    *- 面板FGLS估计
    use mus08cigar.dta, clear
    xtgls lnc lnp lnpmin lny i.state year, correlation(psar1) panels(correlated)

    qui reg lnc lnp lnpmin lny i.state year, cluster(state)
    est store ols
    qui xtpcse lnc lnp lnpmin lny i.state year, corr(psar1)
    est store xtpcse  
    qui xtgls lnc lnp lnpmin lny i.state year, correlation(psar1) panels(correlated)
    est store xtgls
    qui xtscc lnc lnp lnpmin lny i.state year
    est store xtscc
    esttable ols xtpcse xtgls xtscc using xmm.rtf, replace b(%6.3f) se(%6.3f) ar2(3) mtitle(ols xtpcse xtgls xtscc) star(*0.1 **0.05 ***0.01) compress nogap keep(lnp lnpmin lny year)   //keep()表只输出变量()的系数

    *- 偏差校正LSDV动态面板估计
    ssc install xtlsdvc
    xtlsdvc lnc lnp lnpmin lny year, initial(ab) vcov(50) bias(3) //控制的是时间趋势项year,而不是i.year


*- 7.7 面板中介效应模型
    *- 面板数据固定效应中介模型估计
    *-segmediarion命令,搜索下载,解压到外部命令文件夹之下(PLUS:C:\Users\jjys9\ado\plus)。设定不同,sysdir查看
    use mus08cigar.dta, clear
    xtset state year
    tab state, gen(state)  //生成state虚拟变量。如果是短面板数据,则可用此命令生成时间虚拟变量)

    quietly reg lnc lnp lnpmin lny year i.state
    est store ols
    quietly xtreg lnc lnp lnpmin lny year, fe
    est store fe
    esttab ols fe using xmm.rtf, replace b(%6.3f) se(%6.3f) ar2(3) mtitle(OLS FE) star(*0.1 **0.05 ***0.01) compress nogap keep(lnp lnpmin lny year) 

    segmediarion lnc, mv(lnp) iv(lny) cv(state2-state10 year) //state2-state10 year表美国10个州的虚拟变量,控制个体效应;year控制时间趋势

     *************segmediarion中介效应结果中,主要有三张回归估计表格和一个SobelGoodman中介效应检验。其中,前三张表格分别对应中介效应分析的前三步,第四个检验对应前文中介效应判定第四步****************
    xtreg lnc lny year, fe     //第一步回归估计结果,实际上与面板数据固定效应模型分步回归估计结果一致
    xtreg lnp lny year, fe     //第二步回归估计结果,等价于面板固定效应估计(分步回归第二步)。
    xtreg lnc lnp lny year, fe //第三步回归估计结果,等价于面板固定效应估计(分步回归第三步)。没有第四步可以手动计算。

    *- 基于结构方程的中介模型估计
    ssc install medsem, replace //专门用于sem命令之后计算中介效应)
    medsem, indep(lny) med(lnp) dep(lnc) //

*- 7.8 面板随机前沿模型
    *- 面板随机前沿模型估计
    webuse xtfrontier1, clear
    xtfrontier lnwidgets lnmachines lnworkers, ti //xtfrontier命令要求所有因变量和解释变量先取对数,然后才能进行回归估计。
                                              //ti代表时间不变无效率模型,使用此模型计算的个体或地区技术效率不会随时间发生变化
        //时间不变无效率模型,虽然是面板数据,但是假设每个企业的效率水平和外生冲击不随时间变化,因而估计出的企业技术效率也不随时间变化
    predict efficiency1 te     //计算个体或地区技术效率
    list efficiency1 if id==1  

    xtfrontier lnwidgets lnmachines lnworkers, tvd //各个企业不同年份的技术效率。tvd代表时间变化衰退无效率模型。
                                               //使用此模型计算的个体或地区技术效率会随时间发生变化
    predict efficiency2, te    //计算个体或地区计数效率
    xtsum efficiency1 efficiency2


    **********************也可使用sfpanel命令***********************
    ssc install sfpanel, replace //随机前沿模型估计也可采用sfpanel,但计算结果不如xtfrontier命令稳定
    ssc install sfcross, replace //sfcross是截面数据的随机前沿估计命令,sfpanel面板随机前沿估计需要调用

    webuse xtfrontier1, clear
    sfpanel lnwidgets lnworkers lnmachines, m(tfe) usigma(lnworkers) robust //真实固定效应模型。help*

    predict jlms1, jlms        //计算计数效率                                               


 

  • 6
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值