-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
89 lines (69 loc) · 2.58 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# flap
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/flap)](https://CRAN.R-project.org/package=flap)
[![R-CMD-check](https://github.com/FinYang/flap/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/FinYang/flap/actions/workflows/R-CMD-check.yaml)
[![Licence](https://img.shields.io/badge/licence-GPL--3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0.en.html)
<!-- badges: end -->
The goal of `flap` is to provide the Forecast Linear Augmented Projection method that can reduce forecast error 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.
## Installation
You can install the **stable** version from
[CRAN](https://cran.r-project.org/package=flap).
``` r
install.packages("flap")
```
You can install the **development** version from
[Github](https://github.com/FinYang/flap)
``` r
# install.packages("remotes")
remotes::install_github("FinYang/flap")
```
## Example
This is a basic workflow to flap:
```{r example}
## 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
mdl <- apply(fpp2::visnights, 2, forecast::ets)
fc <- vapply(mdl, function(mdl) forecast::forecast(mdl, h=12)$mean,
FUN.VALUE = numeric(12))
res <- vapply(mdl, residuals,
FUN.VALUE = numeric(nrow(fpp2::visnights)))
# Obtain components and their forecasts and residuals
pca <- stats::prcomp(fpp2::visnights, center = FALSE, scale. = FALSE)
mdl_comp <- apply(pca$x, 2, forecast::ets)
fc_comp <- vapply(mdl_comp, function(mdl) forecast::forecast(mdl, h=12)$mean,
FUN.VALUE = numeric(12))
res_comp <- vapply(mdl_comp, residuals,
FUN.VALUE = numeric(nrow(pca$x)))
Phi <- t(pca$rotation)
# flap!
proj_fc <- flap(fc, fc_comp, Phi, res, res_comp)
proj_fc
# 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")
}
```