Get startedGet started for free

Rolling functions

1. Rolling functions

Another useful set of indicators for time series data involves applying a function over a particular window in your data. These windows can be discrete -- for example, including all observations since the beginning of the year -- or rolling -- for example, including the last ten observations in the data.

2. Discrete windows

In the context of economic data, you may be interested in the number of new jobs added since the beginning of the decade or the maximum level of unemployment since the beginning of the year. Generating rolling functions over discrete windows such as these requires you to follow the split-lapply-rbind sequence, which involves splitting your data according to a particular period, applying a function within that period, and then binding the new data into another xts object.

3. Rolling windows

Alternatively, it may be more useful to calculate a rolling average across a certain number of periods. This type of indicator can help smooth out short-term volatility in your time series data. For example, given month-to-month or seasonal volatility in unemployment, you may find it helpful to generate a rolling average across several periods. To generate an indicator across a rolling window, you can use the rollapply() function, which allows you to specify a rolling window length and a function to apply to that window.

4. Let's practice!

In the last set of exercises in this chapter, you'll use these techniques to generate some useful rolling indicators in your economic data. Let's practice!