繁体   English   中英

Python Statsmodel ARIMA开始[平稳性]

[英]Python Statsmodel ARIMA start [stationarity]

我刚开始使用statsmodels进行时间序列分析。 我有一个包含日期和值的数据集(大约3个月)。 我正面临着为ARIMA模型提供正确订单的一些问题。 我希望调整趋势和季节性,然后计算异常值。

我的'价值'不是固定不变的,statsmodel说我要么必须诱导平稳性,要么提供一些差异以使其有效。 我玩了不同的顺序(没有深入了解改变p,q和d的后果)。

当我为差异引入1时,我收到此错误:

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

当我通过命令(例如)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.
>>> 

任何关于如何诱导平稳性(或指向一个很好的教程的链接)的帮助都会有所帮助。 而且,平稳性的测试(例如, http://www.maths.bris.ac.uk/~guy/Research/LSTS/TOS.html )将是有用的。

更新:我正在通过ADF测试阅读:

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

谢谢! PD。

诱导平稳性:

  1. 去季节化(去除季节性)
  2. 去趋势(去除趋势)

有几种方法可以实现时间序列的平稳性 - Box-Cox族的变换,差分等,方法的选择取决于数据。 以下是常用的常用测试。

测试平稳性:1。增强Dickey-Fuller测试2. KPSS测试KPSS python代码

您可以使用R脚本代替statmodels。 R对统计估计更有效。

如果你想使用python,你可以通过os接口从python运行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))

并通过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}'

你得到答案:

{
    "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