www.apress.com

28/05/19

Simulating Autoregressive and Moving Average Time Series in R

by Margot Tollefson

A time series is a series of observations where each consecutive observation is separated in time (or space) by a set interval; for example, by days in a week or months in a year.  Two models commonly used in time series are the autoregressive model and the moving average model.  Both models fit time series for which the time series does not blow up over time (is stationary) and for which the variance of the observations is constant and the covariance between two observations is not zero and depends only on the number of observations between the two.  In this blog post, I describe the models for the two types of time series and I simulate one example of each.

For autoregressive time series:

New Content Item

For moving average time series:

New Content Item

Below is the function to create the two time series.  The simulation creates second order time series.

function( n=10000, a1=0.18828, a2=0.05861 ) {

# generate n+2 standard normal variates

E = rnorm(n+2)

# create an autoregressive process and plot the first 200 observations,
# the autocorrelation function, and the partial autocorrelation function

Y = numeric(n)
Y[1] = E[3] + a1*E[2] + a2*E[1]
Y[2] = E[4] + a1*Y[1] + a2*E[2]
for (i in 3:n) Y[i] = E[i+2] + a1*Y[i-1] + a2*Y[i-2]

par( mfcol=c( 3, 2 ) )

plot(Y[ 1:200 ], type="l", main="autoregressive")
acf( Y )
pacf( Y )

# create moving average process and plot the first 200 observations,
# the autocorrelation function, and the partial autocorrelation function


X = numeric( n )
for (i in 1:n ) X[ i ] = E[ i+2 ] + a1*E[ i+1 ] + a2*E[ i ]

plot(X[ 1:200 ], type="l", main="moving average")
acf( X )
pacf( X )
}

The plots are below.  For autoregressive processes, the partial autocorrelation plot goes below the error bands at the lag of the order of the series.  For moving average processes, the autocorrelation plot goes below the error bands at the lag of the order of the time series.

New Content Item


The plots confirm that Y is a second order autoregressive time series and X is a second order moving average time series.


About the Author

Margot Tollefson is a self-employed consulting statistician residing in the tiny town of Stratford in the corn and soybean fields of north-central Iowa. She started using the S-Plus language in the early 1990s and was happy to switch to R about ten years ago. Margot enjoys writing her own functions in R - to do plots and simulations, as well as to implement custom modeling and use published statistical methods. She earned her graduate degrees in statistics from Iowa State University in Ames, Iowa.

This article was contributed by Margot Tollefson, author of R Quick Syntax Reference.