简体   繁体   English

双边移动平均线?

[英]two-sided moving average?

how to get two sided "moving average" that is a function that averages n numbers from right and left of a vector and gives them weights according to their distance from center value ? 如何获得双向“移动平均线”,这是一个从矢量的左右平均n个数字的函数,并根据它们与中心值的距离给出它们的权重?

I tried to use TTR but its moving averages works only from left to right and set leftmost values as NA. 我尝试使用TTR,但其移动平均线仅从左到右工作,并将最左边的值设置为NA。 So I cannot use that smoothed vector as a input to smooth.spline 所以我不能使用平滑的矢量作为smooth.spline的输入

In the zoo package rollmean and rollapply have arguments that allow numerous variations. 在动物园包中, rollmeanrollapply具有允许多种变化的参数。

library(zoo)
x <- seq(10)^2

# no NAs at end
rollmean(x, 3)

# NAs at ends
rollmean(x, 3, na.pad = TRUE)

# weighted mean
rollapply(zoo(x), 3, function(x) c(1, 2, 1) %*% x / 4) 

# at ends take means of less than 3 points - needs devel version
# partial= is in development and at this point must use na.rm = TRUE to use partial
source("http://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/zoo/R/rollapply.R?revision=802&root=zoo")
rollapply(zoo(x), 3, mean, partial = TRUE, na.rm = TRUE)

EDIT: 编辑:

Note that since this was written the development version of zoo was changed so that instead of writing partial = TRUE one writes rule = "partial" or rule = 3 . 请注意,由于这是写的,动态园的开发版本已更改,因此不是写partial = TRUE而是写rule =“partial”或rule = 3 The problem was that as new end rules were added to the development version (there are now 3 and a 4th will be added before its released) having a separate argument for each one clutters the user interface. 问题在于,随着新的结束规则被添加到开发版本(现在有3个和第4个将在其发布之前添加),每个规则都有一个单独的参数,使用户界面变得混乱。 Also rule is more consistent with approx in the core of R. In fact, rule=1 and rule=2 will have the same meaning in rollapply and in approx (from the core of R) for better consistency and ease of use. rule是与更一致的approx在R的核心事实上, rule=1rule=2将具有相同的含义rollapply和在approx (来自R的芯),用于更好的一致性和易用性。 The parentheses around mean in the example below are currently required in the development version to prevent it from calling rollmean , where rule="partial" has not yet been implemented, but the need to do that will be eliminated by the time its officially released. 周围的括号mean在下面的例子中,目前需要开发版本,以防止它调用rollmean ,其中rule="partial"尚未实现,但要做到这一点,需要将被淘汰的时候它的正式发布。

source("http://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/zoo/R/rollapply.R?revision=815&root=zoo")
rollapply(zoo(x), 3, (mean), rule = "partial")

Look at the filter() function, and particularly the sides argument: 查看filter()函数,特别是sides参数:

 filter package:stats R Documentation Linear Filtering on a Time Series Description: Applies linear filtering to a univariate time series or to each series separately of a multivariate time series. Usage: filter(x, filter, method = c("convolution", "recursive"), sides = 2, circular = FALSE, init) Arguments: [...] sides: for convolution filters only. If 'sides=1' the filter coefficients are for past values only; if 'sides=2' they are centred around lag 0. In this case the length of the filter should be odd, but if it is even, more of the filter is forward in time than backward. 

你可以和kernapply一起尝试kernel (在第一页的末尾有一些例子)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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