Autocorrelation

Author

Eric A. Suess

Published

January 29, 2025

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(fpp3)
Registered S3 method overwritten by 'tsibble':
  method               from 
  as_tibble.grouped_df dplyr
── Attaching packages ──────────────────────────────────────────── fpp3 1.0.0 ──
✔ tsibble     1.1.5     ✔ fable       0.3.4
✔ tsibbledata 0.4.1     ✔ fabletools  0.4.2
✔ feasts      0.3.2     
── 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.

\(w_t \sim N( \mu = 0, \sigma^2 = 1)\)

set.seed(30)

y <- tsibble(
  sample = 1:50, 
  wn = rnorm(50),
  index = sample)
y
y |> autoplot(wn) + labs(title = "White noise")

Moving average model.

\(y_t = w_t + phi * lag(w_t)\)

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
y_ma |> autoplot(wn)

y_ma |> autoplot(yt_ma1)
Warning: Removed 1 row containing missing values or values outside the scale range
(`geom_line()`).

y_ma |> autoplot(yt_ma2)
Warning: Removed 1 row containing missing values or values outside the scale range
(`geom_line()`).

y_ma |> ACF(wn) |> autoplot()

y_ma |> ACF(yt_ma1) |> autoplot()

y_ma |> ACF(yt_ma2) |> autoplot()