smartFilter: Calculate a moving dot product (or filter) over a numeric...

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

View source: R/smartFilter.R

Description

Calculate a moving dot product over a vector (typically a time series). It dynamically accounts for the incomplete windows which are caused by missing values and which occur at the beginning and end of the series. It does not propogate NAs.

Usage

1
2
smartFilter(y, weights, min.window = 1, start = 1, skip = 1,
  balance = TRUE)

Arguments

y

A numeric vector (can be labeled)

weights

Vector of weights that will be used to calculate the moving dot product. Should be odd in length and should sum to unity.

min.window

The minimum number of non-missing data points in a window that are required to calculate the dot product

start

The index of the center of the first window

skip

The number of indexes to advance the center of the moving window each time the dot product is calculated.

balance

= TRUE requires that the first non-missing value in a window occur on or before the center point of the window, and that the last non-missing value occur on or after the center point of the window.

Details

smartFilter has very similar behavior to filter, except it calculates at the edge of a series and it does not propogate NAs which may be imbedded within the series.

When the window contains missing values, either due to being at the edge of the series or due to NAs imbedded within the series, the weights corresponding to the non-missing data points are re-normalized and the dotproduct is calculated using the available data. If the number of non-missing data points in the window is less than min.window, an NA is produced for the corresponding index. Likewise, if balance = TRUE, and the required conditions (described above in the argument description of balance) are not met, an NA is returned for the corresponding index.

Value

Returns the moving dot product

Author(s)

Landon Sego

See Also

movAvg2, filter

Examples

 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
 # Define a simple vector
 x <- 2^(0:8)
 names(x) <- letters[1:9]

 # Define weights for a simple moving average of 3 points
 # (1 point in the past, the present point, and 1 point in the future)
 wts <- rep(1, 3) / 3

 # Note how they are the same, except at the edges of the series.
 smartFilter(x, wts)
 filter(x, wts)

 # filter() and smartFilter() apply the weights in reverse order of each other,
 # which makes a difference if the weights are not symmetric. Note how these
 # two statements produce the same result (with the exception of the first and
 # last elements)
 filter(x, 1:3 / 6)
 smartFilter(x, 3:1 / 6)

 # Notice how filter() propogates missing values
 y <- 3^(0:8)
 y[5] <- NA
 smartFilter(y, wts)
 filter(y, wts)

 # Compare starting on the second value and skip every other point
 smartFilter(x, wts)
 smartFilter(x, wts, start = 2, skip = 2)

 # Demonstrate how the 'min.window' and 'balance' work
 y <- round(rnorm(1:20),2)
 names(y) <- letters[1:20]
 y[7:9] <- NA
 y
 smartFilter(y, rep(1,5)/5, min.window = 2, balance = TRUE)
 smartFilter(y, rep(1,5)/5, min.window = 2, balance = FALSE)

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