2013-12-19 45 views
34

時間序列分解是一種將時間序列數據集分隔爲三個(或更多)組件的方法。例如:Python中的時間序列分解函數

x(t) = s(t) + m(t) + e(t) 

其中

t is the time coordinate 
x is the data 
s is the seasonal component 
e is the random error term 
m is the trend 

在R I會做的功能decomposestl。我將如何在Python中做到這一點?

回答

-1

你已經介紹到scipy了嗎?從我在幾個PDF文件/網站

HereHere

已經看到這是可行的。但是,如果沒有看到具體的例子,就很難有人向您展示代碼示例。 Scipy是真棒我用它在我的研究的東西,仍然沒有讓它失望。

+2

是的我目前正在使用Scipy,包括statsmodel,pandas和numpy。我能找到的最接近的東西是使用來自熊貓的「resample」,但是這不允許你對時間序列進行解碼。 – user3084006

44

我一直有一個類似的問題並試圖找到最好的路徑。嘗試將您的數據移入Pandas數據幀,然後撥打StatsModelstsa.seasonal_decompose。見following example

import statsmodels.api as sm 

dta = sm.datasets.co2.load_pandas().data 
# deal with missing values. see issue 
dta.co2.interpolate(inplace=True) 

res = sm.tsa.seasonal_decompose(dta.co2) 
resplot = res.plot() 

Three plots produced from above input

然後,您可以從回收分解的各個組成部分:

res.resid 
res.seasonal 
res.trend 

我希望這有助於!

+0

你如何重新構造組件的原始時間序列? – vgoklani

+0

您可以選擇是否通過減法或除法來解構它們。加法是典型的方法,在這種情況下,您只需將這些組件重新組合在一起。 – AN6U5

+0

在那個代碼片斷中,哪個變量保存輸入數據? – davneet

2

我已經回答了這個問題here,但是下面是關於如何用rpy2做到這一點的快速功能。這使您可以使用R的穩健統計分解與黃土,但在Python!

from rpy2.robjects import r 
def decompose(series, frequency, s_window, **kwargs): 
    df = pd.DataFrame() 
    df['date'] = series.index 
    s = [x for x in series.values] 
    length = len(series) 
    s = r.ts(s, frequency=frequency) 
    decomposed = [x for x in r.stl(s, s_window, **kwargs).rx2('time.series')] 
    df['observed'] = series.values 
    df['trend'] = decomposed[length:2*length] 
    df['seasonal'] = decomposed[0:length] 
    df['residual'] = decomposed[2*length:3*length] 
    return df 

上述函數假定您的系列具有日期時間索引。它返回一個數據框,其中包含您可以使用您最喜歡的圖形庫進行圖形化的單個組件。

你可以通過參數stl看到here,但改變任何週期爲下劃線,例如上述函數中的位置參數是s_window,但在上面的鏈接中是s.window。另外,我在this repository上發現了一些上述代碼。

相關問題