movingAve: Moving Averages

View source: R/movingAve.R

movingAveR Documentation

Moving Averages

Description

Implements the Savitzky-Golay (Savitzky and Golay, 1964) filter on a regular series of data to compute a moving average.

Usage

movingAve(x, span = 3, order = 0, pos = "center")

Arguments

x

the data to be averaged or differenced. Missing values are permitted but result in missing values in the output.

span

the length of the data to be averaged.

order

the polynomial order for averaging. Must be less than span.

pos

how to position the output data relative to the value returned; "center" means that the value represents the average of the most central value relative to the span, "end" or "trailing" means the the value is the average of the preceding span values, and "begin" or "leading" means the value is the average of the following span values.

Value

A vector of the same length as x containing the averages.

Note

For odd values of span and pos equal to "center," order equal to 0 or 1 gives the same result.

In general, there is no reason to use polynomial orders greater than 2, and pos should always be set to "center" for polynomial orders greater than 1 to avoid strange behavior due to end effects.

The weights for the averages are computed based on linear model theory (Savitzky and Golay, 1964; Wood and Hockens, 1970). Wood and Hockens (1970) also discuss some artifacts resulting from smoothing.

References

Savitzky, A., and Golay, M.J.E., 1964, Smoothing and differentiation of data by simplified least squares procedures: Analytical Chemistry, v. 36, no. 8, p. 1627–1639.

Wood, L.C., and Hockens, S.N., 1970, Least squares smoothing operators: Geophysics, v. 35, no. 6, p. 1005–1019.

See Also

filter, diff, movingDiff

Examples


## Construct a simple valley
movingData <- abs(seq(-5, 5))
movingAve(movingData, span=5)
movingAve(movingData, span=5, order=2)

USGS-R/smwrBase documentation built on Oct. 18, 2022, 9:55 a.m.