Calculate the moving average using a 2-sided, symmetric window

Share:

Description

Wrapper for smartFilter that creates a set of symmetric weights for the 2-sided window based on the Gaussian kernel, exponential decay, linear decay, or simple uniform weights, and then calculates the moving average (dot product) using those weights.

Usage

1
2
3
4
5
6
7
8
movAvg2(y = NULL, bw = 30, type = c("gaussian", "exponential", "linear",
  "uniform"), furthest.weight = 0.01, center.weight = 1, ...)

## S3 method for class 'movAvg2'
print(x, ...)

## S3 method for class 'movAvg2'
plot(x, ...)

Arguments

y

The numerical vector for which the moving averages will be calculated. If NULL, the moving averages are not calculated, but the weights are calculated and included in the attributes of the returned object.

bw

A single, positive whole number that indicates the 'bandwidth' of the window, which is roughly half the width of the moving window. The total width of the window is 2 * bw + 1.

type

Character string which uniquely indentifies the type of weights to use, corresponding to the Gaussian kernel, exponential decay, linear decay, or uniform weights. Defaults to gaussian.

furthest.weight

A single, positive number corresponding to the unormalized value of the weights at the left and right edges of the window. Ignored when type = 'uniform'.

center.weight

A single, positive number corresponding to the unnormalized value of the weights at the center of the window.

x

Object of class movAvg2.

...

For movAvg2, these are additional arguments to smartFilter. For the print and plot methods, the "..." are additional arguments passed to print.default and plot.default, respectively.

Details

All the weights are normalized (so that they sum to 1) prior to calculating the moving average. The moving "average" is really the moving dot product of the normalized weights and the corresponding elements of y.

Since it uses smartFilter to calculate the moving average, the moving average for points near the edge of the series or in the neighborhood of missing values are calculated using as much of the window weights as possible.

Value

An object class movAvg2, which is a numeric vector containing the moving average (dot product) of the series, with attributes that describe the weights. If y = NULL, numeric(0) is returned.

Methods (by generic)

  • print: Prints the movAvg2 object by only showing the series of dot products and suppressing the attributes.

  • plot: Plots the unnormalized weights.

Author(s)

Landon Sego

See Also

smartFilter, filter

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
z <- movAvg2(rnorm(25), bw = 10, type = "e", center.weight = 2)
z

# Look at the attributes
attributes(z)

# Plot the weights
plot(z)

# If we just want to see the weights (without supplying data)
plot(movAvg2(bw = 20, type = "g", center.weight = 1))

# Note how it produces the same values as filter (except at the edge
# of the series
x <- rnorm(10)
movAvg2(x, bw = 2, type = "u")
filter(x, rep(1, 5) / 5)

# These are also the same, except at the edge.
movAvg2(x, bw = 1, type = "l", furthest.weight = 0.5, center.weight = 1)
filter(x, c(0.5, 1, 0.5) / 2)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.