Giter VIP home page Giter VIP logo

flap's Introduction

flap

CRAN status R-CMD-check Licence

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.

install.packages("flap")

You can install the development version from Github

# install.packages("remotes")
remotes::install_github("FinYang/flap")

Example

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
mdl <- apply(fpp2::visnights, 2, forecast::ets)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
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
#> 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")
}

flap's People

Contributors

finyang avatar

Stargazers

Allister Hodge avatar

Watchers

Rob J Hyndman avatar George Athanasopoulos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.