扩展窗口

R 中的时间序列数据处理

Harrison Brown

Graduate Researcher in Geography

滚动 vs 扩展窗口

滚动窗口:

  • 起点移动
  • 固定窗口宽度

扩展窗口:

  • 固定起点
  • 窗口宽度递增
R 中的时间序列数据处理

扩展窗口过程

递增的窗口宽度:

  • 观测1时宽度为1,
  • 观测2时宽度为2,
  • 观测3时宽度为3,

可用数据的汇总是什么?

固定起点:

  • 所有窗口均从时间序列起始处开始。

时间序列扩展窗口的概念图。四个水平排列、标注1至4的方块表示时间序列。下方有四条逐渐变长的水平线:第一条从最左到第一个方块末端,第二条到第二个方块末端,依此类推。

R 中的时间序列数据处理

计算扩展窗口

R 中的扩展窗口:

  • zoo::rollapply()
  • width 可为单个数或向量

扩展窗口宽度:

  • 1234,…

生成数列的函数:

  • base::seq_along()
seq_along(daily_temp)
[1] 1 2 3 4 5 6 7 8 9 ...
    ... 363 364 365
R 中的时间序列数据处理

计算扩展窗口

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'
  )
R 中的时间序列数据处理

绘制扩展窗口

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')

每日气温时间序列图。灰色"噪声"线显示逐日波动较大。其上红线为数据集的扩展均值。

R 中的时间序列数据处理

扩展窗口推断

  • 统计量逼近全局汇总
  • 扩展均值对变化更不敏感
  • 早期观测对变化更敏感

每日气温时间序列图。灰色"噪声"线显示逐日波动较大。其上红线为数据集的扩展均值。

R 中的时间序列数据处理

让我们来练习!

R 中的时间序列数据处理

Preparing Video For Download...