【问题标题】:Moving average with varying window size [duplicate]具有不同窗口大小的移动平均值[重复]
【发布时间】:2018-03-29 08:07:11
【问题描述】:

我正在使用zoo::rollmean 来计算移动平均线。但是,平均每个值的窗口的大小是恒定的k(我想在rollmean 实现中出于性能原因)。

是否存在可以接受动态窗口的移动平均线的 R 实现?

toSmoothed   = c(1,2,3,2,1,2,3,2)
dynamicRange = c(1,2,1,2,1,2,1,2)
foo(toSmoothed, dynamicRange, fill = NA, align = "left") # please notice the aligned left
# return
# c(1,2.5,3,1.5,1,2.5,3,NA)

【问题讨论】:

    标签: r smoothing


    【解决方案1】:

    zoo::rollapply 在这里很有用。试试:

    zoo::rollapply(toSmoothed, dynamicRange, FUN = mean, fill = NA, align = "left")
    [1] 1.0 2.5 3.0 1.5 1.0 2.5 3.0  NA
    

    【讨论】:

      【解决方案2】:

      不确定包裹中是否有任何东西,但你可以做这样的事情......

      foo <- function(toSmoothed, dynamicRange){
        x <- c(0, cumsum(toSmoothed))
        lower <- 1:length(toSmoothed)
        upper <- lower+dynamicRange
        x <- (x[upper]-x[lower])/(upper-lower)
        return(x)
      }
      
      foo(toSmoothed, dynamicRange)
      [1] 1.0 2.5 3.0 1.5 1.0 2.5 3.0  NA
      

      【讨论】:

        猜你喜欢
        • 2020-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-09
        • 2020-07-12
        • 1970-01-01
        • 2020-03-29
        • 1970-01-01
        相关资源
        最近更新 更多