我将以下面板存储在 df
中:
状态
区
年
是
持续的
x1
x2
时间
0
01
01001
2009年
12
1
0.956007
639673
1
1
01
01001
2010年
20
1
0.972175
639673
2
2
01
01001
2011年
22
1
0.988343
639673
3
3
01
01002
2009年
0
1
0
33746
1
4
01
01002
2010年
1
1
0.225071
33746
2
5
01
01002
2011年
5
1
0.450142
33746
3
6
01
01003
2009年
0
1
0
45196
1
7
01
01003
2010年
5
1
0.427477
45196
2
8
01
01003
2011年
9
1
0.854955
45196
3
y
是每个区的抗议次数constant
是一列满满的x1
是移动网络提供商覆盖的地区面积的比例x2
是每个区的人口数(注意是时间固定的)如何在 Python 中运行以下模型?
这是我尝试过的
# Transform `x2` to match model
df['x2'] = df['x2'].multiply(df['time'], axis=0)
# District fixed effects
df['delta'] = pd.Categorical(df['district'])
# State-time fixed effects
df['eta'] = pd.Categorical(df['state'] + df['year'].astype(str))
# Set indexes
df.set_index(['district','year'])
from linearmodels.panel import PanelOLS
m = PanelOLS(dependent=df['y'], exog=df[['constant','x1','x2','delta','eta']])
ValueError: exog does not have full column rank. If you wish to proceed with model estimation irrespective of the numerical accuracy of coefficient estimates, you can set rank_check=False.
我究竟做错了什么?
最佳答案
我在附近挖掘 the documentation结果证明解决方案非常简单。
设置索引并将固定效果列转为pandas.Categorical
后类型(见上面的问题):
# Import model
from linearmodels.panel import PanelOLS
# Model
m = PanelOLS(dependent=df['y'],
exog=df[['constant','x1','x2']],
entity_effects=True,
time_effects=False,
other_effects=df['eta'])
m.fit(cov_type='clustered', cluster_entity=True)
即,不要将您的固定效应列传递给 exog
.您应该将它们传递给
entity_effects
( bool ), time_effects
( bool 值)或 other_effects
( Pandas 。分类)。
关于python - 使用 Python 进行具有固定效应的面板数据回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69260530/