Reader small image

You're reading from  Hands-On Time Series Analysis with R

Product typeBook
Published inMay 2019
Reading LevelBeginner
PublisherPackt
ISBN-139781788629157
Edition1st Edition
Languages
Right arrow
Author (1)
Rami Krispin
Rami Krispin
author image
Rami Krispin

Rami Krispin is a data scientist at a major Silicon Valley company, where he focuses on time series analysis and forecasting. In his free time, he also develops open source tools and is the author of several R packages, including the TSstudio package for time series analysis and forecasting applications. Rami holds an MA in Applied Economics and an MS in actuarial mathematics from the University of MichiganAnn Arbor.
Read more about Rami Krispin

Right arrow

Working with zoo and xts Objects

In the previous chapter, we introduced the core class in R for time series data, the ts object. In this chapter, we will focus on another common data structure for time series data—the zoo class and its extension and the xts class, from the zoo and xts packages respectively. Those two classes are popular in the domain of financial time series analysis (that is, stock prices, indices, and so on), mainly due to their index format, which can store external date and time objects such as the Date, POSIXct/lt, yearmon, and yearqtr classes. Throughout this chapter, we will introduce methods and techniques for creating, manipulating, and visualizing zoo and xts objects.

In this chapter, we will cover the following topics:

  • Creating, working with, and manipulating zoo and xts objects
  • The attributes of the zoo and xts classes
  • The yearmon and yearqtr...

Technical requirement

The zoo class

The zoo package provides a framework for working with regular and irregular time series data. This includes the zoo class, an indexed object for storing time series data, and a set of functions for creating, preprocessing, and visualizing time series data. Similar to the ts and mts classes, the zoo class is comprised of two components:

  • Data structure: A vector (for univariate time series data) or matrix (for multivariance time series data) format
  • Index vector: This stores the series observation's corresponding index

On the other hand, unlike the ts class, the index of the zoo class has a flexible structure, as it can store different date and time classes, such as Date, POSIXct/lt, yearmon or yearqtr, as indices.

yearmon and yearqtr are two index classes for regular time series data. The yearmon class is suitable for representing a monthly time series when...

The xts class

The eXtensible time series, xts package is an extension of the zoo package. It provides the xts class and a set of functions and tools for preprocessing, manipulating, and visualizing time series data. The xts class is a zoo object with additional attributes. Therefore, by default, any xts object carries zoo class attributes, and any of the zoo functions can be applied to the xts object. In the following examples, we will use the Michigan_CS series, which is an xts object that represents the famous consumer sentiment index of the University of Michigan since 1980. This series is available on the TSstudio package. We will start by loading the series and will review its main characteristics:

data(Michigan_CS)

ts_info(Michigan_CS)
## The Michigan_CS series is a xts object with 1 variable and 468 observations
## Frequency: monthly
## Start time: Jan 1980
## End time...

Manipulating the zoo and xts objects

One of the main advantages of the zoo class and, in particular, the xts class (with respect to the ts object), is their friendly and simple structure. This allows us to preprocess and manipulate these types of objects in a seamless manner. Both the zoo and the xts packages provide a variety of utility functions for working with these types of objects. In this section, we will introduce some of the main functions for manipulating the zoo and xts objects.

Merging time series objects

In some instances, you may want to join or merge different series into a single object. Both the zoo and xts packages provide functions for merging a time series object (including merging mixed classes, such as...

Plotting zoo and xts objects

As we discussed in the previous chapter, there are two main approaches for plotting time series objects such as the zoo and xts objects:

  • Direct: Using built-in or customized functions to visualize either the zoo or xts objects, such as plot.zoo and plot.xts functions, or other visualization tools from the TSstudio or dygraph packages
  • Indirect: By transforming or reformatting the structure of the object and using some data visualization packages, such as ggplot2, plotly, and rbokeh
Note that plotting the zoo or xts objects with the TSstudio or dygraph packages follows the exact same process as we demonstrated in the previous chapter with the ts object. Therefore, to avoid redundancy, we will focus in this section only on the built-in visualization functions of the zoo and xts packages.

The use of either approach depends on the user's needs and...

xts, zoo, or ts – which one to use?

It depends.

There is no doubt that out of the three objects we have introduced so far (ts, zoo, and xts), the xts class is the most advanced and friendly to use. Moreover, since the xts class is also a zoo class with additional functionalities and improvements, the question actually should be xts or tswhich one to use? This mainly depends on the type of packages and applications you are using for time series analysis. However, in my mind, working with xts objects has more benefits compared to ts objects, since most of the forecasting models in R support only ts objects.

On the other hand, if you're not bound by requirements or if you just want to slice and dice a time series object, it is highly recommended that you use the xts (or zoo) object. The good news here, as we saw in some instances, is that both the xts and zoo...

Summary

In this chapter, we introduced two of the most advanced classes for time series data in R, the zoo and xts classes, and their applications. It is safe to claim that working with those objects, in particular, the xts class, is more friendly and convenient than the ts class. Their unique structure of data frames and well-organized time indices gives users seamless preprocessing and faster data querying. Furthermore, the zoo and xts packages have rich functionality and applications; more than we can cover in one chapter. Therefore, it is highly recommended that you look at the documentation and vignettes of the packages for more information.

In the next chapter, we will look at the decomposition of time series data process.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Time Series Analysis with R
Published in: May 2019Publisher: PacktISBN-13: 9781788629157
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Rami Krispin

Rami Krispin is a data scientist at a major Silicon Valley company, where he focuses on time series analysis and forecasting. In his free time, he also develops open source tools and is the author of several R packages, including the TSstudio package for time series analysis and forecasting applications. Rami holds an MA in Applied Economics and an MS in actuarial mathematics from the University of MichiganAnn Arbor.
Read more about Rami Krispin