Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion pandas/stats/ols.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import numpy as np

from pandas.core.api import DataFrame, Series
from pandas.core.api import DataFrame, Series, isnull
from pandas.core.common import _ensure_float64
from pandas.core.index import MultiIndex
from pandas.core.panel import Panel
Expand Down Expand Up @@ -381,6 +381,58 @@ def y_predict(self):
For in-sample, this is same as y_fitted."""
return self.y_fitted

def predict(self, beta=None, x=None, fill_value=None,
fill_method=None, axis=0):
"""
Parameters
----------
beta : Series
x : Series or DataFrame
fill_value : scalar or dict, default None
fill_method : {'backfill', 'bfill', 'pad', 'ffill', None}, default None
axis : {0, 1}, default 0
See DataFrame.fillna for more details

Notes
-----
1. If both fill_value and fill_method are None then NaNs are dropped
(this is the default behavior)
2. An intercept will be automatically added to the new_y_values if
the model was fitted using an intercept

Returns
-------
Series of predicted values
"""
if beta is None and x is None:
return self.y_predict

if beta is None:
beta = self.beta
else:
beta = beta.reindex(self.beta.index)
if isnull(beta).any():
raise ValueError('Must supply betas for same variables')

if x is None:
x = self._x
orig_x = x
else:
orig_x = x
if fill_value is None and fill_method is None:
x = x.dropna(how='any')
else:
x = x.fillna(value=fill_value, method=fill_method, axis=axis)
if isinstance(x, Series):
x = DataFrame({'x' : x})
if self._intercept:
x['intercept'] = 1.

x = x.reindex(columns=self._x.columns)

rs = x.values.dot(beta.values)
return Series(rs, x.index).reindex(orig_x.index)

RESULT_FIELDS = ['r2', 'r2_adj', 'df', 'df_model', 'df_resid', 'rmse',
'f_stat', 'beta', 'std_err', 't_stat', 'p_value', 'nobs']

Expand Down
27 changes: 27 additions & 0 deletions pandas/stats/tests/test_ols.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,33 @@ def test_y_predict(self):
assert_series_equal(model1.y_predict, model1.y_fitted)
assert_almost_equal(model1._y_predict_raw, model1._y_fitted_raw)

def test_predict(self):
y = tm.makeTimeSeries()
x = tm.makeTimeDataFrame()
model1 = ols(y=y, x=x)
assert_series_equal(model1.predict(), model1.y_predict)
assert_series_equal(model1.predict(x=x), model1.y_predict)
assert_series_equal(model1.predict(beta=model1.beta), model1.y_predict)

exog = x.copy()
exog['intercept'] = 1.
rs = Series(exog.values.dot(model1.beta.values), x.index)
assert_series_equal(model1.y_predict, rs)

x2 = x.reindex(columns=x.columns[::-1])
assert_series_equal(model1.predict(x=x2), model1.y_predict)

x3 = x2 + 10
pred3 = model1.predict(x=x3)
x3['intercept'] = 1.
x3 = x3.reindex(columns = model1.beta.index)
expected = Series(x3.values.dot(model1.beta.values), x3.index)
assert_series_equal(expected, pred3)

beta = Series(0., model1.beta.index)
pred4 = model1.predict(beta=beta)
assert_series_equal(Series(0., pred4.index), pred4)

def test_longpanel_series_combo(self):
wp = tm.makePanel()
lp = wp.to_frame()
Expand Down