【问题标题】:Pandas: Trouble implementing Panel OLSPandas:实施 Panel OLS 时遇到问题
【发布时间】:2016-09-03 14:49:27
【问题描述】:

我有点难以理解如何在 pandas 中实现 Panel OLS。我收到了有关此主题的帮助,并且我认为我正在了解情况。现在我正在尝试实施我遇到了困难。以下是我的数据:

url='https://raw.githubusercontent.com/108michael/ms_thesis/master/crsp.dime.mpl.df.1'



   df=pd.read_csv(url, usecols=(['date', 'cid', 'log_diff_rgdp', 'billsum_support', \
'years_exp', 'leg_totalbills', 'log_diff_rgdp', 'unemployment',  'expendituresfor',\
    'direct_expenditures', 'indirect_expenditures', 'Republican', 'sen'])))
    df.head(1)  

    cid     date    log_diff_rgdp   unemployment    leg_totalbills  years_exp   Republican  sen     billsum_support     expendituresfor     direct_expenditures     indirect_expenditures
0   N00013870   2007    0.026069    4.6     44  5   1.0     1.0     1.0     4.0     4.0     0.0


df=df.T.to_panel()

df=df.transpose(2,0,1)

df

<class 'pandas.core.panel.Panel'>
Dimensions: 505 (items) x 10 (major_axis) x 72 (minor_axis)
Items axis: N00000010 to N00035686
Major_axis axis: 2005 to 2014
Minor_axis axis: index to indirect_expenditures

我的理解(我想我可能错了)Items axis 包含所有panels; Minor_axis 包含每个 panels 中的所有列;并且Major_axis 是time index。我已经发布了第一行数据,然后将其发送到Panel,billsum_support 是最后一列的第四行;但是,当我尝试使用 billsum_support 作为 Y 变量进行回归时,出现以下错误。

reg=PanelOLS(y=df['billsum_support'],x=df[['years_exp', 'unemployment', 'dir_ind_expendituresfor']],time_effects=True)
reg
KeyError                                  Traceback (most recent call last)
/home/jayaramdas/anaconda3/lib/python3.5/site-packages/pandas/indexes/base.py in get_loc(self, key, method, tolerance)
   1875             try:
-> 1876                 return self._engine.get_loc(key)
   1877             except KeyError:

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4027)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3891)()

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12408)()

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12359)()

KeyError: 'billsum_support'

我已经看到了工作示例here,但这个人的数据似乎是堆叠格式而不是面板。 是否有人对 OLS Panel 有一定的经验并且可以理解我在这里做错了什么?

【问题讨论】:

    标签: python pandas ipython regression


    【解决方案1】:

    我明白了;跟进ptrj,并进行一些简单的探索,我找到了解决方案并将其发布在问题中

    df=df.pivot_table(index='date',columns='cid', fill_value=0,aggfunc=np.mean)
    
    df=df.T.to_panel()
    
    df=df.transpose(2,1,0)
    
    df=df.to_frame()
    

    【讨论】:

      猜你喜欢
      • 2020-08-07
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-21
      • 2012-11-06
      • 1970-01-01
      相关资源
      最近更新 更多