Description Usage Arguments Details Value Author(s) See Also Examples
Applies a function over subsets of the vector(s) formed by taking a fixed number of previous points.
1 2 3 |
X |
data vector |
Y |
data vector (optional) |
fun |
Function to apply. Default is |
width |
Integer giving the number of vector elements to include in the subsets. Defaults to the lesser of the length of the data and 20 elements. |
allow.fewer |
Boolean indicating whether the function should be
computed for subsets with fewer than |
pad |
Boolean indicating whether the returned results should
be 'padded' with NAs corresponding to sets with less than
|
align |
One of "right", "center", or "left".
This controls the relative location of ‘short’ subsets with less
then |
simplify |
Boolean. If FALSE the returned object will be a list containing one element per evaluation. If TRUE, the returned object will be coerced into a vector (if the computation returns a scalar) or a matrix (if the computation returns multiple values). Defaults to FALSE. |
by |
Integer separation between groups. If |
... |
parameters to be passed to |
running
applies the specified function to
a sequential windows on X
and (optionally) Y
. If
Y
is specified the function must be bivariate.
List (if simplify==TRUE
), vector, or matrix containg the
results of applying the function fun
to the
subsets of X
(running
) or X
and Y
.
Note that this function will create a vector or matrix even for
objects which are not simplified by sapply
.
Gregory R. Warnes greg@warnes.net, with contributions by Nitin Jain nitin.jain@pfizer.com.
wapply
to apply a function over an x-y window
centered at each x point, sapply
,
lapply
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | # show effect of pad
running(1:20, width=5)
running(1:20, width=5, pad=TRUE)
# show effect of align
running(1:20, width=5, align="left", pad=TRUE)
running(1:20, width=5, align="center", pad=TRUE)
running(1:20, width=5, align="right", pad=TRUE)
# show effect of simplify
running(1:20, width=5, fun=function(x) x ) # matrix
running(1:20, width=5, fun=function(x) x, simplify=FALSE) # list
# show effect of by
running(1:20, width=5) # normal
running(1:20, width=5, by=5) # non-overlapping
running(1:20, width=5, by=2) # starting every 2nd
# Use 'pad' to ensure correct length of vector, also show the effect
# of allow.fewer.
par(mfrow=c(2,1))
plot(1:20, running(1:20, width=5, allow.fewer=FALSE, pad=TRUE), type="b")
plot(1:20, running(1:20, width=5, allow.fewer=TRUE, pad=TRUE), type="b")
par(mfrow=c(1,1))
# plot running mean and central 2 standard deviation range
# estimated by *last* 40 observations
dat <- rnorm(500, sd=1 + (1:500)/500 )
plot(dat)
sdfun <- function(x,sign=1) mean(x) + sign * sqrt(var(x))
lines(running(dat, width=51, pad=TRUE, fun=mean), col="blue")
lines(running(dat, width=51, pad=TRUE, fun=sdfun, sign=-1), col="red")
lines(running(dat, width=51, pad=TRUE, fun=sdfun, sign= 1), col="red")
# plot running correlation estimated by last 40 observations (red)
# against the true local correlation (blue)
sd.Y <- seq(0,1,length=500)
X <- rnorm(500, sd=1)
Y <- rnorm(500, sd=sd.Y)
plot(running(X,X+Y,width=20,fun=cor,pad=TRUE),col="red",type="s")
r <- 1 / sqrt(1 + sd.Y^2) # true cor of (X,X+Y)
lines(r,type="l",col="blue")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.