The method of calculating moving average in R language

Source: Internet
Author: User

moving average can make time series smooth, it is a typical ordered computation problem, its basic algorithm is: Will N consecutive time series members as a collection, computes the average of the collection, and goes through the collection one after the other. Here is an example of how R calculates the moving average.

Case Description:

The data frame sales has two fields: Date and day sales, and the 3rd moving average is calculated. The specific algorithm is: To find out the previous day, the day, after the sales average, and daily. Some of the source data are as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4C/50/wKiom1Q7TD2yq4zkAAB6yHVaESg524.jpg "style=" float: none; "title=" r_moving_average_1.jpg "alt=" Wkiom1q7td2yq4zkaab6yhvaesg524.jpg "/>

Code:

Filter (SALES$AMOUNT/3, Rep (1, 3))

Calculation Result:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/4C/51/wKioL1Q7THWyQ_rIAADKHNC-eOA961.jpg "style=" float: none; "title=" r_moving_average_2.jpg "alt=" Wkiol1q7thwyq_riaadkhnc-eoa961.jpg "/>

Code interpretation:

The R language can use the function filter to calculate the moving average, which is very convenient.

     function filter Although very convenient, but beginners are not easy to understand. For example sales$amount/ 3 Amount The current value in the field is divided by 3 filter Rep (1,3) [1,1,1] r

in fact, the function filter is a general-purpose linear filter that does more than simply calculate the moving average. Its complete function is described as follows:filter (x, filter, method= C ("convolution", "recursive"), sides = 2,circular = FALSE, init).

If you want to change the algorithm, the code will be more difficult to understand, such as to calculate the day, the previous day, the first two days of the three-day moving average, can not be written as: Filter (Sales$amount/3,rep (0,2)), instead of filter (Sales$amount/3,rep (1,3), sides = 1) .


Summary: R languages can calculate moving averages, but the code is difficult to understand.

Third-party Solutions

This case can also be used Python, the collector,Perl and other languages to implement. As with the R language, these languages can be used for statistical analysis of data, can calculate the moving average, the following is a brief introduction to Python and the solution of the collector.

Python (Pandas)

Pandas is a third-party library function of Python, whose basic data type is modeled after the data frame in R, and has strong structural data processing capability, the latest version is 0.14. The code is as follows:

Pandas.stats.moments.rolling_mean (sales["Amount"],3)

Rolling_mean This function name at a glance, even if the developers just contact Pandas can easily find this function,rolling_mean usage is very concise, the first parameter is the computed sequence, the second parameter is n in the moving average of the nth day .

the collector

The Collector specializes in the free expression of business logic with agile syntax, and its relative position expression can easily solve the problems in ordered computation. The code shows:

Sales. (Amount{-1,1}.avg ())

in the Code { -1,1} represents a relative interval, that is, three days between the previous and the following day. As you can see, moving averages are intuitively calculated using relative intervals and are more flexible. For example, the same change algorithm: Calculate the day, the previous day, the first two days of the three days of the moving average, the collector only need to change the interval to { -2,0}.

The relative interval is a set, and the collector can also express the relative position of the elements, such as sales growth can be used sales (amount-amount[-1]) to intuitively calculate, and the R language and Python are not so well understood.


The method of calculating moving average in R language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.