statsmodels.api.ols 中的固定效果

Fixed effects in statsmodels.api.ols

有没有办法在 statsmodels.api.olsstatsmodels.formula.api.ols 中添加固定效应而无需手动创建虚拟变量?

statsmodels不支持固定效应回归。但是,包 linearmodels 可以。

from linearmodels.panel import PanelOLS
import pandas as pd
from linearmodels.datasets import wage_panel
import statsmodels.api as sm

data = wage_panel.load()
year = pd.Categorical(data.year)
data = data.set_index(["nr", "year"])
data["year"] = year

exog_vars = ["expersq", "union", "married", "year"]
exog = sm.add_constant(data[exog_vars])
mod = PanelOLS(data.lwage, exog, entity_effects=True)
fe_res = mod.fit()
print(fe_res)

这会打印

                          PanelOLS Estimation Summary
================================================================================
Dep. Variable:                  lwage   R-squared:                        0.1806
Estimator:                   PanelOLS   R-squared (Between):             -0.0052
No. Observations:                4360   R-squared (Within):               0.1806
Date:                Wed, Feb 16 2022   R-squared (Overall):              0.0807
Time:                        12:49:00   Log-likelihood                   -1324.8
Cov. Estimator:            Unadjusted
                                        F-statistic:                      83.851
Entities:                         545   P-value                           0.0000
Avg Obs:                       8.0000   Distribution:                 F(10,3805)
Min Obs:                       8.0000
Max Obs:                       8.0000   F-statistic (robust):             83.851
                                        P-value                           0.0000
Time periods:                       8   Distribution:                 F(10,3805)
Avg Obs:                       545.00
Min Obs:                       545.00
Max Obs:                       545.00

                             Parameter Estimates
==============================================================================
            Parameter  Std. Err.     T-stat    P-value    Lower CI    Upper CI
------------------------------------------------------------------------------
const          1.4260     0.0183     77.748     0.0000      1.3901      1.4620
expersq       -0.0052     0.0007    -7.3612     0.0000     -0.0066     -0.0038
union          0.0800     0.0193     4.1430     0.0000      0.0421      0.1179
married        0.0467     0.0183     2.5494     0.0108      0.0108      0.0826
year.1981      0.1512     0.0219     6.8883     0.0000      0.1082      0.1942
year.1982      0.2530     0.0244     10.360     0.0000      0.2051      0.3008
year.1983      0.3544     0.0292     12.121     0.0000      0.2971      0.4118
year.1984      0.4901     0.0362     13.529     0.0000      0.4191      0.5611
year.1985      0.6175     0.0452     13.648     0.0000      0.5288      0.7062
year.1986      0.7655     0.0561     13.638     0.0000      0.6555      0.8755
year.1987      0.9250     0.0688     13.450     0.0000      0.7902      1.0599
==============================================================================

F-test for Poolability: 9.1568
P-value: 0.0000
Distribution: F(544,3805)

Included effects: Entity

Included effects: Entity 行表示实体(即固定效果)已包含在内。