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

To quit R:

> q()

What does it ask you? What does this mean?

For R HELP, for example to get help about the function rnorm ,

> help(rnorm)

You can also go to Help on the R toolbar and select R Help.


Example 1: Let the games begin.

Generate a sample of n=100, N(0,1) random variables.

> help(rnorm)

> rnorm(100)

What happened?

Now, let's try:

> temp <- rnorm(100)

What is in the object temp?

What is the length of the oject temp?

Make a informative plot of temp?

Q: How can you make a time series plot of the data? (The model is: Zt = at)

Let's now use the R function set.seed() to allow us to reproduce our results! Repeat the above command: (temp <- rnorm(100) after

> set.seed(1)

Compare what is in the object temp with a neighbor. It should be exactly the same!


Example 1: AR(1)

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

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?

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: Can you simulate a white noise process? Plot the ACF and PACF?


Example 3: The Random Walk

Let's look at the sample code in random_walk_ex.r

Q: What happens when you change (larger and smaller) the standard deviation of ythe noise?


Example : 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.19), (3.1.20), and (3.31); 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)!


(NOW, GO TO THE INTRO TO SWEAVE/KNITR EXAMPLE AT THE BOTTOM OF THE LAB!

Due to Popular Demand, Let's go to R Markdown instead!

in: STAT 575 Lab 1 and using the Dr. Dribble Example 2.

Here is a R Markdown Tutorial!



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!


Intro to Sweave/knitr:

Here are some knitr resources
Send me more and I will gladly add to this!

Here is a HW template that I have made for you (Thanks Nick!): STAT673HW0.Rnw
From the STAT 672 Lab1 for Example 2 (Dr. Dribble) and Example 3

Let's make a template for STAT 673 Lab1 from Example 2: the Random Walk (or something up there!).


Intro to R:

Try going through the following Intro. Lessons. The # sign is a comment, so you can copy and paste.

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