python - 使用 Python 进行具有固定效应的面板数据回归

标签 python statistics regression statsmodels linearmodels

我将以下面板存储在 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 中运行以下模型?
    model

    这是我尝试过的
    # 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/

    相关文章:

    python - 有效地找到多列的低中位数

    matlab - 确定 MATLAB fitglm() 模型拟合是否收敛

    machine-learning - 年龄的神经网络序数分类

    machine-learning - 是否有支持向量回归 (SVR) 库可以为我提供所使用的回归模型/方程?

    python - tensorflow 如何连接链接卷积层的维度?

    python - WxTimer 不让 wxFrame 关闭

    python - 从非常大的列表中排除特定值的平均值

    regression - 在 MATLAB 中比较两个线性回归模型

    python - Geoalchemy2 和 ST_Within - 点和多边形之间的类型不匹配?

    python - 如何在 tensorflow 2.0 中使用预制的密集层进行训练?