1. Applying functions to rolling windows
Alright, great job!
2. Rolling functions from zoo
In the previous exercises, we looked at some of the 'rolling' versions of statistical functions from the zoo package, such as rollmean, rollmax, rollsum, etc. These functions work great, and in most cases, they're the best choice to use.
However, they can be quite limited in what they offer; notice that there isn't a 'rollmin' function for calculating the minimum of a rolling window!
Rather than create a rolling version of every possible function, the authors of zoo offer a solution. In this lesson, we'll cover how to generate our own 'custom' rolling window functions based on whatever summary function we want!
3. rollapply
The solution offered in zoo is a function called rollapply. Its syntax and arguments are a bit different than in the standard rolling functions, so let's see how it works.
The first argument we need to pass is our time series; here, it's the ftse dataset.
Then, we need to set the width of the window in the width argument. Take note that this argument is called 'width', while in the other functions like rollmean, it's called 'k'.
The FUN argument takes a summary function to apply to the rolling window, like mean, min, sum, and so on.
The align argument specifies the window alignment, described in the previous lesson.
Finally, the optional fill argument specifies how to fill in the values that are 'outside' the window; without a value in fill, the resulting time series drops these elements instead.
4. Roll-applying functions
Let's look at a few examples of using different summary functions with the rollapply function from zoo.
Imagine we want to calculate the range of values within a 30-day rolling window; this would give us an insight into the extreme values and the approximate 'variability' in our data.
For an example, let's take a time series of daily average temperatures, called daily_temp.
We can create a function – let's call it 'find_range', that subtracts the maximum of x, any input vector of numbers, from the minimum of x. This is just an example of writing our own function in R; we won't dive too deep into that in this course!
Our find_range function can be called like any other function in R, meaning we can use it within a rolling window.
We'll create a variable, called daily_temp_range, and use the find_range function in the FUN argument to rollapply. Our window width is 30, and our window alignment is 'right'. We also set the fill argument to NA, so that the first 29 observations – those that can't make a full, 30-day window – have a value of NA.
5. Roll-applying functions
Let's plot daily_temp_range.
Our output is a time series that represents the difference between the greatest and smallest values within the previous 30-day window. Again, our custom 'find_range' function is just an example; we could create a function for any task that we want, and use it in a rolling window with rollapply!
6. Let's practice!
Let's apply our skills in the exercises!