我已经回答了这个问题here,但下面是关于如何使用 rpy2 执行此操作的快速函数。这使您可以使用 R 的鲁棒统计分解与 loess,但在 python 中!
import pandas as pd
from rpy2.robjects import r, pandas2ri
import numpy as np
from rpy2.robjects.packages import importr
def decompose(series, frequency, s_window = 'periodic', log = False, **kwargs):
'''
Decompose a time series into seasonal, trend and irregular components using loess,
acronym STL.
https://www.rdocumentation.org/packages/stats/versions/3.4.3/topics/stl
params:
series: a time series
frequency: the number of observations per “cycle”
(normally a year, but sometimes a week, a day or an hour)
https://robjhyndman.com/hyndsight/seasonal-periods/
s_window: either the character string "periodic" or the span
(in lags) of the loess window for seasonal extraction,
which should be odd and at least 7, according to Cleveland
et al.
log: boolean. take log of series
**kwargs: See other params for stl at
https://www.rdocumentation.org/packages/stats/versions/3.4.3/topics/stl
'''
df = pd.DataFrame()
df['date'] = series.index
if log: series = series.pipe(np.log)
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).rx2('time.series')]
df['observed'] = series.values
df['trend'] = decomposed[length:2*length]
df['seasonal'] = decomposed[0:length]
df['residuals'] = decomposed[2*length:3*length]
return df
上述函数假定您的系列具有日期时间索引。它返回一个包含各个组件的数据框,然后您可以使用您最喜欢的图形库对其进行图形化。
你可以传递stl seenhere的参数,但是将任何句点更改为下划线,例如上面函数中的位置参数是s_window,但在上面的链接中它是s.window。另外,我在this repository 上找到了上面的一些代码。
示例数据
希望以下内容有效,老实说还没有尝试过,因为这是我回答问题很久之后的请求。
import pandas as pd
import numpy as np
obs_per_cycle = 52
observations = obs_per_cycle * 3
data = [v+2*i for i,v in enumerate(np.random.normal(5, 1, observations))]
tidx = pd.date_range('2016-07-01', periods=observations, freq='w')
ts = pd.Series(data=data, index=tidx)
df = decompose(ts, frequency=obs_per_cycle, s_window = 'periodic')