fable - looping through columns for ARIMA

Hi,

I am familiar with the forecast package and am making the transition over to fable and tidyverse.

Example data, at daily frequency, (with more rows) in a rawdata.csv file is as follows:

Index,Var1,Var2,Var3,nIndex,Var_weekday
2022-01-04,5.764,10.0179,0.9133,1,1
2022-01-05,5.7652,10.0185,0.9185,2,2
2022-01-06,5.7637,10.0186,0.9124,3,3
2022-01-07,5.7618,10.0162,0.911,4,4
2022-01-08,5.762,10.0169,0.9154,5,5
2022-01-11,5.765,10.0170,0.9154,6,1
2022-01-13,5.766,10.0173,0.9141,7,3

I am attempting to loop through the columns in a tsibble, and want to estimate 3 separate ARIMA models, each for Var1, Var2, Var3, only, in a for loop, but having difficulties in choosing the column and column name for ARIMA.

#===============================
library(fpp3)

data <- readr::read_csv("path to rawdata.csv")

data_tsibble <-
data %>%
mutate(Index = nIndex) %>%
as_tsibble(index = Index, key = NULL, regular = TRUE)

str(data_tsibble)
x <- colnames(data_tsibble)

for (i in c(x[2],x[3],x[4])) {

refit <-
data_tsibble %>%
model(arima110000 = ARIMA( i ~ 1 + pdq(1,1,0),
method = "CSS-ML",
optim.control = list(maxit=1000))) %>%
select(arima110000) %>%
report()

}

Please can you advise on the above. Thanks.

Amarjit


Referred here by Forecasting: Principles and Practice, by Rob J Hyndman and George Athanasopoulos

I think using pivot and group_split to directly transform the data.frame into a three-pieces list is considerable.
i.e.:

list1 <- data %>% mutate(Index = nIndex) %>%
  pivot_longer(
    cols = matches("^(Var[0-9])"),
    names_to = "vars",
    values_to = "vals"
  ) %>% group_split(vars)

and you can easily loop through the list to do the ARIMA model.

refit <- NULL
for (x in list1){
  refit <- bind_cols(
    x %>% as_tsibble(index = Index, key = NULL, regular = TRUE) %>% 
      model(arima110000 = ARIMA(
        vals ~ 1 + pdq(1,1,0),
        method = "CSS-ML",
        optim.control = list(maxit=1000)
      )) %>% select(arima110000) %>% report(),
    refit
  )
}

or use map function from purrr to looping.

list1 %>% map_dfc(
  ~ .x %>% as_tsibble(index = Index, key = NULL, regular = TRUE) %>% 
    model(arima110000 = ARIMA(
      vals ~ 1 + pdq(1,1,0),
      method = "CSS-ML",
      optim.control = list(maxit=1000)
    )) %>% select(arima110000) %>% report()
) -> refit

But most of the time, it is welcome to adopt the nested data feature to manipulate models through certain sets of data:

data %>% mutate(Index = nIndex) %>%
  pivot_longer(
    cols = matches("^(Var[0-9])"),
    names_to = "vars",
    values_to = "vals"
  ) %>% nest(data = -c(vars)) %>% 
  mutate(
    model = data %>% map(
      ~ .x %>% as_tsibble(index = Index, key = NULL, regular = TRUE) %>% 
        model(arima110000 = ARIMA(
          vals ~ 1 + pdq(1,1,0),
          method = "CSS-ML",
          optim.control = list(maxit=1000)
        )) %>% select(arima110000) %>% report())
  )

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.