movingAverage: Calculates a moving average of a vector using 'filter'

Description Usage Arguments Details Value Author(s) Examples

Description

Calculates a moving average of a vector using 'filter'

Usage

1
movingAverage(x, n=11, ...)

Arguments

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, n should be odd, representing an average of the current value, and the (n-1)/2 values before and after.

...

other options for filter

Details

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.

Value

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).

Author(s)

Clark Richards

Examples

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

richardsc/crMisc documentation built on May 27, 2019, 7:59 a.m.