business-science / modeltime

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

Internal error in `df_slice()` when using one model and no calibration #63

Closed vidarsumo closed 3 years ago

vidarsumo commented 3 years ago

I have a modeltime table with nine models and I select one of them as the best model.

final_model <- submodels_tbl %>% 
    filter(str_detect(.model_desc, "PROPHET"))

doing so results in an error when creating forecast, only when I do not use modeltime calibrate.

This gives me the error:

final_model %>% 
    # modeltime_calibrate(testing(splits)) %>% 
    modeltime_forecast(
        new_data = testing(splits),
        actual_data = vr_use_tbl
    ) %>% 
    plot_modeltime_forecast()

Error: Internal error in df_slice(): Columns must match the data frame size.

But this works:

final_model %>% 
    modeltime_calibrate(testing(splits)) %>% 
    modeltime_forecast(
        new_data = testing(splits),
        actual_data = vr_use_tbl
    ) %>% 
    plot_modeltime_forecast()

If I do not filter on the modeltime table, everything works fine without calibration. I.e., this works:

submodels_tbl %>% 
    # modeltime_calibrate(testing(splits)) %>% 
    modeltime_forecast(
        new_data = testing(splits),
        actual_data = vr_use_tbl
    ) %>% 
    plot_modeltime_forecast()
mdancho84 commented 3 years ago

Hey Vidar,

I'm unfortunately not able to reproduce any errors. Here is the code I'm using and it seems to work fine. If you are still experiencing an issue, then please provide a reproducible example and I'd be happy to take a look.

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

m750_splits
#> <Analysis/Assess/Total>
#> <282/24/306>

# Model 1: auto_arima ----
model_fit_arima <- arima_reg() %>%
    set_engine(engine = "auto_arima") %>%
    fit(value ~ date, data = training(m750_splits))
#> frequency = 12 observations per 1 year

# Model 2: prophet ----
model_fit_prophet <- prophet_reg() %>%
    set_engine(engine = "prophet") %>%
    fit(value ~ date, data = training(m750_splits))
#> Disabling weekly seasonality. Run prophet with weekly.seasonality=TRUE to override this.
#> Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.

# Submodels

submodels_tbl <- modeltime_table(
    model_fit_arima,
    model_fit_prophet
) 

submodels_tbl %>%
    modeltime_calibrate(testing(m750_splits)) %>%
    modeltime_forecast(
        new_data    = testing(m750_splits),
        actual_data = m750
    ) %>%
    plot_modeltime_forecast(.interactive = FALSE)
#> Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning -
#> Inf

# Final Model

final_model <- submodels_tbl %>%
    filter(str_detect(.model_desc, "PROPHET"))

final_model %>%
    modeltime_calibrate(testing(m750_splits)) %>%
    modeltime_forecast(
        new_data    = testing(m750_splits),
        actual_data = m750
    ) %>%
    plot_modeltime_forecast(.interactive = FALSE)
#> Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning -
#> Inf

Created on 2021-05-27 by the reprex package (v2.0.0)