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

Cannot tune ets_model and loss arguments in adam_reg() function. #225

Closed
tavad opened this issue May 8, 2023 · 1 comment
Closed

Cannot tune ets_model and loss arguments in adam_reg() function. #225

tavad opened this issue May 8, 2023 · 1 comment

Comments

@tavad
Copy link

tavad commented May 8, 2023

I am teaching myself how this function works by tuning it. I may be mistaken, but I do not think ets and loss arguments should be present in a model that is based on ARIMA. Again I may be wrong.

I was trying to tune these parameters and it trows errors as seen in the code below. PS. thanks for your contributions!!

library(dplyr)
library(parsnip)
library(rsample)
library(timetk)
library(modeltime)
library(smooth)

Data

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

Split Data 80/20

set.seed(1234)
splits <- initial_time_split(m750, prop = 0.8)

set.seed(456)
time_roll <-
rolling_origin(
data = training(splits),
initial = round(nrow(training(splits)) * 0.75),
assess = 24,
cumulative = TRUE,
skip = TRUE
)

recipe

rec <- recipe(value ~ date, data = training(splits)) %>%
step_log(value)

Model Spec

model_spec <-
modeltime::adam_reg(
ets_model = tune("ets"),
# loss = tune("loss")
) %>%
set_engine('adam')

selecting terms to be tuned

ets_params <-
crossing(
error = c("A", "M"),
trend = c("N", "A", "Ad", "M", "Md"),
seasonality = c("N", "A", "M")
) %>%
mutate(comb = paste0(error, trend, seasonality)) %>%
pull(comb) %>%
sample(2) # randomly selecting 2 ets terms to be tuned

loss_params <-
c("likelihood", "MSE", "MAE", "HAM", "LASSO",
"RIDGE", "MSEh", "TMSE", "GTMSE", "MSCE") %>%
sample(2) # randomly selecting 2 loss terms to be tuned

tuning the model

adam_fit <-
workflow() %>%
add_model(model_spec) %>%
add_recipe(rec) %>%
tune_grid(
resamples = time_roll,
metrics = metric_set(rmse, rsq),
grid = crossing(
ets = loss_params,
)
)

the error

 # Error in `mutate()`:
 #   ℹ In argument: `object = purrr::map(call_info, eval_call_info)`.
 # Caused by error in `purrr::map()`:
 #   ℹ In index: 1.
 # Caused by error in `.f()`:
 #   ! Error when calling ets_model(): Error : 'ets_model' is not an exported object from 'namespace:modeltime'


 # > rlang::last_trace()
 # <error/dplyr:::mutate_error>
 #   Error in `mutate()`:
 #   ℹ In argument: `object = purrr::map(call_info, eval_call_info)`.
 # Caused by error in `purrr::map()`:
 #   ℹ In index: 1.
 # Caused by error in `.f()`:
 #   ! Error when calling ets_model(): Error : 'ets_model' is not an exported object from 'namespace:modeltime'
 # ---
 #   Backtrace:
 #   ▆
 # 1. ├─... %>% ...
 # 2. ├─tune::tune_grid(...)
 # 3. ├─tune:::tune_grid.workflow(...)
 # 4. │ └─tune:::tune_grid_workflow(...)
 # 5. │   └─tune::check_parameters(...)
 # 6. │     ├─hardhat::extract_parameter_set_dials(wflow)
 # 7. │     └─workflows:::extract_parameter_set_dials.workflow(wflow)
 # 8. │       ├─hardhat::extract_parameter_set_dials(model)
 # 9. │       └─parsnip:::extract_parameter_set_dials.model_spec(model)
 # 10. │         └─... %>% ...
 # 11. ├─dplyr::mutate(., object = purrr::map(call_info, eval_call_info))
 # 12. ├─dplyr:::mutate.data.frame(., object = purrr::map(call_info, eval_call_info))
 # 13. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
 # 14. │   ├─base::withCallingHandlers(...)
 # 15. │   └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
 # 16. │     └─mask$eval_all_mutate(quo)
 # 17. │       └─dplyr (local) eval()
 # 18. └─purrr::map(call_info, eval_call_info)
 # 19.   └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
 # 20.     ├─purrr:::with_indexed_errors(...)
 # 21.     │ └─base::withCallingHandlers(...)
 # 22.     ├─purrr:::call_with_cleanup(...)
 # 23.     └─parsnip (local) .f(.x[[i]], ...)
 # 24.       └─base::stop(paste0("Error when calling ", x$fun, "(): ", as.character(res)))
 # 
@mdancho84
Copy link
Contributor

I've fixed this in the development version of modeltime.

Install:

remotes::isntall_github("business-science/modeltime")

Reproducible example:

library(dplyr)
library(parsnip)
library(rsample)
library(timetk)
library(modeltime)
library(smooth)


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

set.seed(1234)
splits <- initial_time_split(m750, prop = 0.8)

set.seed(456)
time_roll <-
    rolling_origin(
        data = training(splits),
        initial = round(nrow(training(splits)) * 0.75),
        assess = 24,
        cumulative = TRUE,
        skip = TRUE
    )

# recipe
rec <- recipe(value ~ date, data = training(splits)) %>%
    step_log(value)

# Model Spec
model_spec <-
    modeltime::adam_reg(
        # ets_model = tune("ets"),
        loss = tune()
    ) %>%
    set_engine('adam')

# selecting terms to be tuned
ets_params <-
    crossing(
        error = c("A", "M"),
        trend = c("N", "A", "Ad", "M", "Md"),
        seasonality = c("N", "A", "M")
    ) %>%
    mutate(comb = paste0(error, trend, seasonality)) %>%
    pull(comb) %>%
    sample(2) # randomly selecting 2 ets terms to be tuned

loss_params <-
    c("likelihood", "MSE", "MAE", "HAM", "LASSO",
      "RIDGE", "MSEh", "TMSE", "GTMSE", "MSCE") %>%
    sample(2) # randomly selecting 2 loss terms to be tuned


grid_1 <- crossing(
    # ets_model = ets_params,
    loss = loss_params
)

# tuning the model
adam_fit <-
    workflow() %>%
    add_model(model_spec) %>%
    add_recipe(rec) %>%
    tune_grid(
        resamples = time_roll,
        metrics = metric_set(rmse, rsq),
        grid = grid_1
    )

Output:

frequency = 12 observations per 1 year
→ A | warning: You have not provided lambda parameter. I will set it to zero.
frequency = 12 observations per 1 yearns   A: x1
→ B | error:   The horizon "h" needs to be specified and be positive in order for the multistep loss to work.
→ C | warning: A correlation computation is required, but `estimate` is constant and has 0 standard deviation, resulting in a divide by
               0 error. `NA` will be returned.
frequency = 12 observations per 1 yearns   A: x1   B: x1   C: x1
frequency = 12 observations per 1 yearns   A: x2   B: x1   C: x1
frequency = 12 observations per 1 yearns   A: x2   B: x2   C: x1
frequency = 12 observations per 1 yearns   A: x3   B: x2   C: x2
frequency = 12 observations per 1 yearns   A: x3   B: x3   C: x2
frequency = 12 observations per 1 yearns   A: x4   B: x3   C: x3
frequency = 12 observations per 1 yearns   A: x4   B: x4   C: x4
frequency = 12 observations per 1 yearns   A: x5   B: x4   C: x4
frequency = 12 observations per 1 yearns   A: x5   B: x5   C: x5
frequency = 12 observations per 1 year
frequency = 12 observations per 1 yearns   A: x6   B: x6   C: x5
frequency = 12 observations per 1 yearns   A: x7   B: x6   C: x6
frequency = 12 observations per 1 yearns   A: x7   B: x7   C: x6
frequency = 12 observations per 1 yearns   A: x8   B: x7   C: x7
frequency = 12 observations per 1 year
frequency = 12 observations per 1 yearns   A: x9   B: x8   C: x8
frequency = 12 observations per 1 yearns   A: x9   B: x9   C: x8
frequency = 12 observations per 1 yearns   A: x10   B: x9   C: x9
frequency = 12 observations per 1 yearns   A: x10   B: x10   C: x10
frequency = 12 observations per 1 yearns   A: x11   B: x10   C: x10
frequency = 12 observations per 1 year
frequency = 12 observations per 1 yearns   A: x12   B: x11   C: x11
frequency = 12 observations per 1 yearns   A: x12   B: x12   C: x11
frequency = 12 observations per 1 yearns   A: x13   B: x12   C: x12
frequency = 12 observations per 1 yearns   A: x13   B: x13   C: x12
frequency = 12 observations per 1 yearns   A: x14   B: x13   C: x13
frequency = 12 observations per 1 yearns   A: x14   B: x14   C: x14
frequency = 12 observations per 1 yearns   A: x15   B: x14   C: x14
frequency = 12 observations per 1 year
frequency = 12 observations per 1 yearns   A: x16   B: x15   C: x15
frequency = 12 observations per 1 yearns   A: x16   B: x16   C: x15
frequency = 12 observations per 1 yearns   A: x17   B: x16   C: x16
frequency = 12 observations per 1 yearns   A: x17   B: x17   C: x16
frequency = 12 observations per 1 yearns   A: x18   B: x17   C: x17
frequency = 12 observations per 1 yearns   A: x18   B: x18   C: x18
frequency = 12 observations per 1 year
There were issues with some computations   A: x19   B: x19   C: x19

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