Time Series Smoothing in R

Kyle Lee Dixon
Towards Dev
Published in
3 min readMar 14, 2022

--

Photo by Maxim Hopman

In time series analysis smoothing is usually done to help us better see patterns and trends. The idea is to smooth out the irregular roughness to see a clearer signal. For instance, with seasonal data we might smooth out the seasonality so that we can identify the trend or frequency of the seasonality. Smoothing doesn’t provide us with a model however, but it can be a good first step in describing the components of the series. In the following examples I am going to take historical global warming data and illustrate three different smoothing techniques in R.

Moving Average Smoothing

The moving average is one of the oldest processes for smoothing data and it continues to be useful today. It is very simple to calculate, as it is just an average of data points over a time period.

In the plot below, we are going to take yearly data points and average them over a period of 7. Meaning, every 7 years the data is averaged together to produce a new data point. It is a “moving” average because as we add a new year into the 7 period average, we drop an old data point out of the average to produce our second data point and so on.

This technique works well, but has shortcomings at the beginning and end of the time series. For instance, you cannot calculate a moving average until you have enough data points to satisfy your time period. So on all moving average plots, you will notice a gap at the beginning and end of the plot because you no longer have enough data points to satisfy the time period. The larger the time period, the larger the gap.

Kernel Smoothing

In 1954, Nadaraya and Watson proposed an estimate, m, as a locally weighted average using a kernel as a weighting function.

Similarly to kernel density estimation, in the Nadaraya–Watson estimator the bandwidth has a substantial effect on the shape of the estimator, and the kernel is less important. The bandwidth, h, determines the degree of smoothing. A large h increases the width of the bins, increasing the smoothness. A small h decreases the width of the bins, producing a less smooth result.

Lowess Smoothing

LOWESS, or LOcally WEighted Scatterplot Smoothing, uses locally weighted linear regression to smooth data. The process is weighted because it defines a regression weight function for the data points contained within the span.

At each point in the range of the data set a low-degree polynomial is fitted to a subset of the data. The polynomial is fitted using weighted least squares, giving more weight to points near the point whose response is being estimated and less weight to points further away.

Conclusion

In each of the plots you can see the clear difference between these three techniques. With this particular data and bandwidth settings, the lowess smoothed signal has the least variance where the 7 period moving average had the highest variance.

The code used to create these plots can be found below:

Kyle Dixon

Data Scientist, Quant, Entrepreneur

https://linktr.ee/kyleleedixon

--

--