【Python+Stata】豪斯曼检验:固定效应or随机效应?


此文章首发于公众号:Python for Finance

链接:https://mp.weixin.qq.com/s/DO80SNKKyYbLDceJo_kgZw

本文研究陈强《计量经济学及Stata应用》12.13案例的python及stata实现。lin_1992dta数据集链接:链接:https://pan.baidu.com/s/10WAjBbZgL4NzZV69JbgbZw 提取码:d0dx

一、Python实现

(一)获取面板数据

import numpy as np
import linearmodels as plm
import scipy.stats as stats
import pandas as pd

lin =pd.read_stata(r'C:\Users\mi\Documents\stata\lin_1992.dta')
lin = lin.set_index(['province', 'year'], drop=False)

(二)固定效应模型

reg_fe = plm.PanelOLS.from_formula(formula='ltvfo ~1 +ltlan + ltwlab + ltpow + ltfer + hrs + mci+ ngca + EntityEffects',data=lin)
results_fe = reg_fe.fit()

(三)随机效应模型

reg_re = plm.RandomEffects.from_formula(
    formula='ltvfo ~1 +ltlan + ltwlab + ltpow + ltfer + hrs + mci+ ngca', data=lin)
results_re = reg_re.fit()

(四)豪斯曼检验

b_fe = results_fe.params
b_re = results_re.params
b_diff = b_fe - b_re
v_fe = results_fe.cov
v_re = results_re.cov
v_diff = v_fe - v_re
df = len(b_fe)

table = pd.DataFrame({
    
    'FE':b_fe,'RE':b_re,'Difference':b_diff,'sqrt(diag(v_fe-v_re))':np.sqrt(np.diag(v_diff))})
chi2 = np.dot(b_diff.T,  np.linalg.inv(v_diff).dot(b_diff))
pval = 1 - stats.chi2.cdf(chi2, df)

print(table)
print()
print(f'chi-Squared: {
      
      chi2:.2f}')
print(f'degrees of freedom: {
      
      df}')
print(f'p-Value:{
      
      pval:.5f}')

结果为:

                 FE        RE  Difference  sqrt(diag(v_fe-v_re))
Intercept  2.310114  2.387878   -0.077764               0.178212
hrs        0.207582  0.218610   -0.011028                    NaN
ltfer      0.176277  0.188274   -0.011997               0.004112
ltlan      0.639966  0.565591    0.074374               0.043153
ltpow      0.077160  0.060477    0.016683               0.001574
ltwlab     0.123993  0.144184   -0.020192               0.009747
mci        0.258036  0.470237   -0.212201               0.052192
ngca       0.772279  0.674517    0.097762               0.046883

chi-Squared: 48.68
degrees of freedom: 8
p-Value:0.00000

二、Stata实现

(一)获取面板数据

use C:\Users\mi\Documents\stata\lin_1992.dta,clear
xtset province year

结果为:

Panel variable: province (strongly balanced)
 Time variable: year, 70 to 87
         Delta: 1 unit

(二)固定效应模型

xtreg ltvfo ltlan ltwlab ltpow ltfer hrs mci ngca,fe
estimates store FE

(三)随机效应模型

xtreg ltvfo ltlan ltwlab ltpow ltfer hrs mci ngca,re
estimates store RE

(四)豪斯曼检验

由于传统的豪斯曼检验假设球形扰动项,故在进行固定效应与随机效应的估计时,均不使用异方差或聚类稳健的标准误。

hausman FE RE,constant

其中,选择项“constant”表示在比较系数估计值时包括常数项(默认不包括常数项)

结果为:

                 ---- Coefficients ----
             |      (b)          (B)            (b-B)     sqrt(diag(V_b-V_B))
             |       FE           RE         Difference       Std. err.
-------------+----------------------------------------------------------------
       ltlan |    .6399658     .5655915        .0743743        .0431529
      ltwlab |    .1239927     .1441844       -.0201917         .009747
       ltpow |    .0771604      .060477        .0166834         .001574
       ltfer |    .1762775     .1882741       -.0119966        .0041117
         hrs |    .2075817     .2186096       -.0110279               .
         mci |    .2580359     .4702368       -.2122009         .052192
        ngca |    .7722795     .6745175         .097762        .0468832
       _cons |    2.310114     2.387878       -.0777638         .178212
------------------------------------------------------------------------------
                          b = Consistent under H0 and Ha; obtained from xtreg.
           B = Inconsistent under Ha, efficient under H0; obtained from xtreg.

Test of H0: Difference in coefficients not systematic

    chi2(8) = (b-B)'[(V_b-V_B)^(-1)](b-B)
            =  48.68
Prob > chi2 = 0.0000
(V_b-V_B is not positive definite)

由于p指为0.00000,故强烈拒绝原假设,认为应该使用固定效应模型,而非随机效应模型。

但是很多时候计算出的 s q r t ( d i a g ( V b − V B ) ) sqrt(diag(V_b-V_B)) sqrt(diag(VbVB))可能为负,使用sigmamore选项,表示统一使用随机效应估计量的方差估计,可以大大减少出现负值的可能性。

使用sigmamore选项,我不知道怎么在python中实现,可能还是不懂sigmamore的实现原理。先mark一下,以后再更新。

hausman FE RE,constant sigmamore

结果为:

                 ---- Coefficients ----
             |      (b)          (B)            (b-B)     sqrt(diag(V_b-V_B))
             |       FE           RE         Difference       Std. err.
-------------+----------------------------------------------------------------
       ltlan |    .6399658     .5655915        .0743743        .0476709
      ltwlab |    .1239927     .1441844       -.0201917        .0125808
       ltpow |    .0771604      .060477        .0166834        .0081232
       ltfer |    .1762775     .1882741       -.0119966        .0078425
         hrs |    .2075817     .2186096       -.0110279        .0052769
         mci |    .2580359     .4702368       -.2122009        .0583709
        ngca |    .7722795     .6745175         .097762        .0828671
       _cons |    2.310114     2.387878       -.0777638        .2078242
------------------------------------------------------------------------------
                          b = Consistent under H0 and Ha; obtained from xtreg.
           B = Inconsistent under Ha, efficient under H0; obtained from xtreg.

Test of H0: Difference in coefficients not systematic

    chi2(8) = (b-B)'[(V_b-V_B)^(-1)](b-B)
            =  48.49
Prob > chi2 = 0.0000
(V_b-V_B is not positive definite)

猜你喜欢

转载自blog.csdn.net/mfsdmlove/article/details/127027376