Skip to content

Calling generate for a saved and loaded NNETAR object causes UseMethod() error #437

@drgomulka

Description

@drgomulka

I encountered an error while creating a NNETAR model with one script and then generating from it a sample in a Quarto document.

The error does not happen when in the same second script some other model is fit first, and then a sample is generated from the old saved model. This must be caused by some library being loaded on model fitting, but not on sample generation. The error does not happen when an ETS or ARIMA model is fit.

The following example code replicates the issue with one script file that is run twice. It creates and saves a model, then restarts the session to clear the loaded packages from memory and then tries to generate a sample in the second run. Error message is below:

library(fable)

file_name <- "./a_save.rda" 

if(!file.exists(file_name)) {
  message("generate and save a NNETAR model fit:")
  a_fit  <- as_tsibble(USAccDeaths) |>
    model(NNETAR(value)
          #ETS(value)
          #ARIMA(value)
          ) 
  save(a_fit, file = file_name)
  rstudioapi::restartSession(command = "print('Rerun the script now.')", clean = T)
} else {
  message("try to generate sample from a saved NNETAR model fit:")
  load(file_name)
  a_sample <-  a_fit |>   generate(times = 5)
  print(a_sample)
}

# try to generate sample from a saved NNETAR model fit:
# Error in `as_tibble()`:
#   ℹ In argument: `.sim = sim_nnetar(.innov)`.
# ℹ In group 1: `.rep = "1"`.
# Caused by error in `UseMethod()`:
# ! msgid "no applicable method for 'predict' applied to an object of class "nnet"

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions