0

I'm trying to use the ARIMA function from the fable package. I'd like to test, using cross validation, every specification, given by the pdqPDQ data.frame rows, using a multisession plan from the future package. I will then make forecasts and later calculate accuracy measures.

ARIMA function cannot see the pdqPDQ object. I'm aware of the future missing globals issues, and maybe that's the case here (?).

Any ideas for how I could solve this?

library(GetBCBData)
library(lubridate)
library(tsibble)
library(fable)
library(tidyr)
library(future)
library(dplyr)

#============================================================#
#Data ----
#============================================================#

ipca <- gbcbd_get_series(c(433, 4449, 10844, 11428, 27863, 27864), first.date = "01/01/2004")

ipca <- 
  ipca %>%
  mutate(series.name = 
           case_when(id.num == 433 ~ "ipca",
                     id.num == 4449 ~ "administrados",
                     id.num == 10844 ~ "serviços",
                     id.num == 11428 ~ "livres",
                     id.num == 27863 ~ "industriais",
                     id.num == 27864 ~ "alimentos",
                     TRUE ~ series.name))
ipca <- 
  ipca %>%
  select(data = ref.date, valor = value, series.name) %>%
  pivot_wider(names_from = "series.name", values_from = "valor")


ipca_tsb <- 
  ipca %>%
  mutate(data = yearmonth(data)) %>%
  arrange(data) %>%
  as_tsibble()



#============================================================#
#fable and future: Time series cross validation forecast ----
#============================================================#
ipca_fable <-
  ipca_tsb %>%
  stretch_tsibble(.step = 1, .init = 144)

model_list <- list()


pdqPDQ <- expand.grid(p = 0:4, d = 0, q = 0:4, P = 0:2, D = 0:1, Q = 0:2)

plan(multisession)

for (i in 1:nrow(pdqPDQ)) {
  
  print(pdqPDQ[i,])
  
  #constante incluída
  model_list[[i]] <-
    ipca_fable %>%
      model(ARIMA(alimentos ~ 1 + pdq(pdqPDQ[i, 1], pdqPDQ[i, 2], pdqPDQ[i, 3]) + 
                    PDQ(pdqPDQ[i, 4], pdqPDQ[i, 5], pdqPDQ[i, 6]))) %>%
      forecast(h = 18) %>%
      group_by(.id) %>%
      mutate(h = row_number()) %>%
      ungroup() %>%
      #accuracy requer classe fable
      as_fable(response = "alimentos", distribution = alimentos)
}  
Isaiah
  • 2,091
  • 3
  • 19
  • 28
Rodrigo Remedio
  • 640
  • 6
  • 20
  • What is the generated error description?. I cant' see also the use of futures, you're use simply for. – Ric Dec 28 '22 at 18:47

1 Answers1

0

I don't like answer my own question, but it which was too long for a comment. May not be the most elegant solution, but I could solve the error (Error: object 'pdqPDQ' not found) using the listenv package which allowed me supply the pdqPDQ object inside the brackets.

model_list <- listenv()

for (i in 1:nrow(pdqPDQ)) {
  
  #constante incluída
  model_list[[i]] %<-% {
    pdqPDQ;
    ipca_fable %>%
      model(ARIMA(alimentos ~ 1 + pdq(pdqPDQ[i, 1], pdqPDQ[i, 2], pdqPDQ[i, 3]) + 
                    PDQ(pdqPDQ[i, 4], pdqPDQ[i, 5], pdqPDQ[i, 6]), method = "ML")) %>%
      rename_with(~c(".id", paste0(pdqPDQ[i,], collapse = ", "))) %>%
      forecast(h = 18) %>%
      group_by(.id) %>%
      mutate(h = row_number()) %>%
      ungroup() %>%
      #aparentemente, estruturalmente não muda nada, mas accuracy requer classe fable
      as_fable(response = "alimentos", distribution = alimentos)
  }
Rodrigo Remedio
  • 640
  • 6
  • 20
  • There should be no need to add that `pdqPDQ;` line in the first line in the future expression; the future framework picks up `pdqPDQ` as a global variable anyway, because it is found from `pdqPDQ[i, 1]`, `pdqPDQ[i, 2]`, etc. – HenrikB Jan 02 '23 at 21:48