The goal of flap
is to provide the Forecast Linear
Augmented Projection method that can reduce forecast variance by
adjusting the forecasts of multivariate time series to be consistent
with the forecasts of linear combinations (components) of the series by
projecting all forecasts onto the space where the linear constraints are
satisfied.
You can install the stable version from CRAN.
install.packages("flap")
You can install the development version from Github
# install.packages("remotes")
::install_github("FinYang/flap") remotes
This is a basic workflow to flap:
## The following pacakges are required to run this example
# install.packages("tidyr")
# install.packages("ggplot2")
# install.packages("forecast")
# install.packages("fpp2")
library(flap)
library(tidyr)
library(ggplot2)
# Obtain the forecast and the residual of the original series
<- apply(fpp2::visnights, 2, forecast::ets)
mdl #> Registered S3 method overwritten by 'quantmod':
#> method from
#> as.zoo.data.frame zoo
<- vapply(mdl, function(mdl) forecast::forecast(mdl, h=12)$mean,
fc FUN.VALUE = numeric(12))
<- vapply(mdl, residuals,
res FUN.VALUE = numeric(nrow(fpp2::visnights)))
# Obtain components and their forecasts and residuals
<- stats::prcomp(fpp2::visnights, center = FALSE, scale. = FALSE)
pca <- apply(pca$x, 2, forecast::ets)
mdl_comp <- vapply(mdl_comp, function(mdl) forecast::forecast(mdl, h=12)$mean,
fc_comp FUN.VALUE = numeric(12))
<- vapply(mdl_comp, residuals,
res_comp FUN.VALUE = numeric(nrow(pca$x)))
<- t(pca$rotation)
Phi
# flap!
<- flap(fc, fc_comp, Phi, res, res_comp)
proj_fc
proj_fc#> Forecast Linear Augmented Projection
#> A named list of numeric matrices of projected forecasts
#> ------------
#> Num. of Series: m = 20
#> Num. of Components: p = 1-20
#> Num. of Forecast Horizons: 12
#> ------------
#> List of 20
#> $ 1 : num [1:12, 1:20] 7.8 7.91 ...
#> $ 2 : num [1:12, 1:20] 7.64 7.76 ...
#> $ 3 : num [1:12, 1:20] 7.64 7.78 ...
#> $ 4 : num [1:12, 1:20] 7.39 7.48 ...
#> $ 5 : num [1:12, 1:20] 7.39 7.49 ...
#> [list output truncated]
# Plot
if(interactive()) {
%>%
proj_fc as.data.frame() %>%
pivot_longer(!c(h, p)) %>%
ggplot(aes(x = h, y = value, colour = p, group = p)) +
geom_line() +
facet_wrap("name", scales = "free_y")
}