1. Time series objects with the zoo package
Alright, it's the moment we've been waiting for – in this lesson, we'll cover how to create a time series object in R, using the zoo package!
2. The zoo package
The zoo package provides numerous tools and functions for time series analysis and manipulation: from creating a time series object itself to plotting our objects with ggplot2.
3. zoo versus base R
So why use a standalone package for this? Base R – specifically, the stats package – has the ts class, so what's the reason for using zoo?
To start, the ts class can only accept data when the time intervals between observations are evenly spaced. With zoo, however, the time between samples can be uneven.
Methods exist to convert zoo objects to objects like data frames, making them more convenient to use in other data science applications. We'll cover converting to data frames later in the chapter.
So, what's the process for creating a zoo object?
4. Creating a zoo object
To make a time series in zoo, there's two kinds of data we need.
First, we have to have the values of our observations, such as the temperature at each point in time, or the daily number of patrons at the library, and so on.
Then, we need the temporal data – the time of each observation. In this example, our data was sampled daily, so our "sample_dates" vector is a series of consecutive days.
5. Creating a zoo object
Once we load the zoo package, we can use the aptly-named zoo function to create our time series. The zoo function has two required arguments: x, representing our values, and order-dot-by, which we set as our index, sample_dates.
If we print our time series, we see that each row contains a time of observation and the value of the observation at that time.
6. Converting to zoo from ts
Another important consideration is that many older datasets were created using base R and the ts class.
The zoo package offers functionality to convert these to the zoo class; to do this, we use the as-dot-zoo function, and pass our ts time series object.
7. zoo versus ts
As a reminder – while the actual data stored within a ts or zoo object is quite similar, the methods used to manipulate, explore, and visualize these objects, such as print, are different depending on the class.
Nowadays, the best practice is to use zoo objects over ts, but it's good to know how to convert to zoo from other types of object.
8. zoo and ggplot2
Another thing to consider – while using the autoplot function works well in most cases, it's also possible to generate more robust plots with the ggplot2 functions.
The syntax to do so is similar to that of a data frame – in fact, ggplot2 converts your zoo to a data frame in the background – something we'll cover how to do later in the chapter.
We set the first argument, data, to the time series object. The x and y values are set to Index and the name of the variable the time series is mapping. By default, it's the name of the time series.
We can use arguments to set the attributes of the plot, such as the plotting color, line size, line type, and so on!
We have to include the "scale_y_continuous" to make sure ggplot knows how to properly handle our time series data.
We can also use ggplot2 themes and give our plot a title and labels!
9. zoo and ggplot2
And here it is!
10. Let's practice!
The zoo package has lots of powerful functions for working with time series data, but for now, let's head over to the exercises and practice creating time series with zoo!