movAvg2: Calculate the moving average using a 2-sided, symmetric...

Description Usage Arguments Details Value Methods (by generic) Author(s) See Also Examples

View source: R/movAvg2.R

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.

...

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.

x

Object of class movAvg2.

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)

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)

Example output

 [1] -0.42246717 -0.04026814 -0.15453932 -0.19089063 -0.18035891 -0.05506676
 [7]  0.11082438  0.26838750  0.47091483  0.51003175  0.66714743  0.55333958
[13] -0.03773782 -0.25563888 -0.28693830 -0.45483428 -0.48245691 -0.57125106
[19] -0.73233910 -0.92719849 -0.89455484 -1.02054225 -1.10389033 -0.77465587
[25] -0.23223133
$class
[1] "movAvg2" "numeric"

$type
[1] "exponential"

$bw
[1] 10

$furthest.weight
[1] 0.01

$center.weight
[1] 2

$wts
 [1] 0.01000000 0.01698646 0.02885400 0.04901274 0.08325532 0.14142136
 [7] 0.24022489 0.40805715 0.69314484 1.17740804 2.00000000 1.17740804
[13] 0.69314484 0.40805715 0.24022489 0.14142136 0.08325532 0.04901274
[19] 0.02885400 0.01698646 0.01000000

 [1] 0.16653051 0.14638273 0.25636898 0.18141125 0.03419059 0.40835384
 [7] 0.75468427 0.76139767 0.64940854 0.85349705
Time Series:
Start = 1 
End = 10 
Frequency = 1 
 [1]         NA         NA 0.25636898 0.18141125 0.03419059 0.40835384
 [7] 0.75468427 0.76139767         NA         NA
 [1]  1.3138440  0.3182095 -0.7141024 -0.2474012  0.6719804  0.7880413
 [7]  0.3241647  0.4701930  1.0945207  1.0924511
Time Series:
Start = 1 
End = 10 
Frequency = 1 
 [1]         NA  0.3182095 -0.7141024 -0.2474012  0.6719804  0.7880413
 [7]  0.3241647  0.4701930  1.0945207         NA

Smisc documentation built on May 2, 2019, 2:46 a.m.