Moving average of previous three values in R
Asked Answered
B

3

17

In the zoo package there is a function called rollmean, which enables you to make moving averages. The rollmean(x,3) will take the previous, current and next value (ie 4, 6 and 2) in the table below. This is shown in the second column.

x   rollmean    ma3
4       
6   4.0 
2   4.3 
5   3.0         4.0
2   6.3         4.3
12  6.0         3.0
4   6.0         6.3
2               6.0

I would like to get the same job done, but by averaging out the previous 3 values in the fourth row. This is displayed in the third column. Can anybody tell me the name of the function that will help to accomplish this?

Bray answered 24/4, 2013 at 13:29 Comment(1)
You can use filter(x,rep(1/3,3),sides=1)Marigraph
C
3

A simplier implementation of w_i_l_l's mavback function based on his mav function

mavback <- function(x,n){ filter(x, c(0, rep(1/n,n)), sides=1) }

Curmudgeon answered 18/1, 2018 at 13:15 Comment(1)
If you're using dplyr, make sure to make the call to filter explicit stats::filterMons
S
24

You can use rollmean, but set align='right'. Or you could use rollmeanr, which has align='right' as the default.

ma3 <- rollmeanr(x[,1],3,fill=NA)

...but you would still need to lag the result. Another solution is to use rollapply with a list for the width argument:

ma3 <- rollapplyr(x[,1],list(-(3:1)),mean,fill=NA)
Seaborne answered 24/4, 2013 at 13:33 Comment(2)
Could you explain what you mean by "...but you would still need to lag the result"? thanksRhabdomancy
They wanted the previous 3 values in the 4th row. Each observation in ma3 contains the mean of the current row and the previous 2 rows. So The 4th row of ma3 contains the mean of rows 2, 3, 4; not 1, 2, 3, like they wanted.Seaborne
P
12

I struggled searching for a simple function for moving averages that had some flexibility to do what I needed. I finally wrote a couple functions extending the one based on the filter function which rinni gives above in the comment (but which itself won't work because it will include the current observation in the 3 period average).

  1. Moving average function that includes the current observation

    mav <- function(x,n){filter(x,rep(1/n,n), sides=1)} 
    
  2. Moving average function that does not include the current observation

    mavback <- function(x,n){
      a<-mav(x,1)
      b<-mav(x,(n+1))
      c<-(1/n)*((n+1)*b - a)
      return(c)
    }
    
  3. Backward looking moving average function, not including current obs, based on [h2] readings starting [h1] periods back

    mavback1<-function(x,h1,h2){
      a<-mavback(x,h1)
      b<-mavback(x,h1-h2)
      c<-(1/h2)*(h1*a -(h1-h2)*b)
      return(c)
    }
    
Petronilapetronilla answered 24/8, 2016 at 2:25 Comment(0)
C
3

A simplier implementation of w_i_l_l's mavback function based on his mav function

mavback <- function(x,n){ filter(x, c(0, rep(1/n,n)), sides=1) }

Curmudgeon answered 18/1, 2018 at 13:15 Comment(1)
If you're using dplyr, make sure to make the call to filter explicit stats::filterMons

© 2022 - 2024 — McMap. All rights reserved.