I am novice in time series analysis. I used statsmodels STL to decompose my time series into trend, seasonality and residuals. The problem is that the trend seems to be noisy. I want the trend to be linear because I believe the nature of the time series I am having will have a small linear growth. Instead i get a very jigsaw trend. Are there any python packages where i can specify how the trend is supposed to be?
2 Answers
statsmodels.tsa.seasonal STL has a couple of parameters that might help. From https://www.statsmodels.org/dev/examples/notebooks/generated/stl_decomposition.html
season - The length of the seasonal smoother. Must be odd.
trend - The length of the trend smoother, usually around 150% of season. Must be odd and larger than season.
low_pass - The length of the low-pass estimation window, usually the smallest odd number larger than the periodicity of the data.
So you could for example, take your trend to be the length of the entire dataset and that should smooth out some of the noise in the trend at the expense of losing high frequencies in the trend. Another approach, if you're certain that your trend is linear, would be to fit a linear regression model to your data, and subtract that out. With scikit-learn, this is super straightforward:
model = LinearRegression()
model.fit(X, y)
Of course, all of this should be done knowing that your residuals are stationary, etc. If you're new to this, I'd recommend something like kaggle's excellent free course for getting started: https://www.kaggle.com/learn/time-series hth.
If you are getting a noisy trend, it may be an indicator of your data being nonstationary. In that case, you cannot decompose your series using a deterministic function. There are approaches of testing nonstationarity, such as augmented Dickey-Fuller test, Phillips-Perron test, KPSS test, in addition to other approaches (to my knowledge, the first one is the most common one). You can, difference until your series becomes stationary (do not enforce it though, because it loses information in each differencing operation) and then reconsider the linear trend function.