business-science / modeltime

Modeltime unlocks time series forecast models and machine learning in one framework
https://business-science.github.io/modeltime/
Other
523 stars 80 forks source link

add_formula(): Error: No date or date-time variable provided. Please supply a date or date-time variable as a predictor. #18

Closed mdancho84 closed 3 years ago

mdancho84 commented 4 years ago

The workflow add_formula() interface does not seem to be transferring the date as an encoded feature.

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

# Data
m750 <- m4_monthly %>% filter(id == "M750")

# Split Data 80/20
splits <- initial_time_split(m750, prop = 0.9)

# --- MODELS ---

model_spec_arima <- arima_reg() %>%
    set_engine(engine = "auto_arima") 

# --- WORKFLOW ---
workflow() %>% 
    add_model(model_spec_arima) %>% 
    add_formula(value ~ date) %>%  # <-- Error here
    fit(training(splits))

# > Error: No date or date-time variable provided. Please supply a date or date-time variable as a predictor.
mdancho84 commented 4 years ago

Note that add_recipe() works fine.

workflow() %>% 
    add_model(model_spec_arima) %>% 
    add_recipe(recipe(value ~ date, training(splits))) %>%  # <-- Replace with this
    fit(training(splits))

Output (as expected):

frequency = 12 observations per 1 year
== Workflow [trained] ==========================================================
Preprocessor: Recipe
Model: arima_reg()

-- Preprocessor ----------------------------------------------------------------
0 Recipe Steps

-- Model -----------------------------------------------------------------------
Series: outcome 
ARIMA(0,1,1)(0,1,1)[12] 

Coefficients:
          ma1     sma1
      -0.3405  -0.4781
s.e.   0.0652   0.0628

sigma^2 estimated as 25114:  log likelihood=-1699.55
AIC=3405.1   AICc=3405.19   BIC=3415.8
mdancho84 commented 3 years ago

This is a known issue in workflows

SOLUTION

Use a recipe as a preprocessor instead of a formula. I have a working example discussed here: #48

Steviey commented 2 years ago

+1