- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
statsmodels 的 ARIMA 为我的输出提供了不准确的答案。我想知道是否有人可以帮助我了解我的代码有什么问题。
这是一个示例:
import pandas as pd
import numpy as np
import datetime as dt
from statsmodels.tsa.arima_model import ARIMA
# Setting up a data frame that looks twenty days into the past,
# and has linear data, from approximately 1 through 20
counts = np.arange(1, 21) + 0.2 * (np.random.random(size=(20,)) - 0.5)
start = dt.datetime.strptime("1 Nov 01", "%d %b %y")
daterange = pd.date_range(start, periods=20)
table = {"count": counts, "date": daterange}
data = pd.DataFrame(table)
data.set_index("date", inplace=True)
print data
count
date
2001-11-01 0.998543
2001-11-02 1.914526
2001-11-03 3.057407
2001-11-04 4.044301
2001-11-05 4.952441
2001-11-06 6.002932
2001-11-07 6.930134
2001-11-08 8.011137
2001-11-09 9.040393
2001-11-10 10.097007
2001-11-11 11.063742
2001-11-12 12.051951
2001-11-13 13.062637
2001-11-14 14.086016
2001-11-15 15.096826
2001-11-16 15.944886
2001-11-17 17.027107
2001-11-18 17.930240
2001-11-19 18.984202
2001-11-20 19.971603
其余代码用于设置 ARIMA 模型。
# Setting up ARIMA model
order = (2, 1, 2)
model = ARIMA(data, order, freq='D')
model = model.fit()
print model.predict(1, 20)
2001-11-02 1.006694
2001-11-03 1.056678
2001-11-04 1.116292
2001-11-05 1.049992
2001-11-06 0.869610
2001-11-07 1.016006
2001-11-08 1.110689
2001-11-09 0.945190
2001-11-10 0.882679
2001-11-11 1.139272
2001-11-12 1.094019
2001-11-13 0.918182
2001-11-14 1.027932
2001-11-15 1.041074
2001-11-16 0.898727
2001-11-17 1.078199
2001-11-18 1.027331
2001-11-19 0.978840
2001-11-20 0.943520
2001-11-21 1.040227
Freq: D, dtype: float64
如您所见,数据在 1
附近保持不变,而不是增加。我在这里做错了什么?
(附带说明,出于某种原因,我无法将像 "2001-11-21"
这样的字符串日期传递到预测函数中。知道原因会很有帮助。)
最佳答案
您使用 predict
的方式返回线性预测差分内生变量不是对原始内生变量水平的预测。
您必须为 predict
方法提供 typ='levels'
以更改此行为:
preds = fit.predict(1, 30, typ='levels')
参见 ARIMAResults.predict
的文档了解详情。
我们加载您在 MCVE 中提供的数据:
import io
import pandas as pd
raw = io.StringIO("""date count
2001-11-01 0.998543
2001-11-02 1.914526
2001-11-03 3.057407
2001-11-04 4.044301
2001-11-05 4.952441
2001-11-06 6.002932
2001-11-07 6.930134
2001-11-08 8.011137
2001-11-09 9.040393
2001-11-10 10.097007
2001-11-11 11.063742
2001-11-12 12.051951
2001-11-13 13.062637
2001-11-14 14.086016
2001-11-15 15.096826
2001-11-16 15.944886
2001-11-17 17.027107
2001-11-18 17.930240
2001-11-19 18.984202
2001-11-20 19.971603""")
data = pd.read_fwf(raw, parse_dates=['date'], index_col='date')
正如我们预期的那样,数据是自相关的:
from pandas.plotting import autocorrelation_plot
autocorrelation_plot(data)
我们创建一个 ARIMA
Model给定设置 (P,D,Q)
的对象,我们使用 fit
在我们的数据上对其进行训练方法:
from statsmodels.tsa.arima_model import ARIMA
order = (2, 1, 2)
model = ARIMA(data, order, freq='D')
fit = model.fit()
它返回一个 ARIMAResults
感兴趣的对象。我们可以检查模型的质量:
fit.summary()
ARIMA Model Results
==============================================================================
Dep. Variable: D.count No. Observations: 19
Model: ARIMA(2, 1, 2) Log Likelihood 25.395
Method: css-mle S.D. of innovations 0.059
Date: Fri, 18 Jan 2019 AIC -38.790
Time: 07:54:36 BIC -33.123
Sample: 11-02-2001 HQIC -37.831
- 11-20-2001
==============================================================================
coef std err z P>|z| [0.025 0.975]
------------------------------------------------------------------------------
const 1.0001 0.014 73.731 0.000 0.973 1.027
ar.L1.D.count -0.3971 0.295 -1.346 0.200 -0.975 0.181
ar.L2.D.count -0.6571 0.230 -2.851 0.013 -1.109 -0.205
ma.L1.D.count 0.0892 0.208 0.429 0.674 -0.318 0.496
ma.L2.D.count 1.0000 0.640 1.563 0.140 -0.254 2.254
Roots
==============================================================================
Real Imaginary Modulus Frequency
------------------------------------------------------------------------------
AR.1 -0.3022 -1.1961j 1.2336 -0.2894
AR.2 -0.3022 +1.1961j 1.2336 0.2894
MA.1 -0.0446 -0.9990j 1.0000 -0.2571
MA.2 -0.0446 +0.9990j 1.0000 0.2571
------------------------------------------------------------------------------
我们可以粗略估计残差是如何分布的:
residuals = pd.DataFrame(fit.resid, columns=['residuals'])
residuals.plot(kind='kde')
如果我们对我们的模型感到满意,那么我们可以预测一些样本内或样本外的数据。
这可以通过 predict
来完成默认情况下返回差分内生变量而不是内生变量本身的方法。要更改此行为,我们必须指定 typ='levels'
:
preds = fit.predict(1, 30, typ='levels')
那么我们的预测确实与我们的训练数据具有相同的水平:
此外,如果我们有兴趣也有置信区间,那么我们可以使用 forecast
方法。
也可以为 predict
提供字符串(如果您想避免麻烦,请始终使用 ISO-8601 格式)或 datetime
对象:
preds = fit.predict("2001-11-02", "2001-12-15", typ='levels')
在 StatsModels 0.9.0 上按预期工作:
import statsmodels as sm
sm.__version__ # '0.9.0'
关于 python 统计模型 : Help using ARIMA model for time series,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31690134/
我有一个字符串,我想多次使用它来输出 configure --help .所以我尝试在 configure.ac 中做这样的事情: AC_ARG_ENABLE([foobar],
我无法使其正确匹配。它只打印“[help]”,但我希望它匹配括号内的字符。 想要: [help] help 代码: Pattern p = Pattern.compile("\\[(\
我刚刚将vim74下载并安装到我的linux机器上。我只是为用户安装在本地。当我进入vim并执行:help时,我得到了错误。 我尝试添加: let $VIM='home/myuser/vim74' l
当我打开一个 Xcode 项目时,快速帮助功能工作得很好,但是当打开一个不在打开的项目中的文件(例如 abc.swift )时,文档不会出现。 下面是一个例子: 最佳答案 安装版本Command Li
我有一个 python click CLI。当我将 --help 传递给任何命令时,它会打印一条我喜欢的帮助消息。我发现现在很多用户都在输入 mycli help foo 而不是 mycli foo
我一直在使用 less 进行前端开发,但最近几天我遇到了这个错误。 我正在使用 PhpStorm 的观察器将 less 文件编译为 css 文件。但是当我编辑 less 文件时,编译器将这一行添加到
当我在 python 解释器中键入 help('string') 时,我得到了有关字符串类的信息。在那里,upper() 被表示为一个函数。然而,我只能将其称为 "hi".upper() 而不是 up
我希望 help 选项的作用与 --help 相同。 旁注:我创建了一个程序,其命令行行为与 svn 或 hg 相同。我设法用子解析器做到了这一点。但是我想让事情保持一致。这就是为什么我希望 help
我是 Maven 的新手。你知道我做错了什么吗(见下文)? mvn -e clean:help. + Error stacktraces are turned on. [INFO] Scannin
help format-list输出格式列表的帮助。 format-list | help输出帮助(get-help)。 最佳答案 该|或“管道”运算符将一个操作的输出重定向到另一个。因此,在这种情况
尝试从脚本检索帮助会出现以下错误: Get-Help : Cannot find Help for topic ".\Process-Test.ps1". At line:1 char:9 + get
我从常规的discord.py更改为重写版本,并且我曾经有一个自定义的!help命令。 现在,每当我发布 !help 时,它都会显示此内容以及编码的帮助消息: No Category: help S
我开发跨平台桌面应用程序,最近我发现了在小部件上使用帮助文本的乐趣。然后用户在标题栏上有一个问号按钮,单击该按钮后,他可以单击窗口中的任何小部件并查看有关它的帮助文本。非常好。 因为我认为所有程序都应
Xcode 4.5.2 Mountain Lion,Mac 应用 我严格遵守文档。帮助文件夹及其子文件夹已添加到项目/Resources 文件夹并显示为蓝色。 如有必要,文件夹引用由 xcode 添加
emulator documentation说: -audio-in Use the specified audio-input backend. 它还说 -help- Print
最近在学习argparse模块,代码下方出现Argument错误 import argparse import sys class ExecuteShell(object): def crea
我正在阅读有关 Maven 的教程,应该提到我对此一无所知。我已经成功地完成了第一章和第二章,这是关于它的设置的。但是在第 3 章“Maven Pom”中我遇到了问题。在最后一段中,它说在你想要的任何
我注意到 Windows 有这个问题,但我运行的是 Ubuntu 13.10(带有 Bash),所以我不知道出了什么问题。 尝试设置我的副本集,我运行以下命令: sudo bash # Accout
很抱歉,我在 python 程序中遇到了一个尴尬的问题。首先我是这里的新手,我是一名中国学生。请原谅我糟糕的英语。我会尽力把问题表达清楚。 错误代码在这里: def __find__(
好的,我试图环顾四周并弄清楚如何做到这一点,但无法弄清楚。这就是我想要做的。 # Run script normally .\myscript.ps1 "blah" "yo" 这些应该做同样的事情 G
我是一名优秀的程序员,十分优秀!