Lab: Introduction to R and Time Series


Getting Started with R

Click on the R icon

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: Yt = et)

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 2: 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 the noise?


Text Book webpage and TSA R Package:

Let's see if we can install the TSA R package on our computers!

Let's see if we can use the TSA R package and produce the plots in Chapter 1 from: Text Book webpage


Check out more tutorials on R linked off the Course Calendar!


NOW FOR KNITR!

Intro to knitr:

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

Let's look at Example 2 Dr. Dribble:
From the STAT 672 Lab1

Here is a HW template that I have made for you (Thanks Nick!): STAT672HW0knitr.Rnw

Note: We really should NOT call this a knitr document, since we are just Sweaving! You are welcome to use knitr in the future!

When we selected Sweave in the global options, when you complile your pdf, it actually adds the line of code:
\SweaveOpts{concordance=TRUE}



BELOW FOR LAB 1 PART II

Example 3: AR(1)

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

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

Q: What are the differences that you observe in the 4 plots and how do they compare to the Random Walk?