Chapter 14. Time Series Analysis
Introduction
Time series analysis has become a hot topic with the rise of quantitative finance and automated trading of securities. Many of the facilities described in this chapter were invented by practitioners and researchers in finance, securities trading, and portfolio management.
Before starting any time series analysis in R, a key decision is your choice of data representation (object class). This is especially critical in an object-oriented language such as R because the choice affects more than how the data is stored; it also dictates which functions (methods) will be available for loading, processing, analyzing, printing, and plotting your data. When I first started using R, for example, I simply stored my time series data in vectors. That seemed natural. However, I quickly discovered that none of the coolest analytics for time series analysis worked with simple vectors. I switched to using an object class intended for time series analysis, and this was the gateway to important functions and analytics.
This chapter’s first recipe recommends using the zoo
or xts
packages for representing time series
data. They are quite general and should meet the needs of most users.
Nearly every subsequent recipe assumes you are using one of those two
representations.
Note
The xts
implementation is a superset of
zoo
, so xts
can do everything that
zoo
can do. In this chapter, whenever a recipe works
for a zoo
object, you can safely assume (unless stated otherwise) ...
Get R Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.