1. Subsetting a window of observations
Welcome back to the course!
2. Time series windows
An important tool in time series analysis is the "window"; a time series window can be thought of as a smaller subset of a range of values taken from a larger time series, which inherits many of the parent's properties. For example, a window obtained from a regular time series is also regular, and has the same sampling frequency.
The difference is in the start and end points; when creating a window, we define a new start and end point to extract data from our time series.
We create time series windows for many of the same reasons as when we subset other types of data. For example, we might want to focus in on a particular range of data, or zoom in on an interesting event to see if it affected the values in the data.
We might also wish to exclude a certain range of data; maybe a sensor like a digital thermometer stopped providing accurate readings, so we only want to take a window when the readings were still up to par.
3. Time series windows
Let's look at an example! Our trading card time series, card_prices, ranges in time from January 1, 2013 to December 31, 2014.
To create a time series window, we can call the window function from the Base R stats package. This function has three arguments: x, the time series we want to subset; start, the start point, and end, the end point. When working with a zoo object that has an index of the Date class, we can enter the start and end arguments as a plain character vector. The start and end arguments are inclusive; if there are observations at those indices, they'll be included in the window.
The result is a time series that ranges from the value we assign in start to the value we assign in end; any observations with an index less than "start" or greater than "end" are omitted!
4. Subsetting by logical expressions
Another method of creating a subset of a time series is to use "relational operators", such as 'greater than', represented by a right angle bracket, 'less than', represented with a left angle bracket, 'is equal to', represented by two equals signs, and so on. By comparing the index to specific values, we can generate complex logical statements, which say "return TRUE if the index is greater than or equal to May 1, 2013 and if the index is less than or equal to July 31, 2013".
This subset is a logical vector,TRUE or FALSE, that we can assign to a variable; let's call it 'subset'. Using the extraction operator — the square brackets — we can extract the observations of our time series from the subset vector. Let's subset our time series to a variable called card_subset.
5. Extracting specific dates
If we only want to return an observation from a specific date, we can use the square bracket extraction operator to do so.
Because the index of our zoo object card_price is of the Date class, zoo is able to interpret the value we pass, "2014-02-28" as a date, even though technically it's a character object.
6. Let's practice!
Alright, let's head over to the exercises and practice extracting windows and subsets from time series datasets!