the Forvision project

Cuong Sai

2018-11-29

Package Summary

Step 1: У нас есть TSTS and FTS Schemas

Example1_TSTS

Example1_FTS

Step 2: Для Exploratory analysis tools

afts1 <- createAFTS(tsts = example1_TSTS, fts = example1_FTS, na = FALSE)

plotPRD(afts1)

plotFAN(tsts = example1_TSTS, fts = example1_FTS, id = “Y1”, method = “A”)

plotFAN(tsts = example1_TSTS, fts = example1_FTS, id = “Y1”, method = c(“A”, “B”,…))

Step 3: Performance measurement

MAE, MdAE, MAPE,…

MASE, …

1. Package installation

You can install the development version of the forvision package (containing the latest improvements and bug fixes) from github:

# Если нет devtools то сначала надо его установить
install.packages(devtools)
# И потом установить пакет
devtools::install_github()

2. The data examples:

The TSTS schema:

library(forvision)
# После того как загрузили forvision package, встроенные данные в пакете сразу доступны
# В пакете пока 2 встроенных набора данных: example1_TSTS and example1_FTS
head(example1_TSTS)
#> # A tibble: 6 x 3
#>   series_id value timestamp
#>   <chr>     <dbl>     <int>
#> 1 Y1        3104.      1984
#> 2 Y1        3360.      1985
#> 3 Y1        3808.      1986
#> 4 Y1        4388.      1987
#> 5 Y1        4937.      1988
#> 6 Y1        5380.      1989

The FTS schema:

head(example1_FTS)
#> # A tibble: 6 x 8
#>   series_id method timestamp origin_timestamp forecast horizon    Lo    Hi
#>   <chr>     <chr>      <int>            <int>    <dbl>   <int> <dbl> <dbl>
#> 1 Y1        A           1989             1988    5406.       1 5183. 5630.
#> 2 Y1        A           1990             1988    5876.       2 5653. 6099.
#> 3 Y1        A           1991             1988    6345.       3 6122. 6569.
#> 4 Y1        B           1989             1988    5474.       1 5251. 5697.
#> 5 Y1        B           1990             1988    6010.       2 5787. 6234.
#> 6 Y1        B           1991             1988    6547.       3 6324. 6770.

3. The function createAFTS()

To create AFTS with NA values (by default) (rows in tsts with no match in fts will have NA values in the afts):

library(forvision)
afts1 <- createAFTS(tsts = example1_TSTS, fts = example1_FTS)
head(afts1, 10)
#> # A tibble: 10 x 9
#>    series_id value timestamp method origin_timestamp forecast horizon    Lo
#>    <chr>     <dbl>     <int> <chr>             <int>    <dbl>   <int> <dbl>
#>  1 Y1        3104.      1984 <NA>                 NA      NA       NA   NA 
#>  2 Y1        3360.      1985 <NA>                 NA      NA       NA   NA 
#>  3 Y1        3808.      1986 <NA>                 NA      NA       NA   NA 
#>  4 Y1        4388.      1987 <NA>                 NA      NA       NA   NA 
#>  5 Y1        4937.      1988 <NA>                 NA      NA       NA   NA 
#>  6 Y1        5380.      1989 A                  1988    5406.       1 5183.
#>  7 Y1        5380.      1989 B                  1988    5474.       1 5251.
#>  8 Y1        5380.      1989 C                  1988    5406.       1 5183.
#>  9 Y1        6159.      1990 A                  1988    5876.       2 5653.
#> 10 Y1        6159.      1990 B                  1988    6010.       2 5787.
#> # ... with 1 more variable: Hi <dbl>

To create AFTS without NA vaules using the option na=FALSE:

library(forvision)
afts2 <- createAFTS(tsts = example1_TSTS, fts = example1_FTS, na = FALSE)
head(afts2, 10)
#> # A tibble: 10 x 9
#>    series_id value timestamp method origin_timestamp forecast horizon    Lo
#>    <chr>     <dbl>     <int> <chr>             <int>    <dbl>   <int> <dbl>
#>  1 Y1        5380.      1989 A                  1988    5406.       1 5183.
#>  2 Y1        5380.      1989 B                  1988    5474.       1 5251.
#>  3 Y1        5380.      1989 C                  1988    5406.       1 5183.
#>  4 Y1        6159.      1990 A                  1988    5876.       2 5653.
#>  5 Y1        6159.      1990 B                  1988    6010.       2 5787.
#>  6 Y1        6159.      1990 C                  1988    5876.       2 5653.
#>  7 Y1        6877.      1991 A                  1988    6345.       3 6122.
#>  8 Y1        6877.      1991 B                  1988    6547.       3 6324.
#>  9 Y1        6877.      1991 C                  1988    6345.       3 6122.
#> 10 Y2        4793.      1989 A                  1988    4143.       1 3920.
#> # ... with 1 more variable: Hi <dbl>

4. The function plotPRD()

Эта функция использует схему AFTS без NAs в качестве входа

# Load the forvision package
library(forvision)
# To create AFTS without NA values from TSTS and FTS
df <- createAFTS(tsts = example1_TSTS, fts = example1_FTS, na = FALSE)
# Plot a PRD
plotPRD(df)

using xlim and ylim:

plotPRD(df, xlim = c(3400, 6000), ylim = c(4000, 6000))

using useLog = TRUE

plotPRD(df, useLogs = TRUE)

5. The fucntion plotFS()

Эта функция использует TSTS and FTS schemas в качестве input

Column timestamp обязательно в виде time-based object

library(forvision)
# Преобразование сolumn timestamp в time-based object
example1_TSTS$timestamp <-  zoo::as.yearmon(example1_TSTS$timestamp)
example1_FTS$timestamp <-  zoo::as.yearmon(example1_FTS$timestamp)
# create a graph with one method
plotFS(ts = example1_TSTS, fs = example1_FTS, id ="Y1", meth = "A")
# create a graph with more methods
plotFS(ts = example1_TSTS, fs = example1_FTS, id ="Y1", meth = c("A", "B"))
# Если не указать method то просто строить a plot of time series
plotFS(ts = example1_TSTS, fs = example1_FTS, id ="Y1")

6. The fucntion plotFAN()

Эта функция использует TSTS and FTS schemas в качестве input

Column timestamp обязательно в виде time-based object

library(forvision)
# Преобразование сolumn timestamp в time-based object
example1_TSTS$timestamp <-  zoo::as.yearmon(example1_TSTS$timestamp)
example1_FTS$timestamp <-  zoo::as.yearmon(example1_FTS$timestamp)
# create a fanplot
plotFAN(ts = example1_TSTS, fs = example1_FTS, id ="Y1", meth = "A")
plotFAN(ts = example1_TSTS, fs = example1_FTS, id ="Y4", meth = "B")

Examples for plotFAN() and plotFS() using time series M599 from M3 competition:

# Using time series from M3
library(Mcomp)
#> Loading required package: forecast
# create TSTS
TS <- M3[[2000]]
TSTS <- data.frame(series_id = TS$st, value = c(TS$x, TS$xx), timestamp = c(time(TS$x), time(TS$xx)))
# Create models
library(forecast)
ets.model <- ets(TS$x)
arima.model <- auto.arima(TS$x)
nn.model <- nnetar(TS$x)
tbat.model <- tbats(TS$x)
# Forecast
ets.forecast <- forecast(ets.model,TS$h)
arima.forecast <- forecast(arima.model,TS$h)
nn.forecast <- forecast(arima.model,TS$h)
tbat.forecast <- forecast(tbat.model,TS$h)
# Create FTS
FTS <- rbind(cbind(series_id = TS$st, as.data.frame(ets.forecast)[,1:3], method = "ETS", timestamp = c(time(TS$xx)), horizon = 1:TS$h), 
             cbind(series_id = TS$st, as.data.frame(arima.forecast)[,1:3], method = "ARIMA",timestamp = c(time(TS$xx)), horizon = 1:TS$h),
             cbind(series_id = TS$st, as.data.frame(nn.forecast)[,1:3], method = "NN", timestamp = c(time(TS$xx)), horizon = 1:TS$h))
rownames(FTS) <- NULL
colnames(FTS) <- c("series_id", "forecast", "Lo", "Hi", "method", "timestamp", "horizon")
head(TSTS)
#>   series_id value timestamp
#> 1      M599  2440  1979.000
#> 2      M599  2175  1979.083
#> 3      M599  1800  1979.167
#> 4      M599  1795  1979.250
#> 5      M599  2075  1979.333
#> 6      M599  1985  1979.417
head(FTS)
#>   series_id forecast       Lo       Hi method timestamp horizon
#> 1      M599 5054.931 4202.243 5907.620    ETS  1989.500       1
#> 2      M599 5054.931 3849.108 6260.755    ETS  1989.583       2
#> 3      M599 5054.931 3578.130 6531.733    ETS  1989.667       3
#> 4      M599 5054.931 3349.682 6760.181    ETS  1989.750       4
#> 5      M599 5054.931 3148.414 6961.449    ETS  1989.833       5
#> 6      M599 5054.931 2966.454 7143.409    ETS  1989.917       6
# Преобразование сolumn timestamp в time-based object
TSTS$timestamp <-  zoo::as.yearmon(TSTS$timestamp)
FTS$timestamp <-  zoo::as.yearmon(FTS$timestamp)
# create a fanplot
library(forvision)
plotFAN(ts = TSTS, fs = FTS, id ="M599", meth = "ETS")
plotFAN(ts = TSTS, fs = FTS, id ="M599", meth = "NN")
plotFAN(ts = TSTS, fs = FTS, id ="M599", meth = "ARIMA")
# plotFS
plotFS(ts = TSTS, fs = FTS, id ="M599", meth = c("ARIMA", "ETS"))
# for plotPRD-create AFTS
data <- createAFTS(tsts = TSTS, fts = FTS, na = FALSE)
# Plot a PRD
plotPRD(data)