How to turn VAR graph forecast in fpp3 R package?

104 views Asked by At

Hi have the following dataset:

library(tidyverse)
library(dplyr)
library(tidymodels)
library(fpp3)
library(tsibble)
library(timetk)

chess.data <- read.csv("https://raw.githubusercontent.com/rhozon/datasets/master/fidedfv3.csv", head = TRUE, sep = ",") %>%
  mutate(
    Period = as.Date(Period),
    Name = as.factor(Name)
  ) %>%
  select(
    Name,
    Period,
    RTNG
  ) %>%
  set_names(
    c("id", "date", "value")
  ) %>%
  as_tibble() %>%
  glimpse()

Rows: 8,067
Columns: 3
$ id    <fct> carlsen, carlsen, carlsen, carlsen, carlsen, carlsen, carlsen, carlsen, carlsen, carlsen, carlsen, carlsen, carlsen, carlsen, carlsen, carls…
$ date  <date> 2001-04-01, 2001-07-01, 2001-10-01, 2002-04-01, 2002-01-01, 2002-07-01, 2002-10-01, 2003-04-01, 2003-01-01, 2003-07-01, 2003-10-01, 2004-04…
$ value <int> 2064, 2084, 2072, 2163, 2127, 2214, 2250, 2356, 2279, 2385, 2450, 2552, 2484, 2567, 2581, 2548, 2553, 2528, 2570, 2646, 2625, 2675, 2698, 26…

Then I transform in a tsibble to set the chess players in columns to get:

chess.data.columns <- chess.data %>%
  pivot_wider(
    names_from = id, values_from = value
  ) %>% 
  mutate(
    date = yearmonth(date)
  ) %>%
  drop_na() %>%
  as_tsibble(index = date ) %>%
  glimpse() 

Rows: 108
Columns: 51
$ date         <mth> 2013 nov, 2013 dez, 2014 jan, 2014 fev, 2014 mar, 2014 abr, 2014 mai, 2014 jun, 2014 jul, 2014 ago, 2014 set, 2014 out, 2014 nov, 201…
$ carlsen      <int> 2870, 2872, 2872, 2872, 2881, 2881, 2882, 2881, 2877, 2877, 2870, 2863, 2863, 2862, 2862, 2865, 2863, 2863, 2876, 2876, 2853, 2853, 2…
$ ding         <int> 2711, 2710, 2717, 2717, 2717, 2710, 2714, 2714, 2726, 2742, 2754, 2730, 2730, 2732, 2732, 2755, 2755, 2751, 2757, 2749, 2749, 2770, 2…
$ nepo         <int> 2721, 2721, 2732, 2732, 2732, 2732, 2735, 2730, 2730, 2714, 2710, 2714, 2714, 2714, 2714, 2714, 2714, 2716, 2728, 2720, 2709, 2705, 2…
$ firouja      <int> 1946, 1946, 1954, 1954, 1954, 2045, 2079, 2079, 2079, 2107, 2188, 2238, 2332, 2332, 2291, 2305, 2312, 2278, 2277, 2277, 2277, 2309, 2…

Then I used the following commands to estimate an VAR model

VAR.MR3 <- chess.data.columns %>%
  model(
    VAR = VAR(vars(
bu,
liem,
cheparinov
    ), ic = "bic")
  ) %>%
  forecast(h = "21 months")

VAR.MR3 %>%
  filter_index("2021-01-01"~.) %>%
  autoplot( chess.data.columns, level = c(95), facets = TRUE ) + ylab("")

But the plot didn´t respect the facets = TRUE argument inside autoplot formula. How did can I solve this ?

1

There are 1 answers

2
Rob Hyndman On BEST ANSWER

First, autoplot() does not have a facets argument, but in this case it will produce a faceted plot. So I'm not sure what you are expecting here.

Second, you are loading many packages that you don't need. Only load packages you actually use, or you run the risk of clashes. fpp3 will load the tsibble and fable packages, which you do use. But timetk and modeltime provide a different approach to tidy time series modelling, and you are not using them.

Here is what I get with your code. Apart from the x-axis being a mess, it seems to work ok. How is it different from what you want?

library(tidyverse)
library(fpp3)

chess.data <- read.csv("https://raw.githubusercontent.com/rhozon/datasets/master/fidedfv3.csv", head = TRUE, sep = ",") %>%
  mutate(
    Period = as.Date(Period),
    Name = as.factor(Name)
  ) %>%
  select(
    Name,
    Period,
    RTNG
  ) %>%
  set_names(
    c("id", "date", "value")
  ) %>%
  as_tibble()


chess.data.columns <- chess.data %>%
  pivot_wider(
    names_from = id, values_from = value
  ) %>% 
  mutate(
    date = yearmonth(date)
  ) %>%
  drop_na() %>%
  as_tsibble(index = date )

VAR.MR3 <- chess.data.columns %>%
  model(
    VAR = VAR(vars(
      bu,
      liem,
      cheparinov
    ), ic = "bic")
  ) %>%
  forecast(h = "21 months")
#> Warning in FUN(X[[i]], ...): NaNs produced

#> Warning in FUN(X[[i]], ...): NaNs produced

#> Warning in FUN(X[[i]], ...): NaNs produced

VAR.MR3 %>%
  filter_index("2021-01-01"~.) %>%
  autoplot(chess.data.columns, level = c(95)) + ylab("")

Created on 2022-11-07 with reprex v2.0.2