Art*_*Sbr 7 python statistics regression statsmodels linearmodels
我将以下面板存储在df:
| 状态 | 区 | 年 | y | 持续的 | 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是一列充满 1 的列x1是移动网络提供商覆盖的地区面积的比例x2是每个区的人口数(注意是时间固定的)这是我尝试过的
# 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']])
Run Code Online (Sandbox Code Playgroud)
ValueError:exog 没有完整的列排名。如果您希望继续进行模型估计,而不考虑系数估计的数值精度,则可以设置rank_check=False。
我究竟做错了什么?
我仔细研究了文档,发现解决方案非常简单。
设置索引并将固定效应列转换为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)
Run Code Online (Sandbox Code Playgroud)
也就是说,不要将固定效应列传递给 exog.
您应该将它们传递给entity_effects(boolean)、time_effects(boolean) 或other_effects(pandas.Categorical)。