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.
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:
|
|
|
|
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:
|
| |
12/12/2016 |
615800 |
192.43 |
12/13/2016 |
6816100 |
198.15 |
12/14/2016 |
4144600 |
198.69 |
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:
|
|
|
|
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:
|
|
| |
2016-12-14 |
4144600 |
198.69 |
0.27 |
Here are some of the examples that show the advantageous behavior of a zoo
object.
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:
|
|
| |
11/1/2016 |
7014900 |
190.79 |
-3.51 |
11/2/2016 |
4208700 |
188.02 |
-1.45 |
11/3/2016 |
2641400 |
187.42 |
-0.32 |
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:
|
| |
12/12/2016 |
615800 |
192.43 |
12/13/2016 |
6816100 |
198.15 |
12/14/2016 |
4144600 |
198.69 |