Learn R Programming

animation (version 1.1-0)

mwar.ani: Demonstration for ``Moving Window Auto-Regression''

Description

This function just fulfills a very naive idea about moving window regression using rectangles to denote the ``windows'' and move them, and the corresponding AR(1) coefficients as long as rough confidence intervals are computed for data points inside the ``windows'' during the process of moving.

Usage

mwar.ani(x, k = 15, conf = 2, mat = matrix(1:2,
    2), widths = rep(1, ncol(mat)), heights = rep(1, nrow(mat)),
    lty.rect = 2, ...)

Arguments

x
univariate time-series (a single numerical vector); default to be sin(seq(0, 2 * pi, length = 50)) + rnorm(50, sd = 0.2)
k
an integer of the window width
conf
a positive number: the confidence intervals are computed as [ar1 - conf*s.e., ar1 + conf*s.e.]
mat, widths, heights
arguments passed to layout to divide the device into 2 parts
lty.rect
the line type of the rectangles respresenting the moving ``windows''
...
other arguments passed to points in the bottom plot (the centers of the arrows)

Value

  • A list containing
  • phithe AR(1) coefficients
  • Llower bound of the confidence interval
  • Uupper bound of the confidence interval

Details

The AR(1) coefficients are computed by arima.

References

Robert A. Meyer, Jr. Estimating coefficients that change over time. International Economic Review, 13(3):705-710, 1972. http://animation.yihui.name/ts:moving_window_ar

See Also

arima

Examples

Run this code
# moving window along a sin curve 
oopt = ani.options(interval = 0.1, nmax = 50)
op = par(mar = c(2, 3, 1, 0.5), mgp = c(1.5, 0.5, 0))
mwar.ani(lty.rect = 3, pch = 21, col = "red", bg = "yellow",type='o')

# for the data 'pageview'
ani.options(interval = 0.1, nmax = 50)
data(pageview)
mwar.ani(pageview$visits, k = 30)

par(op)

# HTML animation page
ani.options(ani.height = 500, ani.width = 600, nmax = 50,
    title = "Demonstration of Moving Window Auto-Regression",
    description = "Compute the AR(1) coefficient for the data in the
    window and plot the confidence intervals. Repeat this step as the
    window moves.")
ani.start()
par(mar = c(2, 3, 1, 0.5), mgp = c(1.5, 0.5, 0))
mwar.ani(lty.rect = 3, pch = 21, col = "red", bg = "yellow",type='o')
ani.stop()

ani.options(oopt)

Run the code above in your browser using DataLab