简体   繁体   中英

Calculating moving average in R

I've been trying to calculate the moving average over a vector in R. Ideally, I would like to write a function that calculates the MA over any number of lags. The mathematical steps i have to code are: 1. creating a dataset with 1:k lags for each column. 2. Summing over the row 3. dividing by 2k+1

I'm stuck at step 2 right now.I can evaluate the sum for say 2 lags, but creating a formula for multiple lags is very cumbersome.

Here is where i am right now.

I have leveraged a code for calculating lags

lagpad <- function(x, k) {
  if (k>0) {
    return (c(rep(NA, k), x)[1 : length(x)] );
  }
  else {
    return (c(x[(-k+1) : length(x)], rep(NA, -k)));
  }
}

Suppose I calculate the MA over the following vector:

i<-1:50
rowSums(cbind(lagpad(i,1)[1:length(i)], lagpad(i,2)[1:length(i)]))

This works fine for 2. But if i want to calculate the MA for 20 lags, I'll have to cbind 20 columns. Is there a faster way to do this/ write this function?

Thanks

Here is a simple function using differences of cumulative sums...

x <- 1:10

mav <- function(x, k){
  y <- c(0, cumsum(x))    #cumulative sums of x
  z <- y[-c(1:k)]         #discard first k values
  length(y) <- length(z)  #discard last k values of y
  return((z - y) / k)
}

mav(x,2)
[1] 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5

mav(x,1)
[1]  1  2  3  4  5  6  7  8  9 10

mav(x,4)
[1] 2.5 3.5 4.5 5.5 6.5 7.5 8.5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM