2016-05-09 13 views
0

パンダで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 axispanelsのすべてが含まれていることを(私はこのことについて間違っていると思う)私の理解です。 Minor_axisにはそれぞれpanelsのすべての列が含まれています。 Major_axistime indexです。私はPanelにそれを送る前に私のデータの最初の行を掲示し、billsum_supportは最後の列から4番目です;しかし、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の経験があり、私がここで間違っていることを理解できる人がいますか?

答えて

0

私はそれを得ました。 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() 
関連する問題