簡體   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