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

Target encoding not working with recursive forecasting #223

Open
vidarsumo opened this issue Mar 20, 2023 · 0 comments
Open

Target encoding not working with recursive forecasting #223

vidarsumo opened this issue Mar 20, 2023 · 0 comments

Comments

@vidarsumo
Copy link

vidarsumo commented Mar 20, 2023

Using target encoding in the recipe (step_lencode_mixed or step_lencode_glm) will return an error when calling modeltime_forecast(),

# 1.0.0 Setup ---------------------------------------------------------------------------------

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


data_tbl <- walmart_sales_weekly %>%
    select(id, Dept, Date, Weekly_Sales) %>%
    set_names("id", "dept", "date", "outcome") %>%
    mutate(dept = paste0("dept_", dept))

lag_function <- function(data) {
    data %>%
        group_by(id) %>%
        tk_augment_lags(.value = outcome, .lags = 1:12) %>%
        ungroup()
}

data_lag_tbl <- data_tbl %>%
    lag_function

splits <- data_lag_tbl %>% time_series_split(date_var = date, assess = 12, cumulative = TRUE)

train_data_tbl <- training(splits)
test_data_tbl  <- testing(splits)


# 2.0.0 Modelling -----------------------------------------------------------------------------

# 2.1.0 Recipes ----
parsnip_recipe <- recipes::recipe(outcome ~ ., data = train_data_tbl) %>%
    embed::step_lencode_mixed(recipes::all_nominal_predictors(), outcome = dplyr::vars(outcome)) %>%
    step_timeseries_signature(date) %>%
    update_role(id, new_role = "indicator") %>%
    update_role(date, new_role = "date-field") %>%
    step_zv(all_predictors()) %>%
    step_rm(matches("(.xts$)|(.iso$)|(.lbl$)|(_wday)|(_day)|(hour)|(minute)|(second)|(am.pm)"))


# 2.2.0 Fit model ----
xgb_spec <- boost_tree(mode = "regression") %>%
    set_engine("xgboost")

# 2.2.1 Non-recusrive ----
xgb_mtbl <- workflow() %>%
    add_recipe(parsnip_recipe) %>%
    add_model(xgb_spec) %>%
    fit(train_data_tbl) %>%
    modeltime_table()


# 2.2.2 Recusrive ----
xgb_recusrive_mtbl <- workflow() %>%
    add_recipe(parsnip_recipe) %>%
    add_model(xgb_spec) %>%
    fit(train_data_tbl) %>%
    recursive(
        id = "id",
        transform = lag_function,
        train_tail = panel_tail(train_data_tbl, "id", 12)
    ) %>%
    modeltime_table()


# 2.3.0 Forecast ----

# 2.3.1 Works ----

xgb_fc_tbl <- xgb_mtbl %>%
    modeltime_forecast(
        new_data = test_data_tbl,
        actual_data = data_lag_tbl,
        keep_data = TRUE
    )


# 2.3.2 Fails ----

xgb_recursive_fc_tbl <- xgb_recusrive_mtbl %>%
    modeltime_forecast(
        new_data = test_data_tbl,
        actual_data = data_lag_tbl,
        keep_data = TRUE
    )

Error: Can't combine `..1$dept` <character> and `..2$dept` <double>.
Error in `dplyr::filter()`:
ℹ In argument: `.model_desc == "ACTUAL" | .key == "prediction"`.
Caused by error:
! object '.key' not found
Run `rlang::last_trace()` to see where the error occurred.
Warning message:
Unknown or uninitialised column: `.key`. 

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

1 participant