繁体   English   中英

熊猫:无法实施Panel OLS

[英]Pandas: Trouble implementing Panel OLS

我在理解如何在熊猫中实施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_axistime index 我已将数据的第一行发布到Panel并且billsum_support在最后一列是第四列; 但是,当我尝试使用billsum_support作为Y变量进行回归时, billsum_support以下错误。

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'

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

我知道了; 跟进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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM