传递给statsmodels OLS.predict的参数的含义

时间:2017-08-03 04:47:27

标签: python linear-regression statsmodels

我使用https://ipython.readthedocs.io/en/latest/install/kernel_install.html来学习一些介绍性的线性回归和预测。数据集包含两个功能;自上次喷发以来的时间以及随后喷发的持续时间:

    eruptions   waiting
0   3.600   79
1   1.800   54
2   3.333   74
3   2.283   62
4   4.533   85

以下是摘要数据:

Old Faithful Geyser Dataset

这是模特:

import statsmodels.api as sm

X = faithful.waiting
X = sm.add_constant(X)
y = faithful.eruptions

model = sm.OLS(y, X)
results = model.fit()

在尝试使用predict()进行预测时,我的问题出现了。例如,如果我等了75分钟,这次喷发会持续多久?

results.predict([1, 75]) # 1 needs to be passed, I don't know why

为什么通过1?是因为add_constant(X)添加了1?:

    const   waiting
0   1.0   79
1   1.0   54
2   1.0   74
3   1.0   62
4   1.0   85

Old faithful dataset summary说主要参数是"线性模型的参数" 唯一的变量是等待时间(75分钟),回归线无论如何都有自己的拦截(-1.87):

results.params
>>>
const     -1.874016
waiting    0.075628
dtype: float64

docs注意到:

  

model.predict不知道参数,并在通话中要求它们

但是,如果情况如此,那么-1.87不是最好的参数吗?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您正在学习y = Ax + b形式的等式的参数 您的模型显示A = 0.075628b = -1.87(b是偏差词) 传递两个值x = 75,b的乘数为1.

你也可以运行预测为, results.predict(sm.add_constant([75], has_constant='add'))