rollapply(zoo)
rollapply()所属R语言包:zoo
Apply Rolling Functions
应用滚动功能
译者:生物统计家园网 机器人LoveR
描述----------Description----------
A generic function for applying a function to rolling margins of an array.
一个通用函数应用一个函数,数组的滚动利润的。
用法----------Usage----------
rollapply(data, ...)
## S3 method for class 'ts'
rollapply(data, ...)
## S3 method for class 'zoo'
rollapply(data, width, FUN, ..., by = 1, by.column = TRUE,
fill = if (na.pad) NA, na.pad = FALSE, partial = FALSE,
align = c("center", "left", "right"))
## Default S3 method:[默认方法]
rollapply(data, ...)
rollapplyr(..., align = "right")
参数----------Arguments----------
参数:data
the data to be used (representing a series of observations).
要使用的数据(代表一系列的观测)。
参数:width
numeric vector or list. In the simplest case this is an integer specifying the window width which is aligned to the original sample according to the align argument. Alternatively, width can be a list regarded as offsets compared to the current time, see below for details.
数值向量或列表。在最简单的情况下,这是对准所述原始样品align参数指定的窗口宽度的整数。另外,width可以视为偏移量相比,当前时间的列表,请参阅下面的详细信息。
参数:FUN
the function to be applied.
该函数被应用。
参数:...
optional arguments to FUN.
可选参数到FUN。
参数:by
calculate FUN at every by-th time point rather than every point. by is only used if width is length 1 and either a plain scalar or a list.
在每一个by个时间点,而不是每一个点的计算FUN。 by如果只使用width是长度为1和一个普通的标量或列表。
参数:by.column
logical. If TRUE, FUN is applied to each column separately.
逻辑。如果TRUE,FUN被应用于每一列分开。
参数:fill
a three-component vector or list (recycled otherwise) providing filling values at the left/within/to the right of the data range. See the fill argument of na.fill for details.
三组分的向量或列表(再循环否则)提供填充在左/ /右数据范围内的值。有关详细信息,请参阅fillna.fill参数。
参数:na.pad
deprecated. Use fill = NA instead of na.pad = TRUE.
过时了。使用fill = NA,而不是na.pad = TRUE。
参数:partial
logical or numeric. If FALSE (default) then FUN is only applied when all indexes of the rolling window are within the observed time range. If TRUE, then the subset of indexes that are in range are passed to FUN. A numeric argument to partial can be used to determin the minimal window size for partial computations. See below for more details.
逻辑或数字。如果FALSE(默认值),然后FUN时,仅适用于滚动窗口内观察到的时间范围内的所有索引。如果TRUE,然后子集范围内的索引传递给FUN。一个数值参数partial可用于对确定部分计算的最小窗口大小。请参阅下面的更多细节。
参数:align
specifyies whether the index of the result should be left- or right-aligned or centered (default) compared to the rolling window of observations. This argument is only used if width represents widths.
specifyies指数的结果是否应左对齐或右对齐或居中(默认值)相比,滚动窗口的观察。这种说法是仅用于width表示宽度。
Details
详细信息----------Details----------
If width is a plain numeric vector its elements are regarded as widths to be interpreted in conjunction with align whereas if width is a list its components are regarded as offsets. In the above cases if the length of width is 1 then width is recycled for every by-th point. If width is a list its components represent integer offsets such that the i-th component of the list refers to time points at positions i + width[[i]]. If any of these points are below 1 or above the length of index(data) then FUN is not evaluated for that point unless partial = TRUE and in that case only the valid points are passed.
width如果是一个普通的数字向量,它的元素被视为宽度来解释结合align而width是一个列表,它的组件被视为偏移。在上述情况下,如果的长度width是1,那么width被回收每by个点。如果width是一个列表,其组成部分表示整数的偏移量,这样的列表中的第i个分量,是指在时间点位置i + width[[i]]。如果这些问题都低于1或以上的长度index(data)然后FUN不评估这一点,除非partial = TRUE和在这种情况下,只有有效的点传递。
The rolling function can also be applied to partial windows by setting partial = TRUE For example, if width = 3, align = "right" then for the first point just that point is passed to FUN since the two points to its left are out of range. For the same example, if partial = FALSE then FUN is not invoked at all for the first two points. If partial is a numeric then it specifies the minimum number of offsets that must be within range. Negative partial is interpreted as FALSE.
的滚动功能也可以通过设置partial = TRUE例如,如果width = 3, align = "right"然后只是点传递给FUN的第一点,因为其左侧的两个点是施加到部分窗口超出范围。同样的例子,如果partial = FALSE然后FUN不会被调用的第一个两分。如果partial是一个数值,那么它必须在范围内的偏移量指定的最小数目。被解释为partial负FALSE。
If FUN is mean, max or median and by.column is TRUE and width is a plain scalar and there are no other arguments then special purpose code is used to enhance performance. Also in the case of mean such special purpose code is only invoked if the data argument has no NA values. See rollmean, rollmax and rollmedian for more details.
如果FUN是mean,max或median和by.column是TRUE和宽度是一个普通的标量,有没有其他参数,则特别代码的目的是用来提高性能。同样的情况下,mean如果data说法有没有NA值,该特殊目的的代码只调用。 rollmean,rollmax和rollmedian更多的细节。
Currently, there are methods for "zoo" and "ts" series and "default" method for ordinary vectors and matrices.
目前,有"zoo"和"ts"系列和"default"普通的向量和矩阵的方法的方法。
rollapplyr is a wrapper around rollapply that uses a default of align = "right".
rollapplyr是一个围绕rollapply使用默认的align = "right"。
值----------Value----------
A object of the same class as data with the results of the rolling function.
一个对象具有相同类的data的滚动功能的结果。
参见----------See Also----------
rollmean
rollmean
实例----------Examples----------
## rolling mean[#滚动的意思]
z <- zoo(11:15, as.Date(31:35))
rollapply(z, 2, mean)
## non-overlapping means[#非重叠装置]
z2 <- zoo(rnorm(6))
rollapply(z2, 3, mean, by = 3) # means of nonoverlapping groups of 3[不重叠的组3的装置]
aggregate(z2, c(3,3,3,6,6,6), mean) # same[同]
## optimized vs. customized versions[#优化与定制版本]
rollapply(z2, 3, mean) # uses rollmean which is optimized for mean[这是优化意味着使用rollmean]
rollmean(z2, 3) # same[同]
rollapply(z2, 3, (mean)) # does not use rollmean[不使用rollmean,]
## rolling regression:[#滚动回归:]
## set up multivariate zoo series with[#设置多元动物园系列]
## number of UK driver deaths and lags 1 and 12[英国的驱动程序死亡和滞后1和12#号]
seat <- as.zoo(log(UKDriverDeaths))
time(seat) <- as.yearmon(time(seat))
seat <- merge(y = seat, y1 = lag(seat, k = -1),
y12 = lag(seat, k = -12), all = FALSE)
## run a rolling regression with a 3-year time window[#运行一个滚动的回归3年的时间窗口]
## (similar to a SARIMA(1,0,0)(1,0,0)_12 fitted by OLS)[#(类似于一个SARIMA(1,0,0)(1,0,0)_12嵌合用OLS)]
rr <- rollapply(seat, width = 36,
FUN = function(z) coef(lm(y ~ y1 + y12, data = as.data.frame(z))),
by.column = FALSE, align = "right")
## plot the changes in coefficients[#图系数的变化]
## showing the shifts after the oil crisis in Oct 1973[1973年10月#石油危机后的变化]
## and after the seatbelt legislation change in Jan 1983[#后的1983年1月安全带立法的变化]
plot(rr)
## different values of rule argument[#不同的规则参数值]
z <- zoo(c(NA, NA, 2, 3, 4, 5, NA))
rollapply(z, 3, sum, na.rm = TRUE)
rollapply(z, 3, sum, na.rm = TRUE, fill = NULL)
rollapply(z, 3, sum, na.rm = TRUE, fill = NA)
rollapply(z, 3, sum, na.rm = TRUE, partial = TRUE)
# this will exclude time points 1 and 2[这将排除时间点1和2]
# It corresonds to align = "right", width = 3[,它corresonds对齐=“正确的”,宽度= 3]
rollapply(zoo(1:8), list(seq(-2, 0)), sum)
# but this will include points 1 and 2[但是,这将包括点1和2]
rollapply(zoo(1:8), list(seq(-2, 0)), sum, partial = 1)
rollapply(zoo(1:8), list(seq(-2, 0)), sum, partial = 0)
# so will this[所以将这种]
rollapply(zoo(1:8), list(seq(-2, 0)), sum, fill = NA)
# by = 3, align = "right"[= 3,=“右”]
L <- rep(list(NULL), 8)
L[seq(3, 8, 3)] <- list(seq(-2, 0))
str(L)
rollapply(zoo(1:8), L, sum)
rollapply(zoo(1:8), list(0:2), sum, fill = 1:3)
rollapply(zoo(1:8), list(0:2), sum, fill = 3)
L2 <- rep(list(-(2:0)), 10)
L2[5] <- list(NULL)
str(L2)
rollapply(zoo(1:10), L2, sum, fill = "extend")
rollapply(zoo(1:10), L2, sum, fill = list("extend", NULL))
rollapply(zoo(1:10), L2, sum, fill = list("extend", NA))
rollapply(zoo(1:10), L2, sum, fill = NA)
rollapply(zoo(1:10), L2, sum, fill = 1:3)
rollapply(zoo(1:10), L2, sum, partial = TRUE)
rollapply(zoo(1:10), L2, sum, partial = TRUE, fill = 99)
rollapply(zoo(1:10), list(-1), sum, partial = 0)
rollapply(zoo(1:10), list(-1), sum, partial = TRUE)
rollapply(zoo(cbind(a = 1:6, b = 11:16)), 3, rowSums, by.column = FALSE)
# these two are the same[这两个是一样的]
rollapply(zoo(cbind(a = 1:6, b = 11:16)), 3, sum)
rollapply(zoo(cbind(a = 1:6, b = 11:16)), 3, colSums, by.column = FALSE)
# these two are the same[这两个是一样的]
rollapply(zoo(1:6), 2, sum, by = 2, align = "right")
aggregate(zoo(1:6), c(2, 2, 4, 4, 6, 6), sum)
# these two are the same[这两个是一样的]
rollapply(zoo(1:3), list(-1), c)
lag(zoo(1:3), -1)
# these two are the same[这两个是一样的]
rollapply(zoo(1:3), list(1), c)
lag(zoo(1:3))
# these two are the same[这两个是一样的]
rollapply(zoo(1:5), list(c(-1, 0, 1)), sum)
rollapply(zoo(1:5), 3, sum)
# these two are the same[这两个是一样的]
rollapply(zoo(1:5), list(0:2), sum)
rollapply(zoo(1:5), 3, sum, align = "left")
# these two are the same[这两个是一样的]
rollapply(zoo(1:5), list(-(2:0)), sum)
rollapply(zoo(1:5), 3, sum, align = "right")
# these two are the same[这两个是一样的]
rollapply(zoo(1:6), list(NULL, NULL, -(2:0)), sum)
rollapply(zoo(1:6), 3, sum, by = 3, align = "right")
# these two are the same[这两个是一样的]
rollapply(zoo(1:5), list(c(-1, 1)), sum)
rollapply(zoo(1:5), 3, function(x) sum(x[-2]))
# these two are the same[这两个是一样的]
rollapply(1:5, 3, rev)
embed(1:5, 3)
# these four are the same[这是相同的]
x <- 1:6
rollapply(c(0, 0, x), 3, sum, align = "right") - x
rollapply(x, 3, sum, partial = TRUE, align = "right") - x
rollapply(x, 3, function(x) sum(x[-3]), partial = TRUE, align = "right")
rollapply(x, list(-(2:1)), sum, partial = 0)
# same as Matlab's buffer(x, n, p) for valid non-negative p[作为Matlab的缓冲液相同的有效的非负P(X,N,p)的]
# See http://www.mathworks.com/help/toolbox/signal/buffer.html[见http://www.mathworks.com/help/toolbox/signal/buffer.html]
x <- 1:30; n <- 7; p <- 3
t(rollapply(c(rep(0, p), x, rep(0, n-p)), n, by = n-p, c))
# these three are the same[这三个是相同的]
y <- 10 * seq(8); k <- 4; d <- 2
# 1[1]
# from http://ucfagls.wordpress.com/2011/06/14/embedding-a-time-series-with-time-delay-in-r-part-ii/[从http://ucfagls.wordpress.com/2011/06/14/embedding-a-time-series-with-time-delay-in-r-part-ii/]
Embed <- function(x, m, d = 1, indices = FALSE, as.embed = TRUE) {
n <- length(x) - (m-1)*d
X <- seq_along(x)
if(n <= 0)
stop("Insufficient observations for the requested embedding")
out <- matrix(rep(X[seq_len(n)], m), ncol = m)
out[,-1] <- out[,-1, drop = FALSE] +
rep(seq_len(m - 1) * d, each = nrow(out))
if(as.embed)
out <- out[, rev(seq_len(ncol(out)))]
if(!indices)
out <- matrix(x[out], ncol = m)
out
}
Embed(y, k, d)
# 2[2]
rollapply(y, list(-d * seq(0, k-1)), c)
# 3[3]
rollapply(y, d*k-1, function(x) x[d * seq(k-1, 0) + 1])
转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。
注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
|