简体   繁体   中英

Getting a simple predict from OLS something different from .6 to .8 of StatsModels

Sorry for cross posting this but can't get past it I cannot get output from the predict function:

I have an OLS model that used to work with SM .6 and now not working in .8 and Pandas increased from 19.2 to 20.3 so that could be the issue?

I just don't understand what I need to feed to the predict method. So my model create looks like:

def fit_line2(x, y):
X = sm.add_constant(x, prepend=True) #Add a column of ones to allow the calculation of the intercept
ols_test = sm.OLS(y, X,missing='drop').fit()
"""Return slope, intercept of best fit line."""
X = sm.add_constant(x)
return ols_test

And that works fine and I get a model out and can see the summary fine. I used to do this to get the prediction one period ahead by using my latest value (on which I want to project forward) worked in SM .6 The predict is called as follows:

 yrahead=ols_test.predict(ols_input)

ols input is created from a pandas DF:

 ols_input=(sm.add_constant(merged2.lastqu[-1:], prepend=True))

 lastqu
 2018-12-31 13209.0  
 type:
 <class 'pandas.core.frame.DataFrame'>

calling predict as:

yrahead=ols_test.predict(ols_input)

This gives me an error: ValueError: shapes (1,1) and (2,) not aligned: 1 (dim 1) != 2 (dim 0)

I tried simply feeding the number by changing ols_input to:

13209.0
Type: 
<class 'numpy.float64'>

That gave me a similar error: ValueError: shapes (1,1) and (2,) not aligned: 1 (dim 1) != 2 (dim 0)

Not sure where to go here?

the base DataFrame table (merged2) from the above looks like so the last line lastqu column contains the value I want to predict Units for:

               Units      lastqu    Uperchg  lqperchg
2000-12-31  19391.000000      NaN      NaN       NaN
2001-12-31  35068.000000   5925.0    80.85       NaN
2002-12-31  39279.000000   8063.0    12.01     36.08
2003-12-31  47517.000000   9473.0    20.97     17.49
2004-12-31  51439.000000  11226.0     8.25     18.51
2005-12-31  59674.000000  11667.0    16.01      3.93
2006-12-31  58664.000000  14016.0    -1.69     20.13
2007-12-31  55698.000000  13186.0    -5.06     -5.92
2008-12-31  42235.000000  11343.0   -24.17    -13.98
2009-12-31  40478.333333   7867.0    -4.16    -30.64
2010-12-31  38721.666667   8114.0    -4.34      3.14
2011-12-31  36965.000000   8361.0    -4.54      3.04
2012-12-31  39132.000000   8608.0     5.86      2.95
2013-12-31  43160.000000   9016.0    10.29      4.74
2014-12-31  44520.000000   9785.0     3.15      8.53
2015-12-31  49966.000000  10351.0    12.23      5.78
2016-12-31  53752.000000  10884.0     7.58      5.15
2017-12-31  57571.000000  12109.0     7.10     11.26
2018-12-31           NaN  13209.0      NaN      9.08

So I'm using the OLS against the lastqu to project units for 2018

I freely confess to not really understanding why SM .6 worked the way it did, but it did!

After some discussion with The library author of Statsmodels it seems there is a bug see the discussion here https://groups.google.com/d/topic/pystatsmodels/a0XsXIiP5ro/discussion

Note my final solution for my specific issue was:

ols_input=np.array([1,merged2.lastqu[-1:].values])
yrahead=ols_test.predict(ols_input)

Which yields the Units for next period..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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