Lab: AR(p) and MA(q)


Getting Started with R

Click on the R icon

If no R icon, go to Start and then All Programs to find R

Let the games begin.

R function: arima.sim

We need to use help to find out about this function.

> help(arima.sim)

What arguments does this function take?

What is a list?


Example 1: AR(1)

The model: Zt = φ1Zt-1 + at

Simulate and plot the time series for φ1 = 0.5

> y.50 <- arima.sim(model=list(ar=.5), n=100)

> plot(y.50)

Plot the ACF.

> acf(y.50)

> temp <- acf(y.50, plot=F)

What's in temp? What do you get when you type the following?

> temp$acf

Let's now use the R function set.seed() to allow us to reproduce our results! Repeat the above commands after

> set.seed(1)
> y.50 <- arima.sim(model=list(ar=.5), n=100)

Now, for the PACF,

> acf(y.50,type="partial")

You can look at plot of Z_t versus Z_t-1 for the AR(1) series

> plot(lag(y.50,1), y.50, xlab=expression(Z[t-1]), ylab=expression(Z[t]))

Simulate and plot the time series for φ1 = 0, φ1 = 0.5, φ1 = -0.5, and φ1 = 0.9.

Let's look at sample code in ar1.ex1.r

Now plot the ACF and PACF of the time series. How would you describe the characteristics?

Let's look at sample code in ar1.ex2.r


Example 2: AR(2)

The model: Zt = φ1Zt-1 + φ2Zt-2 + at

Simulate and plot the ACF of the time series for φ1 = -0.5, φ2 = 0.3

> y <- arima.sim(model=list(ar=c(-0.5,0.3)), n=100)

> acf(y)

> pacf(y)

Activity 1: Compare your sample ACF and PACF to Table 3.3 and Figure 3.8 Plots.

Activity 2: Look at the theoretical ACF and PACF plots in Figure 3.7 and locate the plot that the parameter values satisfy.

Activity 3: Calculate the theoretical ACF and PACF for k=0,1,2, and 3.
Hint: For the ACF use (3.1.2), (3.1.21), and (3.10); For the PACF use (3.1.23a), (3.1.23b), and (3.1.23c).
Compare the theoretical values to the sample values.

Repeat ALL of the above by using n=1000 (or more)!



Example 3: MA(1)

The model: Zt = at - θ1at-1 (Book)
The model: Zt = at + b1at-1 (R)

Note: θ1 is the MA coefficient (Book)

Simulate and plot the ACF and PACF of the time series for θ1 = 0.5

> y <- arima.sim(model=list(ma=0.5), n=100)

OR y1 <- arima.sim(model=list(ma=-0.5), n=100)?

> acf(y)

> acf(y, type="partial")

Notice that ma=0.5 produces an ACF that looks like a θ1 < 0 on p. 49.
We will just have to keep straight the difference in notation between your book and R!

So, to simulate the above model consistent with your book, we should use

> y <- arima.sim(model=list(ma=-0.5), n=100)

> acf(y)

> acf(y, type="partial")

> help(arima) Gives details!


Example 4: Can you simulate a white noise process? Plot the ACF and PACF?


Here are some Introductory Lessons in R

Here are some nice Tutorials


Interested in writing your own function?
You could write your own function to simulate an AR(1)

Let's look at sample code in ar1sim.r