Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seasonal Decomposition of Time Series by Loess with Python

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

The R commands are

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

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.

Thanks.

like image 758
user3724295 Avatar asked Oct 20 '14 16:10

user3724295


People also ask

What is seasonal decomposition in time series?

Time series decomposition involves thinking of a series as a combination of level, trend, seasonality, and noise components. Decomposition provides a useful abstract model for thinking about time series generally and for better understanding problems during time series analysis and forecasting.

What is Loess decomposition?

Seasonal-Trend decomposition using LOESS (STL) is a robust method of time series decomposition often used in economic and environmental analyses. The STL method uses locally fitted regression models to decompose a time series into trend, seasonal, and remainder components.


2 Answers

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

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.

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

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()

Three plots produced from above input

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

res.resid
res.seasonal
res.trend

I hope this helps!

like image 71
AN6U5 Avatar answered Sep 17 '22 07:09

AN6U5


You can also call R from python using rpy2

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

The above function assumes that your series has a datetime index. It returns a dataframe with the individual components that you can then graph with your favorite graphing library.

You can pass the parameters for stl seen here, but change any period to underscore, for example the positional argument in the above function is s_window, but in the above link it is s.window. Also, I found some of the above code on this repository.

like image 21
Jeff Tilton Avatar answered Sep 20 '22 07:09

Jeff Tilton