## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 5)

## -----------------------------------------------------------------------------
library(foresightr)

## -----------------------------------------------------------------------------
d <- decompose_stl(log(AirPassengers), seasonal_window = 13)
d
autoplot(d)

## -----------------------------------------------------------------------------
t <- 0:419
weekly <- c(5, 0, -2, -3, 0, 1, -1)
daily <- 100 + 0.05 * t + weekly[t %% 7 + 1] + 8 * sin(2 * pi * t / 30) +
  2 * sin(t * 1.7) * cos(t * 0.3)
d <- decompose_mstl(daily, periods = c(7, 30))
d$seasonal_strength
autoplot(d)

## -----------------------------------------------------------------------------
model <- model_decomposed(model_drift(), periods = c(7, 30))
f <- forecast_model(model, daily, 14, period = 7)
round(f, 1)

## -----------------------------------------------------------------------------
y <- log(AirPassengers)
y[30] <- y[30] + 0.8     # a typing error
y[100] <- y[100] - 0.7   # a strike
y[61:62] <- NA           # months never recorded
find_outliers(y)

## -----------------------------------------------------------------------------
cleaned <- clean_series(y)
round(exp(cbind(dirty = y, clean = cleaned)[c(30, 61, 62, 100), ]))

