从 OLS 获得一个简单的预测,不同于 StatsModels 的 .6 到 .8
Getting a simple predict from OLS something different from .6 to .8 of StatsModels
很抱歉交叉发布这个但无法通过它我无法从预测函数获得输出:
我有一个用于 SM .6 的 OLS 模型,现在不能在 .8 中使用并且 Pandas 从 19.2 增加到 20.3 所以这可能是问题所在?
我只是不明白我需要向预测方法提供什么。
所以我的模型创建看起来像:
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
这很好用,我得到了一个模型,可以很好地看到摘要。
我曾经这样做是为了通过使用我在 SM .6 中工作的最新值(我想向前预测)来提前一个时期进行预测
预测调用如下:
yrahead=ols_test.predict(ols_input)
ols 输入是从 pandas DF:
创建的
ols_input=(sm.add_constant(merged2.lastqu[-1:], prepend=True))
lastqu
2018-12-31 13209.0
type:
<class 'pandas.core.frame.DataFrame'>
调用预测为:
yrahead=ols_test.predict(ols_input)
这给了我一个错误:
ValueError:形状 (1,1) 和 (2,) 未对齐:1 (dim 1) != 2 (dim 0)
我尝试通过将 ols_input 更改为:
来简单地输入数字
13209.0
Type:
<class 'numpy.float64'>
这给了我一个类似的错误:
ValueError:形状 (1,1) 和 (2,) 未对齐:1 (dim 1) != 2 (dim 0)
不确定去哪里?
上面的基础 DataFrame table (merged2) 看起来像最后一行 lastqu 列包含我想要预测的值单位:
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
所以我使用 OLS 对 lastqu 来预测 2018 年的单位
我坦率地承认,我并不真正理解为什么 SM .6 会这样工作,但它确实做到了!
在与 Statsmodels 的库作者讨论后,似乎存在一个错误,请参阅此处的讨论 https://groups.google.com/d/topic/pystatsmodels/a0XsXIiP5ro/discussion
请注意,我针对特定问题的最终解决方案是:
ols_input=np.array([1,merged2.lastqu[-1:].values])
yrahead=ols_test.predict(ols_input)
产生下一个周期的单位..
很抱歉交叉发布这个但无法通过它我无法从预测函数获得输出:
我有一个用于 SM .6 的 OLS 模型,现在不能在 .8 中使用并且 Pandas 从 19.2 增加到 20.3 所以这可能是问题所在?
我只是不明白我需要向预测方法提供什么。 所以我的模型创建看起来像:
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
这很好用,我得到了一个模型,可以很好地看到摘要。 我曾经这样做是为了通过使用我在 SM .6 中工作的最新值(我想向前预测)来提前一个时期进行预测 预测调用如下:
yrahead=ols_test.predict(ols_input)
ols 输入是从 pandas DF:
创建的 ols_input=(sm.add_constant(merged2.lastqu[-1:], prepend=True))
lastqu
2018-12-31 13209.0
type:
<class 'pandas.core.frame.DataFrame'>
调用预测为:
yrahead=ols_test.predict(ols_input)
这给了我一个错误: ValueError:形状 (1,1) 和 (2,) 未对齐:1 (dim 1) != 2 (dim 0)
我尝试通过将 ols_input 更改为:
来简单地输入数字13209.0
Type:
<class 'numpy.float64'>
这给了我一个类似的错误: ValueError:形状 (1,1) 和 (2,) 未对齐:1 (dim 1) != 2 (dim 0)
不确定去哪里?
上面的基础 DataFrame table (merged2) 看起来像最后一行 lastqu 列包含我想要预测的值单位:
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
所以我使用 OLS 对 lastqu 来预测 2018 年的单位
我坦率地承认,我并不真正理解为什么 SM .6 会这样工作,但它确实做到了!
在与 Statsmodels 的库作者讨论后,似乎存在一个错误,请参阅此处的讨论 https://groups.google.com/d/topic/pystatsmodels/a0XsXIiP5ro/discussion
请注意,我针对特定问题的最终解决方案是:
ols_input=np.array([1,merged2.lastqu[-1:].values])
yrahead=ols_test.predict(ols_input)
产生下一个周期的单位..