Learn R Programming

aTSA (version 3.1.2)

MA: Moving Average Filter

Description

Applies moving average filter to estimate the linear trend or nonseasonal pattern.

Usage

MA(x, nlag = NULL, plot = TRUE)

Value

A list with class "MA" containing the following components:

estimate

the smoothed values.

nlag

the period used to compute the average.

accurate

the accurate measurements.

Arguments

x

a numeric vector or univariate time series.

nlag

the number of period to calculate the average. The default is NULL.

plot

a logical value indicating to print out the plot. The default is TRUE.

Author

Debin Qiu

Details

The moving average filter uses the unweight mean of (2*nlag + 1) adjacent observations. That is, $$hat{X}[t] = (X[t - nlag] + ... + X[t] + ...+ X[t + nlag])/(2*nlag + 1)$$ for \(nlag < t < n - nlag\). For the values at the boundary \(t \le nlag\) or \(n - nlag \le t \le n\), you can refer to Equation (7) in Qiu et al., (2013) for details of calculations. The default method for choosing the optimal nlag uses the rule-of-thumb criterion proposed by Qiu, et al., (2013), in which they showed that the moving average is a special case of local linear estimator in the sense that the kernel function is the uniform one, and the moving average period nlag is a function of bandwidth. Thus, choosing the optimal nlag is equivalent to choosing the optimal bandwidth in local linear regression.

The plot of original values v.s fitted values will be displayed if plot = TRUE.

References

D. Qiu, Q. Shao, and L. Yang (2013), Efficient inference for autoregressive coefficient in the presence of trend. Journal of Multivariate Analysis 114, 40-53.

P.J. Brockwell, R.A. Davis, Time Series: Theory and Methods, second ed., Springer, New York, 1991.

Examples

Run this code
x <- arima.sim(list(order = c(1,0,0),ar = 0.4),n = 100)
y <- 5*(1:100)/100 + x
MA(y)

# moving average filter for co2 data
MA(co2)

Run the code above in your browser using DataLab