Description Usage Arguments Details Value Author(s) Examples
Calculates a moving average of a vector using 'filter'
1 | movingAverage(x, n=11, ...)
|
x |
the vector for which to calculate the moving average |
n |
the width of the moving average window, in indices. For a
true symmetric moving average, |
... |
other options for filter |
Performs a symmetric moving average of the vector x
,
using a linear filter of the form rep(1/n, n)
, with the
standard filter()
function from the stats
package. Note that the default options apply (but can be
changed with the ...
argument), so that sides=2
and method='convolution'
.
Currently doesn't handle missing values, so that any values
missing in x
will be missing from the result. In
addition, the length of the vector is reduced by n
,
owing to the truncation at the beginning and end of the
series.
A vector the same length as x
, but with NAs at the
beginning and end of the series (which sides=2
) so that
the total number of values is length(x) - (n-1)
.
Clark Richards
1 2 3 4 5 | x <- 1:300
y <- sin(x/20) + rnorm(300,sd=.1)
plot(x,y)
lines(x, movingAverage(y, 21), col=2) ## symmetric
lines(x, movingAverage(y, 21, sides=1), col=3) ## one-sided
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.