[Python+Stata] Hausman test: fixed effect or random effect?


This article was first published on the official account: Python for Finance

Link: https://mp.weixin.qq.com/s/DO80SNKKyYbLDceJo_kgZw

This paper studies the python and stata implementation of Chen Qiang's "Econometrics and Stata Application" 12.13 case. lin_1992dta dataset link: link: https://pan.baidu.com/s/10WAjBbZgL4NzZV69JbgbZw extraction code: d0dx

1. Python implementation

(1) Obtain panel data

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)

(2) Fixed effect model

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

(3) Random effect model

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

(4) Hausmann test

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}')

The result is:

                 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

Two, Stata implementation

(1) Obtain panel data

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

The result is:

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

(2) Fixed effect model

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

(3) Random effect model

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

(4) Hausmann test

Since the traditional Hausman test assumes a spherical disturbance term, it does not use heteroscedasticity or cluster robust standard errors when estimating fixed effects and random effects.

hausman FE RE,constant

Among them, the option "constant" indicates that the constant term is included when comparing the coefficient estimates (the constant term is not included by default)

The result is:

                 ---- 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)

Since the p index is 0.00000, the null hypothesis is strongly rejected, and it is believed that the fixed effect model should be used instead of the random effect model.

But many times the calculated sqrt ( diag ( V b − VB ) ) sqrt(diag(V_b-V_B))s q r t ( d ia g ( VbVB)) may be negative, use the sigmamore option, which means that the variance estimation of the random effect estimator is used uniformly, which can greatly reduce the possibility of negative values.

Using the sigmamore option, I don't know how to implement it in python, and I may still not understand the implementation principle of sigmamore. Mark first and update later.

hausman FE RE,constant sigmamore

The result is:

                 ---- 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)

Guess you like

Origin blog.csdn.net/mfsdmlove/article/details/127027376