11
11
import numpy as np
12
12
13
13
from pandas .core .panel import LongPanel
14
- from pandas .core .api import DataFrame , Index , Series
14
+ from pandas .core .api import DataFrame , Index , Series , notnull
15
15
from pandas .stats .api import ols
16
- from pandas .stats .plm import NonPooledPanelOLS
16
+ from pandas .stats .plm import NonPooledPanelOLS , PanelOLS
17
17
from pandas .util .testing import (assert_almost_equal , assert_series_equal ,
18
18
assert_frame_equal )
19
- import pandas .util .testing as testing
19
+ import pandas .util .testing as tm
20
20
21
21
from common import BaseTest
22
22
@@ -40,10 +40,6 @@ def _compare_moving_ols(model1, model2):
40
40
41
41
class TestOLS (BaseTest ):
42
42
43
- FIELDS = ['beta' , 'df' , 'df_model' , 'df_resid' , 'f_stat' , 'p_value' ,
44
- 'r2' , 'r2_adj' , 'rmse' , 'std_err' , 't_stat' ,
45
- 'var_beta' ]
46
-
47
43
# TODO: Add tests for OLS y predict
48
44
# TODO: Right now we just check for consistency between full-sample and
49
45
# rolling/expanding results of the panel OLS. We should also cross-check
@@ -140,6 +136,10 @@ def checkMovingOLS(self, window_type, x, y, **kwds):
140
136
141
137
_check_non_raw_results (moving )
142
138
139
+ FIELDS = ['beta' , 'df' , 'df_model' , 'df_resid' , 'f_stat' , 'p_value' ,
140
+ 'r2' , 'r2_adj' , 'rmse' , 'std_err' , 't_stat' ,
141
+ 'var_beta' ]
142
+
143
143
def compare (self , static , moving , event_index = None ,
144
144
result_index = None ):
145
145
@@ -169,7 +169,7 @@ def compare(self, static, moving, event_index=None,
169
169
assert_almost_equal (ref , res )
170
170
171
171
def test_f_test (self ):
172
- x = testing .makeTimeDataFrame ()
172
+ x = tm .makeTimeDataFrame ()
173
173
y = x .pop ('A' )
174
174
175
175
model = ols (y = y , x = x )
@@ -185,8 +185,49 @@ def test_f_test(self):
185
185
186
186
self .assertRaises (Exception , model .f_test , '1*A=0' )
187
187
188
- class TestPanelOLS (BaseTest ):
188
+ class TestOLSMisc (unittest .TestCase ):
189
+ '''
190
+ For test coverage with faux data
191
+ '''
192
+
193
+ def test_r2_no_intercept (self ):
194
+ y = tm .makeTimeSeries ()
195
+ x = tm .makeTimeDataFrame ()
189
196
197
+ model1 = ols (y = y , x = x )
198
+
199
+ x_with = x .copy ()
200
+ x_with ['intercept' ] = 1.
201
+
202
+ model2 = ols (y = y , x = x_with , intercept = False )
203
+ assert_series_equal (model1 .beta , model2 .beta )
204
+
205
+ # TODO: can we infer whether the intercept is there...
206
+ self .assert_ (model1 .r2 != model2 .r2 )
207
+
208
+ def test_summary_many_terms (self ):
209
+ x = DataFrame (np .random .randn (100 , 20 ))
210
+ y = np .random .randn (100 )
211
+ model = ols (y = y , x = x )
212
+ model .summary
213
+
214
+ def test_y_predict (self ):
215
+ y = tm .makeTimeSeries ()
216
+ x = tm .makeTimeDataFrame ()
217
+ model1 = ols (y = y , x = x )
218
+ assert_series_equal (model1 .y_predict , model1 .y_fitted )
219
+
220
+ def test_longpanel_series_combo (self ):
221
+ wp = tm .makeWidePanel ()
222
+ lp = wp .to_long ()
223
+
224
+ y = lp .pop ('ItemA' )
225
+ model = ols (y = y , x = lp , entity_effects = True , window = 20 )
226
+ self .assert_ (notnull (model .beta .values ).all ())
227
+ self .assert_ (isinstance (model , PanelOLS ))
228
+ model .summary
229
+
230
+ class TestPanelOLS (BaseTest ):
190
231
191
232
FIELDS = ['beta' , 'df' , 'df_model' , 'df_resid' , 'f_stat' ,
192
233
'p_value' , 'r2' , 'r2_adj' , 'rmse' , 'std_err' ,
@@ -501,7 +542,7 @@ def compare(self, static, moving, event_index=None,
501
542
assert_almost_equal (ref , res )
502
543
503
544
def test_auto_rolling_window_type (self ):
504
- data = testing .makeTimeDataFrame ()
545
+ data = tm .makeTimeDataFrame ()
505
546
y = data .pop ('A' )
506
547
507
548
window_model = ols (y = y , x = data , window = 20 , min_periods = 10 )
0 commit comments