In a recent post “Components of Time Series Data“, I talked about decomposing a time series into its component parts and in “Exponential Smoothing of Times Series Data in R“, I talked about simple exponential smoothing for time series analysis. The classical method of time series decomposition originated in the 1920s and was widely used until the 1950s. It still forms the basis of later time series methods, and so it is important to understand how it works. The first step in a classical decomposition is to use a moving average method to estimate the trend-cycle, so we begin by discussing moving averages.

A moving average takes a noisy time series and replaces each value with the average value of a neighborhood about the given value. This neighborhood may consist of purely historical data, or it may be centered about the given value. Furthermore, the values in the neighborhood may be weighted using different sets of weights.

## Example using R

For example, consider the volume of electricity sold to residential customers in South Australia each year from 1989 to 2008 (hot water sales have been excluded). The **elecsales** data are part of the**fpp**-package in R. Annual electricity sales for South Australia in GWh.

- library(fpp)
- data(elecsales)
- elecsales

**Figure 1.** Residential electricity sales with moving average overlaid

Notice how the trend (in red) is smoother than the original data and captures the major movement of the time series without all the minor fluctuations. The moving average method does not allow estimates where data is close to the ends of the series; hence, the red line does not extend to the edges of the graph on either side. Later we will use more sophisticated methods of trend-cycle estimation which do allow estimates near the endpoints.

The order of the moving average determines the smoothness of the trend-cycle estimate. In general, a larger order means a smoother curve. The following code and graphs show the effect of changing the order of the moving average for the residential electricity sales data. The plots of these are shown in Figure 2. The following R code produces the moving averages:

- elecsales.ma3<-ma(elecsales, order=4)
- elecsales.ma5<-ma(elecsales, order=6)
- elecsales.ma7<-ma(elecsales, order=8)
- elecsales.ma9<-ma(elecsales, order=10)

The nest R code snippet set the plotting window up to show four plots in two rows and two columns:

- old.par <- par(mfrow=c(2, 2))

The R code below plots the times series and the moving average on the same plot:

- plot(elecsales, main=”Residential electricity sales”,ylab=”GWh”, xlab=”Year”)
- lines(elecsales.ma3,col=”red”)
- legend(“bottomright”,”MA-3″, pch = 1)
- plot(elecsales, main=”Residential electricity sales”,ylab=”GWh”, xlab=”Year”)
- lines(elecsales.ma5,col=”red”)
- legend(“bottomright”,”MA-5″, pch = 1)
- plot(elecsales, main=”Residential electricity sales”,ylab=”GWh”, xlab=”Year”)
- lines(elecsales.ma7,col=”red”)
- legend(“bottomright”,”MA-7″, pch = 1)
- plot(elecsales, main=”Residential electricity sales”,ylab=”GWh”, xlab=”Year”)
- lines(elecsales.ma9,col=”red”)
- legend(“bottomright”,”MA-9″, pch = 1)

**Figure 2.** Residential electricity sales with moving averages overlaid for orders 3, 5, 7, and 9

Simple moving averages such as these are usually of odd order (e.g., 3, 5, 7, etc.) This is so they are symmetric: in a moving average of order m =2k + 1, there are k earlier observations, k later observations and the middle observation that are averaged. But if *m*were even, it would no longer be symmetric.

## Other Moving Averages

Other moving averages include but are not limited to:

- SMA Simple moving average.
- EMA Exponential moving average.
- WMA Weighted moving average.
- DEMA Double-exponential moving average.
- EVWMA Elastic, volume-weighted moving average.
- ZLEMA Zero lag exponential moving average.
- VWMA Volume-weighed moving average (same as VWAP).
- VWAP Volume-weighted average price (same as VWMA).
- VWA Variable-length moving average.

## Moving average technical details

A moving average of order *m* is the estimate of the trend-cycle at time *t* is obtained by averaging values of the time series within *k* periods of *t*. Observations that are nearby in time are also likely to be close in value, and the average eliminates some of the randomnesses in the data, leaving a smooth trend-cycle component. We call this an *m*-MA meaning a moving average of order *m*.

Here is an example of an equally weighted three-point moving average, using historical data,

Here, represents the smoothed signal, and represents the noisy time series.

In contrast, to simple moving averages, an exponentially weighted moving average (EWMA) adjusts a value according to an exponentially weighted sum of all previous values. This is nice because you don’t have to worry about having a three point window, versus a five point window, or worry about the appropriateness of your weighting scheme. With the EWMA, previous perturbations “remembered,” and “slowly forgotten,” by the term in the last equation, whereas, with a window or neighborhood with discrete boundaries, a perturbation is forgotten as soon as it passes out of the window.

**Authored by:
**

**Jeffrey Strickland, Ph.D.**Jeffrey Strickland, Ph.D., is the Author of * Predictive Analytics Using R* and a Senior Analytics Scientist with Clarity Solution Group. He has performed predictive modeling, simulation and analysis for the Department of Defense, NASA, the Missile Defense Agency, and the Financial and Insurance Industries for over 20 years. Jeff is a Certified Modeling and Simulation professional (CMSP) and an Associate Systems Engineering Professional (ASEP). He has published nearly 200 blogs on LinkedIn, is also a frequently invited guest speaker and the author of 20 books including:

__Operations Research using Open-Source Tools__*Discrete Event simulation using ExtendSim**Crime Analysis and Mapping**Missile Flight Simulation**Mathematical Modeling of Warfare and Combat Phenomenon**Predictive Modeling and Analytics**Using Math to Defeat the Enemy**Verification and Validation for Modeling and Simulation**Simulation Conceptual Modeling**System Engineering Process and Practices*

Connect with __Jeffrey Strickland
__Contact

__Jeffrey Strickland__

Categories: Articles, Education & Training, Featured, Jeffrey Strickland