Lab: MA(q) and AR(p)


Getting Started with R

Click on the R icon

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: MA(1)

The model: Yt = et - θ1et-1 (Book)
The model: Yt = et + b1et-1 (R)

Note: θ1 is the MA coefficient (Book)

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

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

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

> plot(y, type="o", ylab=expression(Y[t]))

> acf(y)

Notice that the R option ma=-0.9 produces a time series and ACF that looks like a θ1 > 0 on p. 59
and p. 59 Exhibit 4.2.
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

> y1 <- arima.sim(model=list(ma=-0.9), n=100)

> plot(y1, type="o", ylab=expression(Y[t]), main="MA(1)")

> acf(y1)

> help(arima) Gives details!

NOTE: There is an R function that will give you the theoretical values of the ACF that we derived in class!

> ARMAacf(ma=-0.9)


Example 2: AR(1)

The model: Yt = φ1Yt-1 + et

Simulate and plot the time series for φ1 = 0.5

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

> plot(y.50, type="o", main="AR(1)")

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)

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

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

How about we use the type="p" option!

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


What about an MA(2)?
See if you can simulate an MA(2) process on p. 63 (You will not be able to simulate the exact one!).

This is from another textbook!

Example 4: 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)!


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