我试着用R来计算矩阵中一系列值的移动平均值。R中似乎没有一个内置函数可以让我计算移动平均线。有任何软件包提供这样的服务吗?还是需要我自己写?
当前回答
在数据。表1.12.0增加了新的滚动平均值函数,以计算快速和准确的滚动平均值,仔细处理NA, NaN和+Inf, -Inf值。
由于在这个问题中没有可重复的例子,所以在这里没有更多的问题要解决。
你可以在手册中找到更多关于?frollmean的信息,也可以在?frollmean网站上找到。
下面是手册中的例子:
library(data.table)
d = as.data.table(list(1:6/2, 3:8/4))
# rollmean of single vector and single window
frollmean(d[, V1], 3)
# multiple columns at once
frollmean(d, 3)
# multiple windows at once
frollmean(d[, .(V1)], c(3, 4))
# multiple columns and multiple windows at once
frollmean(d, c(3, 4))
## three above are embarrassingly parallel using openmp
其他回答
动物园包中的滚动平均值/最大值/中位数(rollmean) TTR中的移动平均线 马云在预测
事实上,RcppRoll非常好。
cantdutchthis发布的代码必须在窗口的第四行进行修正:
ma <- function(arr, n=15){
res = arr
for(i in n:length(arr)){
res[i] = mean(arr[(i-n+1):i])
}
res
}
这里给出了另一种处理缺失的方法。
第三种方法,改进cantdutch这段代码来计算部分平均与否,如下:
ma <- function(x, n=2,parcial=TRUE){
res = x #set the first values
if (parcial==TRUE){
for(i in 1:length(x)){
t<-max(i-n+1,1)
res[i] = mean(x[t:i])
}
res
}else{
for(i in 1:length(x)){
t<-max(i-n+1,1)
res[i] = mean(x[t:i])
}
res[-c(seq(1,n-1,1))] #remove the n-1 first,i.e., res[c(-3,-4,...)]
}
}
为了配合坎迪奇西斯和罗德里戈·雷麦黛奥的回答;
moving_fun <- function(x, w, FUN, ...) {
# x: a double vector
# w: the length of the window, i.e., the section of the vector selected to apply FUN
# FUN: a function that takes a vector and return a summarize value, e.g., mean, sum, etc.
# Given a double type vector apply a FUN over a moving window from left to the right,
# when a window boundary is not a legal section, i.e. lower_bound and i (upper bound)
# are not contained in the length of the vector, return a NA_real_
if (w < 1) {
stop("The length of the window 'w' must be greater than 0")
}
output <- x
for (i in 1:length(x)) {
# plus 1 because the index is inclusive with the upper_bound 'i'
lower_bound <- i - w + 1
if (lower_bound < 1) {
output[i] <- NA_real_
} else {
output[i] <- FUN(x[lower_bound:i, ...])
}
}
output
}
# example
v <- seq(1:10)
# compute a MA(2)
moving_fun(v, 2, mean)
# compute moving sum of two periods
moving_fun(v, 2, sum)
虽然有点慢,但你也可以使用zoo::rollapply在矩阵上执行计算。
reqd_ma <- rollapply(x, FUN = mean, width = n)
其中x为数据集,FUN = mean为函数;你也可以改变它为min, max, sd等,宽度是滚动窗口。
或者你可以简单地计算它使用过滤器,这是我使用的函数:
ma <- function(x, n = 5){filter(x, rep(1 / n, n), sides = 2)}
如果使用dplyr,请注意在上面的函数中指定stats::filter。
推荐文章
- 如何从多个向量中找到公共元素?
- 将数据帧行按向量按特定顺序排序
- 在FUN中访问lapply索引名
- 三元算符在R中存在吗?
- R MarkDown和R NoteBook的区别
- 使用该集合的原因。种子功能
- 加快R中的循环操作
- 错误:无法找到函数…在R
- 在编写自己的函数时,如何使用R的省略号特性?
- 当我的ggplot2语法合理时,我如何处理R CMD检查“全局变量无可见绑定”注释?
- 如何将表转换为数据帧
- 转换data.frame列到向量?
- 如何将颜色分配给ggplot2中具有稳定映射的类别变量?
- 在基础图形的绘图区域之外绘制一个图例?
- 如何计算滚动/移动平均使用python + NumPy / SciPy?