Loess 用 Python 对时间序列进行季节性分解

2022-01-11 00:00:00 python time-series r

问题描述

我正在尝试用 Python 做 R 上的 STL 函数.

I'm trying to do with Python what I the STL function on R.

R 命令是

fit <- stl(elecequip, s.window=5)
plot(fit)

如何在 Python 中做到这一点?我调查了 statmodels.tsa 有一些时间序列分析功能,但我可以在文档中专门找到黄土对时间序列的季节性分解".同样,在 Python.org 上有一个名为 timeseries 0.5.0 的库,但它没有文档,而且它的主页向下看.我知道 rpy2 有一个使用包装器的选项,但我不知道如何使用包装器.

How do I do this in Python? I investigated that statmodels.tsa has some time series analysis functions but I could specifically found "Seasonal Decomposition of Time Series by Loess" in the documentation. Similarly on Python.org there is a library called timeseries 0.5.0, but this doesn't have documentation and it's home site looks down. I know that there is an option with rpy2 using a wrapper, but I don't know how to use the wrapper.

谢谢.


解决方案

我一直有类似的问题,正在努力寻找最佳的前进道路.

I've been having a similar issue and am trying to find the best path forward.

这是一个基于 Loess 过程的 STL 分解的 github 存储库.它基于本文提供的原始fortran代码.它实际上只是原始 Fortran 代码的 Python 包装器,因此您知道它可能运行良好且没有错误.

Here is a github repo for an STL decomposition based on the Loess procedure. It is based on the original fortran code that was available with this paper. It is really just a python wrapper around the original Fortran code, so you know its likely works well and isn't buggy.

如果您想要更以 Python 为中心的东西,并且愿意使用稍微简单的分解例程,StatsModels 有一个:

If you want something more Python centric and are willing to go with a slightly simpler decomposition routine, StatsModels has one:

尝试将您的数据移动到 Pandas 数据帧中,然后调用 StatsModels tsa.seasonal_decompose.请参阅以下示例:

Try moving your data into a Pandas DataFrame and then call StatsModels tsa.seasonal_decompose. See the 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()

然后您可以从以下位置恢复分解的各个组件:

You can then recover the individual components of the decomposition from:

res.resid
res.seasonal
res.trend

我希望这会有所帮助!

相关文章