Calculating moving average

前端 未结 16 1793
夕颜
夕颜 2020-11-21 23:55

I\'m trying to use R to calculate the moving average over a series of values in a matrix. The normal R mailing list search hasn\'t been very helpful though. There doesn\'t s

相关标签:
16条回答
  • 2020-11-22 00:37

    The slider package can be used for this. It has an interface that has been specifically designed to feel similar to purrr. It accepts any arbitrary function, and can return any type of output. Data frames are even iterated over row wise. The pkgdown site is here.

    library(slider)
    
    x <- 1:3
    
    # Mean of the current value + 1 value before it
    # returned as a double vector
    slide_dbl(x, ~mean(.x, na.rm = TRUE), .before = 1)
    #> [1] 1.0 1.5 2.5
    
    
    df <- data.frame(x = x, y = x)
    
    # Slide row wise over data frames
    slide(df, ~.x, .before = 1)
    #> [[1]]
    #>   x y
    #> 1 1 1
    #> 
    #> [[2]]
    #>   x y
    #> 1 1 1
    #> 2 2 2
    #> 
    #> [[3]]
    #>   x y
    #> 1 2 2
    #> 2 3 3
    

    The overhead of both slider and data.table's frollapply() should be pretty low (much faster than zoo). frollapply() looks to be a little faster for this simple example here, but note that it only takes numeric input, and the output must be a scalar numeric value. slider functions are completely generic, and you can return any data type.

    library(slider)
    library(zoo)
    library(data.table)
    
    x <- 1:50000 + 0L
    
    bench::mark(
      slider = slide_int(x, function(x) 1L, .before = 5, .complete = TRUE),
      zoo = rollapplyr(x, FUN = function(x) 1L, width = 6, fill = NA),
      datatable = frollapply(x, n = 6, FUN = function(x) 1L),
      iterations = 200
    )
    #> # A tibble: 3 x 6
    #>   expression      min   median `itr/sec` mem_alloc `gc/sec`
    #>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
    #> 1 slider      19.82ms   26.4ms     38.4    829.8KB     19.0
    #> 2 zoo        177.92ms  211.1ms      4.71    17.9MB     24.8
    #> 3 datatable    7.78ms   10.9ms     87.9    807.1KB     38.7
    
    0 讨论(0)
  • 2020-11-22 00:38

    Here is example code showing how to compute a centered moving average and a trailing moving average using the rollmean function from the zoo package.

    library(tidyverse)
    library(zoo)
    
    some_data = tibble(day = 1:10)
    # cma = centered moving average
    # tma = trailing moving average
    some_data = some_data %>%
        mutate(cma = rollmean(day, k = 3, fill = NA)) %>%
        mutate(tma = rollmean(day, k = 3, fill = NA, align = "right"))
    some_data
    #> # A tibble: 10 x 3
    #>      day   cma   tma
    #>    <int> <dbl> <dbl>
    #>  1     1    NA    NA
    #>  2     2     2    NA
    #>  3     3     3     2
    #>  4     4     4     3
    #>  5     5     5     4
    #>  6     6     6     5
    #>  7     7     7     6
    #>  8     8     8     7
    #>  9     9     9     8
    #> 10    10    NA     9
    
    0 讨论(0)
  • 2020-11-22 00:38
    vector_avg <- function(x){
      sum_x = 0
      for(i in 1:length(x)){
        if(!is.na(x[i]))
          sum_x = sum_x + x[i]
      }
      return(sum_x/length(x))
    }
    
    0 讨论(0)
  • 2020-11-22 00:40

    Here is a simple function with filter demonstrating one way to take care of beginning and ending NAs with padding, and computing a weighted average (supported by filter) using custom weights:

    wma <- function(x) { 
      wts <- c(seq(0.5, 4, 0.5), seq(3.5, 0.5, -0.5))
      nside <- (length(wts)-1)/2
      # pad x with begin and end values for filter to avoid NAs
      xp <- c(rep(first(x), nside), x, rep(last(x), nside)) 
      z <- stats::filter(xp, wts/sum(wts), sides = 2) %>% as.vector 
      z[(nside+1):(nside+length(x))]
    }
    
    0 讨论(0)
  • 2020-11-22 00:42
    • Rolling Means/Maximums/Medians in the zoo package (rollmean)
    • MovingAverages in TTR
    • ma in forecast
    0 讨论(0)
  • 2020-11-22 00:42

    You may calculate the moving average of a vector x with a window width of k by:

    apply(embed(x, k), 1, mean)
    
    0 讨论(0)
提交回复
热议问题