0% found this document useful (0 votes)
24 views4 pages

Time Series Practice

The document provides instructions for using R to analyze time series data. It discusses importing data files, plotting variables, and saving plots. Key steps include reading in data, assigning it to variables, using commands like plot.ts to visualize time series, and saving graphics files for assignments. Examples analyze Southern Oscillation Index and recruitment data.

Uploaded by

Aa Hinda no Aria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Time Series Practice

The document provides instructions for using R to analyze time series data. It discusses importing data files, plotting variables, and saving plots. Key steps include reading in data, assigning it to variables, using commands like plot.ts to visualize time series, and saving graphics files for assignments. Examples analyze Southern Oscillation Index and recruitment data.

Uploaded by

Aa Hinda no Aria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment 1

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.

1. Directories. Create a directory within your “home directory” titled “timeseries”.


Create a directory within this “timeseries” directory titled “assignment1”.

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.

• Go to File >> Change dir...


• Use command “setwd(dir)”, where dir is replaced by the path of the working
directory you want. E.g.
setwd("C:/Users/Owner/Documents/timeseries/assignment1")

The command “getwd()” will display the current working directory.


• Create shortcut to R in desired directory. Right click on shortcut, select “Prop-
erties”, click the “Shortcut” tab, and delete whatever’s in “Start in”. Launching
R with this shortcut will set the working directory to be the one it’s stored in.

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")

scan() is used when the data is a vector.


R has read the numbers and output them to the screen. We would like to store the
numbers so that we can work with them. To do this, we declare a variable soi and set
it equal to the output of the scan() command, i.e. the list of numbers. Type

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().

6. Easy plotting. Making attractive plots is one of R’s strong suits.

(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. (No R required). Suppose E(X) = 0, Var(X) = 4, E(Y ) = 3, Var(Y ) = 1, and


Corr(X, Y ) = 0.5. Find

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 ?

You might also like