问题描述
我正在尝试用 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 有一些时间序列分析功能,但我可以在文档中特别找到Loess 的季节性分解时间序列".同样在 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 DataFrame 中,然后调用 统计模型 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
我希望这会有所帮助!
这篇关于用 Python 对 Loess 的时间序列进行季节性分解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!