Autocorrelation

Author

Eric A. Suess

Published

January 25, 2023

library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
✔ ggplot2 3.3.6     ✔ purrr   0.3.4
✔ tibble  3.1.7     ✔ dplyr   1.0.9
✔ tidyr   1.2.0     ✔ stringr 1.4.0
✔ readr   2.1.2     ✔ forcats 0.5.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(fpp3)
── Attaching packages ──────────────────────────────────────────── fpp3 0.4.0 ──
✔ lubridate   1.8.0     ✔ feasts      0.2.2
✔ tsibble     1.1.1     ✔ fable       0.3.1
✔ tsibbledata 0.4.0     
── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
✖ lubridate::date()    masks base::date()
✖ dplyr::filter()      masks stats::filter()
✖ tsibble::intersect() masks base::intersect()
✖ tsibble::interval()  masks lubridate::interval()
✖ dplyr::lag()         masks stats::lag()
✖ tsibble::setdiff()   masks base::setdiff()
✖ tsibble::union()     masks base::union()

White noise

Random normal with mean 0 and variance 1.

set.seed(30)

y <- tsibble(
  sample = 1:50, 
  wn = rnorm(50),
  index = sample)
y
# A tsibble: 50 x 2 [1]
   sample     wn
    <int>  <dbl>
 1      1 -1.29 
 2      2 -0.348
 3      3 -0.522
 4      4  1.27 
 5      5  1.82 
 6      6 -1.51 
 7      7  0.111
 8      8 -0.761
 9      9 -0.670
10     10  0.275
# … with 40 more rows
y %>% autoplot(wn) + labs(title = "White noise")

Moving average model.

yt = wn + phi * lag(wn)

phi1 <- -0.9
phi2 <-  0.9

y_ma <- y %>% mutate(wn_lag = lag(wn),
                     yt_ma1 = wn + phi1*wn_lag,
                     yt_ma2 = wn + phi2*wn_lag)
y_ma
# A tsibble: 50 x 5 [1]
   sample     wn wn_lag  yt_ma1 yt_ma2
    <int>  <dbl>  <dbl>   <dbl>  <dbl>
 1      1 -1.29  NA     NA      NA    
 2      2 -0.348 -1.29   0.812  -1.51 
 3      3 -0.522 -0.348 -0.209  -0.835
 4      4  1.27  -0.522  1.74    0.804
 5      5  1.82   1.27   0.678   2.97 
 6      6 -1.51   1.82  -3.15    0.131
 7      7  0.111 -1.51   1.47   -1.25 
 8      8 -0.761  0.111 -0.860  -0.661
 9      9 -0.670 -0.761  0.0148 -1.35 
10     10  0.275 -0.670  0.877  -0.328
# … with 40 more rows
y_ma %>% autoplot(wn)

y_ma %>% autoplot(yt_ma1)
Warning: Removed 1 row(s) containing missing values (geom_path).

y_ma %>% autoplot(yt_ma2)
Warning: Removed 1 row(s) containing missing values (geom_path).

y_ma %>% ACF(wn) %>% autoplot()

y_ma %>% ACF(yt_ma1) %>% autoplot()

y_ma %>% ACF(yt_ma2) %>% autoplot()