Expanding windows

Manipulating Time Series Data in R

Harrison Brown

Graduate Researcher in Geography

Rolling versus expanding windows

Rolling window:

  • Moving start point
  • Window has a fixed width

Expanding window:

  • Fixed start point
  • Window has an increasing width
Manipulating Time Series Data in R

Expanding window process

Increasing window width:

  • Width 1 at observation 1,
  • Width 2 at observation 2,
  • Width 3 at observation 3,
  • ...

What is the summary of the data available to us?

Fixed start point:

  • Windows all start at beginning of time series.

Conceptual diagram of an expanding window of a time series. There are four boxes, labeled one through four, which are spaced together horizontally, representing a conceptual time series. Below the time series, there are four horizontal lines of increasing length. The first line starts at the left end of the image and end at the end of the first box. The second line ends at the second box, and so on.

Manipulating Time Series Data in R

Calculating an expanding window

Expanding windows in R:

  • zoo::rollapply()!
  • width can be single number or vector

Expanding window width:

  • 1, 2, 3, 4, ...

Function for sequence of numbers:

  • base::seq_along()
seq_along(daily_temp)
[1] 1 2 3 4 5 6 7 8 9 ...
    ... 363 364 365
Manipulating Time Series Data in R

Calculating an expanding window

exp_widths <- seq_along(daily_temp)

exp_widths
[1] 1 2 3 4 5 ... 363 364 365
temp_expand <- 
  rollapply(
    data = daily_temp,
    FUN = mean,
    width = exp_widths,
    # Alignment must be 'right'
    align = 'right'
  )
Manipulating Time Series Data in R

Plotting expanding windows

ggplot() + 
# Original data
  geom_line(data = daily_temp,
            aes(x = Index,
            y = daily_temp),
            color = 'grey50') +

# Expanding window plot geom_line(data = temp_expand, aes(x = Index, y = temp_expand), color = 'red') + theme_light() + labs(y = 'Degrees Celsius')

Plot of a time series of daily temperature readings. There is a "noisy" grey line, which shows how temperature readings are highly varied from day to day. Over the grey line is a red line, which is the expanding mean of the dataset.

Manipulating Time Series Data in R

Expanding window inferences

  • Statistics approach global summaries
  • Expanding mean becomes less sensitive to change
  • Earlier observations are more sensitive to change

Plot of a time series of daily temperature readings. There is a "noisy" grey line, which shows how temperature readings are highly varied from day to day. Over the grey line is a red line, which is the expanding mean of the dataset.

Manipulating Time Series Data in R

Let's practice!

Manipulating Time Series Data in R

Preparing Video For Download...