zoo

The ts object has its limitations in representing the time series. It is used for representing equally spaced data. It cannot be used to represent the daily level stock prices as stock prices are equally spaced between Monday to Friday, but it is not the same case for Friday to Monday and in case there is market holidays on weekdays. This type of unequally spaced data cannot be represented by a ts object.

zoo is flexible and fully equipped to handle unequally spaced data, equally spaced data, and numerically indexed data.

Let us first install and load the zoo library. This can be done by executing the following code:

> install.packages("zoo") 
> library(zoo) 

Now we will discuss how to represent different time series scenarios using zoo.

Please note we will be using a common dataset for all the examples.

Constructing a zoo object

In order to create a zoo object, an ordered time index and data are required. So we are going to construct a zoo object.

Let us first import a few rows of our sample dataset, which can be done with the following code:

>StockData <- read.table("DataChap4.csv",header = TRUE, sep = ",",nrows=3) 

This gives the following output:

Date

Volume

Adj.Close

Return

12/14/2016

4144600

198.69

0.27

12/13/2016

6816100

198.15

2.97

12/12/2016

615800

192.43

0.13

Now let us try to convert this DataFrame into a zoo object. This can be done by executing the following code:

> dt = as.Date(StockData$Date, format="%m/%d/%Y") 
>Stockdataz = zoo(x=cbind(StockData$Volume,StockData$Adj.Close), order.by=dt)  
> colnames(Stockdataz) <- c("Volume","Adj.Close") 
> Stockdataz 

Upon execution, it generates the following zoo object:

Volume

Adj.Close

12/12/2016

615800

192.43

12/13/2016

6816100

198.15

12/14/2016

4144600

198.69

Reading an external file using zoo

The function read.zoo is a wrapper which can be used to read an external dataset, which assumes that the first column is the index and rest of the columns are data.

Now let us read a dataset using zoo which has the following format:

Date

Volume

Adj Close

Return

12/14/2016

4144600

198.69

0.27

We execute the following code:

>StockData <- read.zoo("DataChap4.csv",header = TRUE, sep = ",",format="%m/%d/%Y") 

This gives us an output with the following format:

Volume

Adj.Close

Return

2016-12-14

4144600

198.69

0.27

Advantages of a zoo object

Here are some of the examples that show the advantageous behavior of a zoo object.

Subsetting the data

Subsetting can be done on an index using the window() function by executing the following code:

>window(StockData, start=as.Date("2016/11/1"), end=as.Date("2016/11/3")) 

This gives the following output:

Volume

Adj.Close

Return

11/1/2016

7014900

190.79

-3.51

11/2/2016

4208700

188.02

-1.45

11/3/2016

2641400

187.42

-0.32

Merging zoo objects

Let us form two zoo objects with a common index and then merge them. This can be done by executing the following code:

> StockData <- read.table("DataChap4.csv",header = TRUE, sep = ",",nrows=3) 
> zVolume <-zoo(StockData[,2:2],as.Date(as.character(StockData[, 1]), format="%m/%d/%Y")) 
> zAdj.Close <-zoo(StockData[,3:3],as.Date(as.character(StockData[, 1]), format="%m/%d/%Y")) 
> cbind(zVolume, zAdj.Close) 

The final output is given in the following table:

zVolume

zAdj.Close

12/12/2016

615800

192.43

12/13/2016

6816100

198.15

12/14/2016

4144600

198.69

Plotting zoo objects

You can plot your data across time. A sample is shown here:

>plot(StockData$Adj.Close) 

This generates the following plot:

Plotting zoo objects

Figure 4.3: Time series plot using zoo object

Disadvantages of a zoo object

An index in a zoo object cannot have Date classed variables, whereas the index of an xts object has to be a known and supported time or Date class. Also, in zoo, we cannot add arbitrary attributes which can be done in xts.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset