1

I am a new user of the packages tidymodels and modeltime. I find these packages really useful for having the same interface of different models from different packages. I try to fine tune a time series with an exponential smoothing from the package forecast::ets. Here is a reproducible example:

library(tidymodels)
library(modeltime)
library(tidyverse)
library(lubridate)

myTS <- c(600, 500, 225, 500, 50, 1025, 1000, 950, 6975, 2400, 2400, 1250, 3500, 3125, 900, 2900, 600, 2150, 900, 1800,  750, 1650,  925,  900, 1075, 1800, 4575, 3725, 1100, 450
          , 175, 1275, 1025, 1025, 1575, 1500, 4525, 2000, 1350, 300,  425,  475,   25,  575,  225, 1000,  325,  225,  275,  325,  200,  450,  350, 1175,  250,  400,  650, 2150, 1075,  425)

myDate <- seq(as_date("2016-06-01"), as_date("2021-05-01"), "months")
TabTS <- bind_cols(YearMonth=myDate, Sum_Qty_kg = myTS)
  
# Split Data 80/20
splits <- initial_time_split(TabTS, prop = 0.8)
data_train <- training(splits)
data_test <- testing(splits)

### Exponential Smoothing Holt-Winters ###

tune_ets <- exp_smoothing(
  error = "auto"
  , trend = "auto"
  , season = "auto"
  , smooth_level = tune()
  , smooth_trend = tune()
  , smooth_seasonal = tune()
) %>%
  set_engine("ets")

grid_ets <- grid_regular(
  parameters(
    list(
      smooth_level = smooth_level(range = c(0.1, 0.8))
      , smooth_trend = smooth_trend(range = c(0.1, 0.8))
      , smooth_seasonal = smooth_seasonal(range = c(0.1, 0.8))
      # , error = error()
      # , trend = trend(values = c("additive", "multiplicative"))
      # , season = season(values = c("additive", "multiplicative"))
    )
  )
)

# Apply the functions to the inputs and models ----------------------------

ForecastModel_FineTune <- function(tune_model, grid_model, IsBoost = "N"){
  
  ### Step 2: Select parameters via Fine tuning ###
  
  # model_recipe <- recipe(Sum_Qty_kg ~ YearMonth , data = data_train) 
  if(IsBoost == "Y") model_recipe <- recipe(Sum_Qty_kg ~ YearMonth , data = data_train) %>% step_date(YearMonth, features = "month", ordinal = FALSE) %>% step_mutate(date_num = as.numeric(YearMonth)) else model_recipe <- recipe(Sum_Qty_kg ~ YearMonth, data = data_train)
  
  model_workflow <- workflow() %>%
    add_model(tune_model) %>%
    add_recipe(model_recipe)
  
  resampling_strategy <- data_train %>%
    time_series_cv(date_var = YearMonth
                   , initial = floor(0.8*nrow(data_train))
                   , assess = nrow(data_train) - floor(0.8*nrow(data_train))
                   # , skip = "3 months"
                   # , slice_limit  = 4
    )
  
  model_tune_results <- model_workflow %>%
    tune_grid(resamples = resampling_strategy
              , grid = grid_model
              , metrics = metric_set(rmse, mae)
    )
  
  best_model <- model_tune_results %>%
    select_best("rmse")
  
  ### Step 3: Finalizing the model ###
  
  model_final <- model_workflow %>%
    finalize_workflow(best_model) %>%
    fit(data_train) %>%
    modeltime_table()
  
  ### Step 4: Calibrate the model to a testing set
  
  calibration_model <- model_final %>%
    modeltime_calibrate(new_data = data_test)    
  
  ### Step 5: Testing Set Forecast & Accuracy Evaluation ###
  
  forecast_model <- calibration_model %>%
    modeltime_forecast(
      new_data    = data_test,
      actual_data = TabTS
    )
  return(list(model_tune_results = model_tune_results, forecast_model = forecast_model))
  
}

forecast_ets <- ForecastModel_FineTune(tune_ets, grid_ets)

When I run this example, I get the following error:

frequency = 9.5 observations per 1 year
frequency = 9.5 observations per 1 year
frequency = 9.5 observations per 1 year
frequency = 9.5 observations per 1 year
x Slice1: preprocessor 1/1, model 4/27: Error in forecast::ets(outcome, model = model_ets, damped = damping_ets, : No model able to be fitted
frequency = 9.5 observations per 1 year
frequency = 9.5 observations per 1 year
frequency = 9.5 observations per 1 year
x Slice1: preprocessor 1/1, model 7/27: Error in forecast::ets(outcome, model = model_ets, damped = damping_ets, : No model able to be fitted
frequency = 9.5 observations per 1 year
x Slice1: preprocessor 1/1, model 8/27: Error in forecast::ets(outcome, model = model_ets, damped = damping_ets, : No model able to be fitted
frequency = 9.5 observations per 1 year
frequency = 9.5 observations per 1 year
frequency = 9.5 observations per 1 year
frequency = 9.5 observations per 1 year
x Slice1: preprocessor 1/1, model 12/27: Error in forecast::ets(outcome, model = model_ets, damped = damping_ets, : No model able to be fitted
frequency = 9.5 observations per 1 year
x Slice1: preprocessor 1/1, model 13/27: Error in forecast::ets(outcome, model = model_ets, damped = damping_ets, : No model able to be fitted
frequency = 9.5 observations per 1 year
frequency = 9.5 observations per 1 year
x Slice1: preprocessor 1/1, model 15/27: Error in forecast::ets(outcome, model = model_ets, damped = damping_ets, : No model able to be fitted
frequency = 9.5 observations per 1 year
x Slice1: preprocessor 1/1, model 16/27: Error in forecast::ets(outcome, model = model_ets, damped = damping_ets, : No model able to be fitted
frequency = 9.5 observations per 1 year
x Slice1: preprocessor 1/1, model 17/27: Error in forecast::ets(outcome, model = model_ets, damped = damping_ets, : No model able to be fitted
frequency = 9.5 observations per 1 year
x Slice1: preprocessor 1/1, model 18/27: Error in forecast::ets(outcome, model = model_ets, damped = damping_ets, : No model able to be fitted
frequency = 9.5 observations per 1 year
frequency = 9.5 observations per 1 year
x Slice1: preprocessor 1/1, model 20/27: Error in forecast::ets(outcome, model = model_ets, damped = damping_ets, : No model able to be fitted
frequency = 9.5 observations per 1 year
x Slice1: preprocessor 1/1, model 21/27: Error in forecast::ets(outcome, model = model_ets, damped = damping_ets, : No model able to be fitted
frequency = 9.5 observations per 1 year
x Slice1: preprocessor 1/1, model 22/27: Error in forecast::ets(outcome, model = model_ets, damped = damping_ets, : No model able to be fitted
frequency = 9.5 observations per 1 year
x Slice1: preprocessor 1/1, model 23/27: Error in forecast::ets(outcome, model = model_ets, damped = damping_ets, : No model able to be fitted
frequency = 9.5 observations per 1 year
x Slice1: preprocessor 1/1, model 24/27: Error in forecast::ets(outcome, model = model_ets, damped = damping_ets, : No model able to be fitted
frequency = 9.5 observations per 1 year
x Slice1: preprocessor 1/1, model 25/27: Error in forecast::ets(outcome, model = model_ets, damped = damping_ets, : No model able to be fitted
frequency = 9.5 observations per 1 year
x Slice1: preprocessor 1/1, model 26/27: Error in forecast::ets(outcome, model = model_ets, damped = damping_ets, : No model able to be fitted
frequency = 9.5 observations per 1 year
x Slice1: preprocessor 1/1, model 27/27: Error in forecast::ets(outcome, model = model_ets, damped = damping_ets, : No model able to be fitted
frequency = 12 observations per 1 year
Warning message:
In `[.tbl_df`(x, is.finite(x <- as.numeric(x))) :
  NAs introduced by coercion

It seems that for some combinations of different parameters values, no model is able to be fitted. How does this problem come from? I tried with another time series and the problem is still there. How can I avoid it?

Quynh-Mai Chu
  • 165
  • 1
  • 9
  • I recommend posting this as an issue on the [modeltime repo](https://github.com/business-science/modeltime/issues). – Julia Silge Jun 18 '21 at 04:37
  • Hello Julia, thanks for answering me. I don't really think it's linked to the modeltime package but the package forecast with the function ets. I will post my question on forecast repo. – Quynh-Mai Chu Jun 18 '21 at 07:47
  • You get this answered, I’m having the issue as well, I would think a model would just create regardless but maybe there are checks like Arima – MCP_infiltrator Apr 19 '22 at 02:26

0 Answers0