Skip to content

Commit 928da9d

Browse files
committed
ENH: use filtered exog index for the y_predict result, per #1027 close #1008
1 parent 015b31e commit 928da9d

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

pandas/stats/ols.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -361,13 +361,27 @@ def var_beta(self):
361361
@cache_readonly
362362
def _y_fitted_raw(self):
363363
"""Returns the raw fitted y values."""
364-
return self.sm_ols.fittedvalues
364+
if self._weights is None:
365+
X = self._x_filtered.values
366+
else:
367+
# XXX
368+
return self.sm_ols.fittedvalues
369+
370+
b = self._beta_raw
371+
return np.dot(X, b)
365372

366373
@cache_readonly
367374
def y_fitted(self):
368375
"""Returns the fitted y values. This equals BX."""
369-
result = Series(self._y_fitted_raw, index=self._y.index)
370-
return result.reindex(self._y_orig.index)
376+
if self._weights is None:
377+
index = self._x_filtered.index
378+
orig_index = index
379+
else:
380+
index = self._y.index
381+
orig_index = self._y_orig.index
382+
383+
result = Series(self._y_fitted_raw, index=index)
384+
return result.reindex(orig_index)
371385

372386
@cache_readonly
373387
def _y_predict_raw(self):

pandas/stats/tests/test_ols.py

+20
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,26 @@ def test_predict(self):
314314
pred4 = model1.predict(beta=beta)
315315
assert_series_equal(Series(0., pred4.index), pred4)
316316

317+
def test_predict_longer_exog(self):
318+
exogenous = {"1998": "4760","1999": "5904","2000": "4504",
319+
"2001": "9808","2002": "4241","2003": "4086",
320+
"2004": "4687","2005": "7686","2006": "3740",
321+
"2007": "3075","2008": "3753","2009": "4679",
322+
"2010": "5468","2011": "7154","2012": "4292",
323+
"2013": "4283","2014": "4595","2015": "9194",
324+
"2016": "4221","2017": "4520"}
325+
endogenous = {"1998": "691", "1999": "1580", "2000": "80",
326+
"2001": "1450", "2002": "555", "2003": "956",
327+
"2004": "877", "2005": "614", "2006": "468",
328+
"2007": "191"}
329+
330+
endog = Series(endogenous)
331+
exog = Series(exogenous)
332+
model = ols(y=endog, x=exog)
333+
334+
pred = model.y_predict
335+
self.assert_(pred.index.equals(exog.index))
336+
317337
def test_longpanel_series_combo(self):
318338
wp = tm.makePanel()
319339
lp = wp.to_frame()

0 commit comments

Comments
 (0)