frankiethull / nixtla-r-tutorial

how to use nixtla's neuralforecast (SOTA DL) python package via R.
6 stars 0 forks source link

Problem with futr_exog_list #1

Closed vidarsumo closed 8 months ago

vidarsumo commented 8 months ago

Thank you for this helpful tutorial on how to use neuralforecast in R.

I wanted to try it out with xreg (futr_exog_list) but I'm running into an error.

airpassengers_df <- data.table::fread("00_data/airpassengersDF.csv")

# add external regressor to the dataset
airpassengers_df$month <- month(airpassengers_df$ds)

train_df <- airpassengers_df |> 
  dplyr::filter(ds<='1959-12-31') # 132 train

test_df  <- airpassengers_df |>
  dplyr::filter(ds>'1959-12-31') # 12 test

horizon <- as.integer(length(test_df$ds))
input_size <- as.integer(2*horizon)

# model parameterization  
models <- c(
  NHITS(input_size = input_size,
        h = horizon,
        max_steps = 10,
        futr_exog_list = c("month")
        )
  )

# stage models
nf_set <- neural_model_setup(models = models, frequency = "M")

# fit models   
nf_fit <- neural_model_fit(model_setup = nf_set, df = train_df)

Error in py_call_impl(callable, call_args$unnamed, call_args$named) :
Exception: {'t', 'o', 'h', 'm', 'n'} future exogenous variables not found in input dataset

Any idea how to (if possible) to include external features when using NHITS in R?

frankiethull commented 8 months ago

hi there -- thanks for trying this out and glad to see someone interested!

I did some testing on my end and it's sometimes tedious to know what var type the python lib wants compared to R workflows (at least for me).

I tried out your code and think a small adjustment will make it work. Changing the future exogenous from c() to list() removes the error for me. I also tried to break/test month as different types as I just wanted to see for myself.

horizon <- length(test_df$ds)
input_size <- as.integer(2*horizon)

  levels <- c(70, 95)

# model parameterization  
models <- c(
  NHITS(input_size = input_size,
        h = horizon,
        max_steps = 10,
            # changing c() to list() here works
        futr_exog_list = list("month")
        )
  )

# stage models
nf_set <- neural_model_setup(models = models, frequency = "M")

# fit models   
# fitting with month as numeric or factor worked here once "month" was passed as list()
nf_fit <- neural_model_fit(model_setup = nf_set, df = train_df |> mutate(month = as.factor(month)))  

next, I don't think my lazy wrapping of the underlying reticulate objects will work for the predictions. Since you have to pass the future xregs as futr_df, I did not account for that in the nixtla_fcns.R script that gets sourced.

The code ran for me like so:

 # make predictions

# note, the fcns for the readme tutorial masks some underlying reticulate stuff
# futr_df needs passed to $predict
# underlying forecasting fcn did not include future exogenous vars
# neural_model_predict <- function(model_setup = neural_model_setup, model_fit = neural_model_fit){
#   model_setup$predict(model_fit)
# }
# nf_preds <- neural_model_predict(model_setup = nf_set, model_fit = nf_fit)

# underlying reticulate method:
nf_preds <- nf_set$predict(nf_fit, futr_df = test_df |> mutate(month = as.factor(month)))

let me know how that works & glad to help out!

vidarsumo commented 8 months ago

This worked but I'm having trouble with multiple unique_id.

Will create a another example and post here.

Thank you so much for the help.