13

Have been working with time series in Python, and using sm.tsa.seasonal_decompose. In the docs they introduce the function like this:

We added a naive seasonal decomposition tool in the same vein as R’s decompose.

Here is a copy of the code from the docs and its output:

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)
res.plot()

seasonal decomposition plot

They say it is naive but there is no disclaimer about what is wrong with it. Does anyone know?

cardamom
  • 6,873
  • 11
  • 48
  • 102
  • There is nothing wrong with it. I guess it's "naive" because it uses simple convolution and averages to separate out trend and seasonal structure, i.e. it's not a fancy algorithm. – Josef Nov 02 '17 at 14:49
  • Thanks, @user333700 , I wonder if R's `decompose` is also "naive" and if and when it is ever necessary to go fancy. Get the impression given that `.seasonal_decompose` worked on my data that it's probably all you typically need. – cardamom Nov 02 '17 at 15:10
  • 1
    One possibility where it is most likely too "naive", i.e. doesn't capture the features of the data appropriately, is shifting seasonal patterns, for example because of a trend or break in the pattern. Sometimes those can also be removed by a box-cox or similar transformation. Seasonal adjustments for macro economic data (e.g. by X11) also take shifts in holidays and similar calendar effects into account. – Josef Nov 02 '17 at 16:02

1 Answers1

22

I made some (aehm...naive) researches, and, according to the reference, it seems that StatsModels uses the classic moving average method to detect trends and apply seasonal decomposition (you can check more here, specifically about Moving Average and Classical Decomposition).

However, other advanced seasonal decomposition techniques are available, such as STL decomposition, which also has some Python implementations. (UPDATE - 11/04/2019 as pointed out in the comments by @squarespiral, such implementations appear to have been merged in the master branch of StatsModels).

At the above links, you can find a complete reference on the advantages and disadvantages of each one of the proposed methods.

Hope it helps!

  • 3
    The "Python implementation" of STL decomposition is apparently already merged into statsmodels: https://github.com/statsmodels/statsmodels/issues/4044 - the documentation for the function is here: https://www.statsmodels.org/dev/examples/notebooks/generated/stl_decomposition.html – squarespiral Oct 31 '19 at 15:15
  • Thanks @squarespiral, I updated the answer according to your comment. – Angelo Cardellicchio Nov 04 '19 at 12:30
  • Great answer. Still, the +1 was mostly for the pun. – Riley Jul 01 '20 at 09:09