简体   繁体   English

Python Statsmodel ARIMA开始[平稳性]

[英]Python Statsmodel ARIMA start [stationarity]

I just began working on time series analysis using statsmodels. 我刚开始使用statsmodels进行时间序列分析。 I have a dataset with dates and values (for about 3 months). 我有一个包含日期和值的数据集(大约3个月)。 I am facing some issues with providing the right order to the ARIMA model. 我正面临着为ARIMA模型提供正确订单的一些问题。 I am looking to adjust for trends and seasonality and then compute outliers. 我希望调整趋势和季节性,然后计算异常值。

My 'values' are not stationary and statsmodel says that I have to either induce stationarity or provide some differencing to make it work. 我的'价值'不是固定不变的,statsmodel说我要么必须诱导平稳性,要么提供一些差异以使其有效。 I played around with different ordering (without understanding deeply about the consequences of changing p,q and d). 我玩了不同的顺序(没有深入了解改变p,q和d的后果)。

When I introduce 1 for differencing, I get this error: 当我为差异引入1时,我收到此错误:

ValueError: The start index -1 of the original series has been differenced away

When I remove the differencing by having my order as (say) order = (2,0,1), I get this error: 当我通过命令(例如)order =(2,0,1)删除差异时,我收到此错误:

    raise ValueError("The computed initial AR coefficients are not "
ValueError: The computed initial AR coefficients are not stationary
You should induce stationarity, choose a different model order, or you can
pass your own start_params.
>>> 

Any help on how to induce stationarity (or a link to a nice tutorial) would be helpful. 任何关于如何诱导平稳性(或指向一个很好的教程的链接)的帮助都会有所帮助。 And, also, tests of stationarity (like, http://www.maths.bris.ac.uk/~guy/Research/LSTS/TOS.html ) would be useful. 而且,平稳性的测试(例如, http://www.maths.bris.ac.uk/~guy/Research/LSTS/TOS.html )将是有用的。

Update: I am reading through ADF test: 更新:我正在通过ADF测试阅读:

http://statsmodels.sourceforge.net/stable/generated/statsmodels.tsa.stattools.adfuller.html

Thanks! 谢谢! PD. PD。

To induce stationarity: 诱导平稳性:

  1. de-seasonalize (remove seasonality) 去季节化(去除季节性)
  2. de-trend (remove trend) 去趋势(去除趋势)

There are several ways to achieve stationarity of a time series - Box-Cox family of transformations, Differencing etc., Choice of method depends on the data. 有几种方法可以实现时间序列的平稳性 - Box-Cox族的变换,差分等,方法的选择取决于数据。 Below are the commonly used tests for stationarity. 以下是常用的常用测试。

Tests for stationarity: 1. Augmented Dickey-Fuller test 2. KPSS test KPSS python code 测试平稳性:1。增强Dickey-Fuller测试2. KPSS测试KPSS python代码

You can use R script instead statmodels. 您可以使用R脚本代替statmodels。 R is more powerful for statistical estimation. R对统计估计更有效。

If you want use python, you can run R-script from a python through os interface: 如果你想使用python,你可以通过os接口从python运行R脚本:

for example R script for arima estimation "arimaestimation.r": 例如用于arima估计的R脚本“arimaestimation.r”:

library(rjson)

args <- commandArgs(trailingOnly=TRUE)

jsonstring = ''

for(i in seq(0, length(args))) {
    if ( length(args[i]) && args[i]=='--jsondata' ) {
        jsonstring = args[i+1]
    }
}

jsonobject = fromJSON(jsonstring)
data = as.numeric(unlist(jsonobject['data']))
p = as.numeric(unlist(jsonobject['p']))
d = as.numeric(unlist(jsonobject['d']))
q = as.numeric(unlist(jsonobject['q']))

estimate = arima(data, order=c(p, d, q))

phi = c()
if (p>0) {
    for (i in seq(1, p)) {
        phi = c(phi, as.numeric(unlist(estimate$coef[i])))
    }
}
theta = c()
if (p+1 <= p+q) {
    for (i in seq(p+1, p+q)) {
        theta = c(theta, as.numeric(unlist(estimate$coef[i])))
    }
}
if (d==0) {
    intercept = as.numeric(unlist(estimate$coef[p+q+1]))
} else {
    intercept = 0.0
}

if (length(phi)) {
    if (length(phi)==1) {
        phi = list(phi)
    }
} else {
    phi = list()
}

if (length(theta)) {
    if (length(theta)==1) {
        theta = list(-1 * theta)
    } else {
        theta = -1 * theta
    }
} else {
    theta = list()
}

arimapredict = predict(estimate, n.ahead = 12)
prediction = as.numeric(unlist(arimapredict$pred))
predictionse = as.numeric(unlist(arimapredict$se))

response = list(phi=phi,
                theta=theta,
                intercept=intercept,
                sigma2=estimate$sigma2,
                aic=estimate$aic,
                prediction=prediction,
                predictionse=predictionse)

cat(toJSON(response))

And call him with python through json interface: 并通过json接口用python调用他:

Rscript arima/arimaestimate.r --jsondata '{"q": 2, "p": 2, "data": [247.0, 249.0, 213.0, 154.0, 122.0, 164.0, 141.0, 174.0, 281.0, 141.0, 159.0, 168.0, 243.0, 261.0, 211.0, 303.0, 308.0, 239.0, 237.0, 185.0], "d": 1}'

and you get the answer: 你得到答案:

{
    "phi": [],
    "theta": [
        0.407851844478153
    ],
    "intercept": 0,
    "sigma2": 3068.29837379914,
    "aic": 210.650287294343,
    "prediction": [
        210.184175597721,
        210.184175597721,
        210.184175597721,
        210.184175597721,
        210.184175597721,
        210.184175597721,
        210.184175597721,
        210.184175597721,
        210.184175597721,
        210.184175597721,
        210.184175597721,
        210.184175597721
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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