christophsax / seasonalbook.content

https://christophsax.github.io/seasonalbook.content/
1 stars 0 forks source link

Outlier effects graph #28

Closed andreranza closed 6 months ago

andreranza commented 7 months ago

Closes #22.

Screenshot 2024-01-19 at 15 35 08
jlivsey commented 7 months ago

Some of the directions are not quite right. For example, level shifts go from -1 to 0 indicating the new level of the series is not being adjusted. Similarly, ramps (and quadratic ramps) go down as well to 0. Otherwise, I really like the graphic!

ao <- c(rep(0, 15), 1, rep(0, 15))
ls <- c(rep(1, 15), rep(0, 16))
tl <- c(rep(0, 11), rep(1, 10), rep(0, 10))
tc <- c(rep(0, 10), .75^(0:20))
so <- c(rep(0, 5), 1, rep(0, 6), 1, rep(0, 6), 1, rep(0, 6), 1, rep(0, 4))
rp <- c(rep(1, 10), 1 - (0:10)/10  , rep(0, 10))
qi <- c(rep(1, 5), 1 - (((0:20)/20-1)^2 - 1)^2 , rep(0, 5))
qd <- c(rep(1, 5), ((0:20)/20-1)^2, rep(0, 5))

outlier_df <- data.frame(
  x = rep(0:30, 8),
  y = c(ao, ls, tl, tc, so, rp, qi, qd),
  type = factor(
    rep(c("AO", "LS", "TL", "TC", "RP", "SO", "QI", "QD"), each = 31L),
    levels = c("AO", "LS", "TL", "TC", "SO", "RP", "QI", "QD"),
    labels = c(
      "Additive outlier (AO)",
      "Level shifts (LS)",
      "Temporary level shift (TL)",
      "Temporary change (TC)",
      "Seasonal outlier (SO)",
      "Ramp (RP)",
      "Increasing quadratic ramp (QI)",
      "Decreasing quadratic ramp (QD)"
    )
  )
)

library(ggplot2)
ggplot(outlier_df, aes(x = x, y = y)) +
  geom_line() +
  scale_y_continuous(breaks = c(0, 1)) +
  theme(plot.title = element_text(hjust = 0.5), panel.grid = element_blank()) +
  facet_wrap(vars(type), ncol = 2) +
  labs(x = NULL, y = NULL)

Created on 2024-01-25 with reprex v2.0.2