Time Series Practice
Time Series Practice
1 R Tutorial
In this tutorial, we will learn some of the basics of the statistical software R. We will learn
how to read data in from a text file, how to handle variables, how to plot data, and how to
save these plots.
2. Launch R. You should always make sure you know what the working directory is.
There are a few ways to set the working directory.
3. Scripts. Scripts are almost like text files which contain the commands that you will enter
on the command line in R. I highly recommend using scripts instead of typing
commands directly to the command line. It’s much easier to edit commands this way
and you can save your commands for future reference. Use File >> New script to open a
window to type in your script. To save, use File >> Save as, and make sure to save the
script with a .R extension.
4. Download files. Obtain the data file, “soi.dat” and save this file in “assignment1”. We
want to import this data into R. One way to do this is to use the scan() command;
simply type
1
scan("soi.dat")
soi<-scan("soi.dat")
5. Command line. Now, we will get familiar with using the command line:
(a) Type ls(). This command lists all the variables present in your workspace. Right
now there should only be soi.
(b) To check the contents of a variable, simply input the name of the variable, such
as soi.
(c) Create a variable temp equal to 2 using the command temp<-2
(d) Type ls(). What do you see?
(e) Remove the variable temp by typing rm(temp).
(f) Now, check to make sure that it’s gone by typing ls().
(a) Create a simple plot of the soi variable using plot(soi). We can expand on this
by changing the x and y labels and adding a title.
plot(soi,xlab="time", ylab="Southern Oscillation Index",
main="Time Series Plot")
Note: for all assignments, all plots must be labeled to obtain full credit.
(b) Next, download the file “recruit.dat”, and load the data into the variable recruit.
7. Time series plots. Since these time series are related, we would like to plot soi and
recruit on the same plot. Right now we have both variables as basic vectors. We
want to first plot them as time series which makes the graphs more attractive and
easier to interpret.
(a) To plot a vector as a time series we can use the following command:
plot.ts(soi)
Notice the difference from how the graph appeared before. Another way to pro-
duce a time series plot is to type the following
soi<-ts(soi)
plot(soi)
2
(b) We want to plot both series together. First, we need to split up the plot “device”.
We do this with the command
par(mfrow=c(1,2))
This will give one row and two columns.
(c) Now, we plot both series by typing plot.ts(soi) and then entering plot.ts(recruit).
8. Saving plots. We can save this plot in any format that we would like (pdf, postscript,
jpg, etc). For example, we want to save this graphic as “joint.jpg”.
jpeg("joint.jpg")
par(mfrow=c(2,1))
plot.ts(soi)
plot.ts(recruit)
dev.off()
Note that you won’t actually see the plot. The dev.off() command will produce the
completed file. Without it you may get nothing at all. To save as pdf, replace
jpeg("joint.jpg") with pdf("joint.pdf").
2 Assignment
1. Read chapters 1, 2, 3, 6, 7, and 8 of An Introduction to R and answer the following questions.
(a) What’s the difference in output between the commands 2*1:5 and (2*1):5? Why
is there a difference?
(b) If you wanted to enter the odd numbers from 1 to 19 in the variable x, what
command would you use?
(c) If you create a variable using the following command y=c(-1,2,-3,4,-5), what
command would put the magnitude of the values of y into the variable z?
(d) What R command would give you the 95th percentile for a chi-squared distribution
with 10 degrees of freedom?
(e) Generate a vector of 1000 standard normal random variables using the command
x=rnorm(1000), use R to give a five number summary of your simulated data;
what is the mean and variance of your x variable? Make and print a histogram
for this data.
2. Download “gas.dat”. The data are monthly heating oil prices from January 1973 to
December 1987. Make a time series plot of the data. Be sure to label the axes and
give a title for the plot. Print out the plot to turn in.
3
(a) Var(X + Y ).
(b) Cov(X, X + Y ).
(c) Corr(X + Y, X − Y ).
4. (No R required). Suppose that {wt } and {zt } are independent and identically dis-
tributed (iid) sequences, with P (wt = 0) = P (wt = 1) = 12 and P (zt = −1) = P (zt =
1) = 12 . We define a time series model as
xt = wt (1 − wt−1 )zt .
Show that {xt } is white noise but not independent. Hint: obtain the mean and variance
for zt . For showing dependence, when xt−1 = 1, what does this imply about the values
of wt−1 and xt ?