A moving average is a series of averages that is taken along a data series. It is a statistical tool used to show trends. The averages are taken over smaller segments that are then used to create a new series containing the averages.

### How To Calculate A Moving Average in R

When you are doing a rolling average on time series data You have a choice of three functions that can do the job. The filter function when it is used in the format of filter(x, rep(1 / k, k), sides), the rollmean function with the format of rollmean(x, k) and the rollmedian function which has the format of rollmedian(x, k ). In each case, x is the vector being evaluated and k is the size of the segments being evaluated. The rollmean and rollmedian functions are part of the zoo package, but the filter function is part of the system library.

### Why Calculating a Moving Average is Useful

When creating a moving average, it can be a trailing moving average or exponential moving average, but it can also be a simple moving average. The process works by taking a data segment, of a given length, in a series and then take the average of the segment. This process is then repeated from the beginning of the series to the end. It produces a new series consisting of those averages. You can use similar processes to produce a moving maximum and moving sum. It has a smoothing effect on the data series that you are evaluating, revealing aspects of the series that would not be noticeable otherwise.

### Examples of Calculating A Moving Average in R

Here are three examples, showing each of these functions in action.

> set.seed(1964)

> x = 1:50 + rnorm(10, 0, 10)

> filter(x, rep(1 / 5, 5), sides = 2)

Time Series:

Start = 1

End = 50

Frequency = 1

[1] NA NA 4.544060 1.623450 8.221260 7.754368 9.487837 8.118620 13.039230 8.441420 10.908311

[12] 11.174843 14.544060 11.623450 18.221260 17.754368 19.487837 18.118620 23.039230 18.441420 20.908311 21.174843

[23] 24.544060 21.623450 28.221260 27.754368 29.487837 28.118620 33.039230 28.441420 30.908311 31.174843 34.544060

[34] 31.623450 38.221260 37.754368 39.487837 38.118620 43.039230 38.441420 40.908311 41.174843 44.544060 41.623450

[45] 48.221260 47.754368 49.487837 48.118620 NA NA

In this example, we use the filter function for the purpose of creating a moving average. This is not the main use for this function, but we have configured it to do the job.

> library(“zoo”)

> set.seed(1964)

> x = 1:50 + rnorm(10, 0, 10)

> rollmean(x, k = 5)

[1] 4.544060 1.623450 8.221260 7.754368 9.487837 8.118620 13.039230 8.441420 10.908311 11.174843 14.544060

[12] 11.623450 18.221260 17.754368 19.487837 18.118620 23.039230 18.441420 20.908311 21.174843 24.544060 21.623450

[23] 28.221260 27.754368 29.487837 28.118620 33.039230 28.441420 30.908311 31.174843 34.544060 31.623450 38.221260

[34] 37.754368 39.487837 38.118620 43.039230 38.441420 40.908311 41.174843 44.544060 41.623450 48.221260 47.754368

[45] 49.487837 48.118620

In this example, we are using the rollmean function from the zoo package. It works by producing the mean of each segment as it is evaluated.

> library(“zoo”)

> set.seed(1964)

> x = 1:50 + rnorm(10, 0, 10)

> rollmedian(x, k = 5)

[1] 1.1473384 0.6071651 1.1473384 0.6071651 3.0201529 3.0201529 14.0161549 3.0201529 11.1473384 11.1473384

[11] 11.1473384 10.6071651 11.1473384 10.6071651 13.0201529 13.0201529 24.0161549 13.0201529 21.1473384 21.1473384

[21] 21.1473384 20.6071651 21.1473384 20.6071651 23.0201529 23.0201529 34.0161549 23.0201529 31.1473384 31.1473384

[31] 31.1473384 30.6071651 31.1473384 30.6071651 33.0201529 33.0201529 44.0161549 33.0201529 41.1473384 41.1473384

[41] 41.1473384 40.6071651 41.1473384 40.6071651 43.0201529 43.0201529

In this example we are using the rollmedian function, to produce a median value for each of the segments that are being evaluated.

Regardless of the approach, the process produces a running average of the vector being evaluated. It is a process that is easy to set up and We have three different methods for producing this running average. The one that you use depends upon the specific situation you are doing the evaluation in.

### Applications of Moving Averages in R

The applications of doing a moving average include smoothing out data, which has the advantage of making graphs look better. It is useful for trend determination to help create models to forecast future observations. As a result, it is useful in data analysis, model creation, and testing. These applications are important parts of data analysis making them incredibly useful tools in data science. While there are other tools that aid in this process, moving averages are extremely easy to understand, use and adjust for unique needs. Some of the other modeling tools require more effort to understand well enough to use. Moving averages on the other hand are based on easy-to-understand statistical principles that you already understand.

Moving averages is an incredibly useful modeling tool for data science. the fact that you have three useful functions for doing this job makes it even more useful. The one that you use is likely to vary from situation to situation. It will help you in testing and creating models for analysis.