4

I want to get decomposed by statsmodels STL method

my time series data looks like bellow:

         success.rate
Date
2020-09-11  24.735701
2020-09-14  24.616301
2020-09-15  24.695900
2020-09-16  24.467051
2020-09-17  24.118799

when I put it into STL like

STL(sdf, seasonal=20, robust=True)

I always get error like:

--------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/mnt/d/mywork/test
 STL(sdf,seasonal=20, robust=True)
----> 1 STL(sdf, seasonal=20, robust=True)

statsmodels/tsa/_stl.pyx in statsmodels.tsa._stl.STL.__init__()

ValueError: Unable to determine period from endog
yunfei
  • 526
  • 2
  • 6
  • 20

1 Answers1

6

If your time series does not have a known frequency on the index (e.g., sdf.index.freq is None, then you need to set the period of the seasonality using the period. seasonal tells STL how many full seasons to use in the seasonal LOWESS but doesn't tell STL how many observations are needed for a full period.

from statsmodels.datasets import co2
from statsmodels.tsa.seasonal import STL
import matplotlib.pyplot as plt
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

data = co2.load(True).data
data = data.resample('M').mean().ffill()

# Remove freq info
data.index = [i for i in range(data.shape[0])]

res = STL(data, period=12).fit()
res.plot()
plt.show()

This code produces

STL Demonstration

Kevin S
  • 2,595
  • 16
  • 22