Nosferican / Econometrics.jl

Econometrics in Julia
https://nosferican.github.io/Econometrics.jl/dev
ISC License
69 stars 19 forks source link

Singularity problems with RandomEffectsEstimator #83

Closed danielbr2 closed 1 year ago

danielbr2 commented 1 year ago

Hello,

I'm trying to replicate an example from Wooldridges Introductory Econometrics textbook, but I'm having problems with the RandomEffectsEstimator. In the example below, the variable year seems to cause the error LinearAlgebra.SingularException(6). If left out, the problem disappears. Is there a bug in the code, or am I missing something?

using WooldridgeDatasets, GLM, DataFrames, Econometrics

wagepan = DataFrame(wooldridge("wagepan"))

# RandomEffectsEstimator gives singularity problems:
reg_re = fit(RandomEffectsEstimator,
    @formula(lwage ~ educ + black + hisp + exper + (exper^2) +
                     married + union + year),
    panel=:nr, time=:year,
    wagepan,
    contrasts=Dict(:year => DummyCoding()))

Without error I would expect something like Pythons linearmodels :

import wooldridge as woo
import pandas as pd
import linearmodels as plm

wagepan = woo.dataWoo('wagepan')
reg_re = plm.RandomEffects.from_formula(
    formula='lwage ~ educ + black + hisp + exper + I(exper**2) +'
            'married + union + C(year)', data=wagepan)
results_re = reg_re.fit()
table_re = pd.DataFrame({'b': round(results_re.params, 4),
                         'se': round(results_re.std_errors, 4),
                         't': round(results_re.tstats, 4),
                         'pval': round(results_re.pvalues, 4)})
print(f'table_re: \n{table_re}\n')

# table_re: 
#                     b      se       t    pval
# C(year)[1980]  0.0234  0.1514  0.1546  0.8771
# C(year)[1981]  0.0638  0.1601  0.3988  0.6901
# C(year)[1982]  0.0543  0.1690  0.3211  0.7481
# C(year)[1983]  0.0436  0.1780  0.2450  0.8065
# C(year)[1984]  0.0664  0.1871  0.3551  0.7225
# C(year)[1985]  0.0811  0.1961  0.4136  0.6792
# C(year)[1986]  0.1152  0.2052  0.5617  0.5744
# C(year)[1987]  0.1583  0.2143  0.7386  0.4602
# educ           0.0919  0.0107  8.5744  0.0000
# black         -0.1394  0.0480 -2.9054  0.0037
# hisp           0.0217  0.0428  0.5078  0.6116
# exper          0.1058  0.0154  6.8706  0.0000
# I(exper ** 2) -0.0047  0.0007 -6.8623  0.0000
# married        0.0638  0.0168  3.8035  0.0001
# union          0.1059  0.0179  5.9289  0.0000
Nosferican commented 1 year ago

Hi, I suspect it may have to do something with the perfect multicollinearity. Thanks for reporting the reproducible example. I will try to take a look at it this week.

danielbr2 commented 1 year ago

Did you have a chance to look into it? Appreciate your feedback!

Nosferican commented 1 year ago

I am so sorry. I haven't. Got assigned a release schedule and seminar in the last few weeks, which came out this week and presented the seminar this morning. I will try to catch my breath and look at it this weekend. Thanks for following up I haven't forgotten but been quite busy in this last window.