Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracting engine fit from workflow #249

Closed
acarpignani opened this issue May 30, 2024 · 1 comment
Closed

Extracting engine fit from workflow #249

acarpignani opened this issue May 30, 2024 · 1 comment

Comments

@acarpignani
Copy link

Hi there!

I was trying to tune a Prophet model and wanted then to check the model. I am just at the beginning of learning how to use modeltime, so I wanted to check the model using prophet. Herewith the code that I have written:

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

# Define the Prophet model specification
prophet_spec <- 
    prophet_reg() |> 
    set_args(
        seasonality_yearly = TRUE,
        seasonality_weekly = TRUE,
        seasonality_daily = FALSE,
        prior_scale_changepoints = tune(),
        prior_scale_seasonality = tune(),
        prior_scale_holidays = tune()    
    ) |> 
    set_engine("prophet")

# Define the grid of tuning parameters
tune_grid <- 
    grid_latin_hypercube(
        prior_scale_changepoints(),
        prior_scale_seasonality(),
        prior_scale_holidays(),
        size = 10  # Number of combinations to try
    )

splits <- initial_time_split(m750)
train <- training(splits)
test <- testing(splits)

# Define the rolling origin cross-validation
folds <- 
    rolling_origin(
        train,
        initial = 100,
        assess = 10,
        skip = 15
    )

# Set up the workflow
workflow <- 
    recipe(value ~ date, data = train) |> 
    workflow(prophet_spec) 

# Performing tuning
tune_results <- 
    tune_grid(
        workflow,
        resamples = folds,
        grid = tune_grid
    )

# Evaluate and select the best model
best_model <- select_best(tune_results, metric = "rmse")

# Finalize the model with the best hyperparameters
final_model <- finalize_workflow(workflow, best_model)

# Fit the finalized workflow on the full dataset
final_fit <- final_model |> 
    fit(data = train)

# Extract the fitted Prophet model
fitted_prophet <- extract_fit_engine(final_fit)

# Create future dataframe
future <- make_future_dataframe(fitted_prophet, periods = 15)

# Make forecasts
forecast <- predict(fitted_prophet, future)

However, when I try to extract the fit from the workflow, using extract_fit_engine() and I use it to create the future with make_future_dataframe(), I get the following error:

> future <- make_future_dataframe(fitted_prophet, periods = 15)
Error in make_future_dataframe(fitted_prophet, periods = 15) : 
  Model must be fit before this can be used.

As far as I have understood the problem, it seems that extract_fit_engine() doesn't extract the fit in the correct format as it should. Is there any chance that this can be fixed?

Thank you and thank you for any help you can give me.

Andrea

@mdancho84
Copy link
Contributor

I believe the issue is you need to extract a fitted model. You are just extracting the engine call.

Here's a reproducible example:

library(dplyr)
library(parsnip)
library(rsample)
library(timetk)

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

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

# ---- PROPHET ----

# Model Spec
model_spec <- prophet_reg() %>%
    set_engine("prophet")

# Fit Spec
model_fit <- model_spec %>%
    fit(log(value) ~ date, data = training(splits))
model_fit

base_prophet_model <- model_fit$fit$models$model_1

prophet::make_future_dataframe(base_prophet_model, periods = 12, freq = "month")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants