In this post, we are attempting to replicate STL seasonal component calculation. We will use S&P 500 close prices for demonstration.
The first step would be to get the closing prices. As we are looking specifically at annual season, we would require a number of years to average the values for a particular day of the month. We arbitrary chose 7 years:
Because of weekends and public holidays and we are using daily data, we need to fill these gaps as NA values. But in the following code, we would use linear interpolation to fill them up with values:
STL stands for “Seasonal and Trend decomposition using Loess”. It decompose a time series data into a trend, seasonal, and remainder components. We will invoke STL to extract the seasonal component and in later sections replicate the result.
We will iterate in a loop for about 365 days, and average the values for the same day or the month across the 7 years in our data. We keep track of a year when the day and month is the same for the next year. This will inform us that we have finished a year worth of data. We will store the data in a tibble.
Once this is done, we need to detrend the averages and extract the residues. The easiest way is to fit a linear regression, followed by calling the resid() function:
Finally we plot the data and contrast it with what we get from STL:
We can see some differences, but the general up/down direction is the same. The tiny differences could be due to STL using a weighted average (giving more weights to years close to the current year).