What is a rolling window?

Manipulating Time Series Data in R

Harrison Brown

Graduate Researcher in Geography

Windows

  • A window is a limited range of observations

Conceptual analogy highlighting the similarities between a time series window and the scroll bar on a computer monitor.

Manipulating Time Series Data in R

Windows

Original data:

Plot of the FTSE time series, which has a general upwards trend in the data.

Window:

Plot showing a time series window based on the FTSE dataset. The window ranges from 1992 to 1993, and is a smaller subset of the entire data, which ranges from 1991 to 1999.

Manipulating Time Series Data in R

Global summary statistics

mean(ftse)
[1] 3565.643
autoplot(ftse) + 
  geom_hline(
    yintercept = mean(ftse)
  ) + 
  ...

Global mean:

Plot of the FTSE time series, but with a blue, dashed horizontal line at a height of roughly 3,500. This blue line represents the global, or overall,mean of the dataset.

Manipulating Time Series Data in R

Rolling window

  • Measure of how statistics change as the data moves in time

Plot of the FTSE time series.

  • Rolling average plotted in red, on top of original data

Plot of the FTSE time series, with a rolling average of the data overlaid with a red line. The rolling average closely follows the original time series, and has a general upwards trend in the data.

Manipulating Time Series Data in R

Rolling with zoo

  • zoo::rollmean()
  • zoo::rollsum()
  • zoo::rollmax()
rollmean(ftse,
         k = 30,
         align = 'right',
         fill = NA)
Manipulating Time Series Data in R

Window arguments

ftse_rm_right <-
  rollmean(ftse,
           k = 7,
           align = "right",
           fill = NA)
  • k: Size of window
  • align: Alignment of window
  • fill: Values to fill-in outside of window
ftse_rm_right
[1]    NA
[2]    NA
[3]    NA
[4]    NA
[5]    NA          
[6]    NA
[7]    2465.971
[8]    2475.229
[9]    2482.414
...
Manipulating Time Series Data in R

NA values

ftse_rm_right
 [1]    NA
 [2]    NA
 [3]    NA
 [4]    NA
 [5]    NA          
 [6]    NA
 [7]    2465.971
 [8]    2475.229
 [9]    2482.414
...
sum(is.na(ftse_rm_right))
[1] 6
Manipulating Time Series Data in R

Window alignment

Alignments:

  • Right-aligned
data_rm <- rollmean(
  data,
  k = 7,
  fill = NA,
  align = "right"
)

Plot of a time series with a right-aligned window overlaid on top. This diagram shows how the window alignment argument determines where the output of the rolling function is "placed" in relation to the original data. In this case, the a right alignment places the output to the right of the window it operates on.

Manipulating Time Series Data in R

Window alignment

Alignments:

  • Left-aligned
data_rm <- rollmean(
  data,
  k = 7,
  fill = NA,
  align = "left"
)

Plot of a time series with a left-aligned window overlaid on top. This diagram shows how the window alignment argument determines where the output of the rolling function is "placed" in relation to the original data. In this case, the a left alignment places the output to the left of the window it operates on.

Manipulating Time Series Data in R

Window alignment

Alignments:

  • Center-aligned
data_rm <- rollmean(
  data,
  k = 7,
  fill = NA,
  align = "center"
)

Plot of a time series with a center-aligned window overlaid on top. This diagram shows how the window alignment argument determines where the output of the rolling function is "placed" in relation to the original data. In this case, the a center alignment places the output to in the middle of the window it operates on.

Manipulating Time Series Data in R

Let's practice!

Manipulating Time Series Data in R

Preparing Video For Download...