Python Statsmodel ARIMA 开始 [平稳性]

11 投票
2 回答
9434 浏览
提问于 2025-04-18 10:26

我刚开始使用statsmodels进行时间序列分析。我有一个包含日期和数值的数据集(大约有3个月的数据)。我在给ARIMA模型提供正确的顺序时遇到了一些问题。我想调整趋势和季节性,然后计算异常值。

我的“数值”不是平稳的,statsmodels说我必须要么让它变得平稳,要么进行一些差分处理才能让它工作。我尝试了不同的顺序(但对改变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.

2 个回答

0

你可以用R脚本来代替statmodels。R在统计估算方面更强大。

如果你想用Python,可以通过操作系统接口从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
    ]
}
2

为了让时间序列数据变得平稳,我们可以采取以下步骤:

  1. 去季节性(去掉季节变化的影响)
  2. 去趋势(去掉长期变化的影响)

有很多方法可以让时间序列数据变得平稳,比如使用Box-Cox变换、差分等。具体用哪种方法要看你的数据情况。下面是一些常用的平稳性检验方法。

平稳性检验的方法有: 1. 增强型迪基-福勒检验 2. KPSS检验 KPSS的Python代码

撰写回答