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

Tuning Dimension Smoothers First Version #12

Closed

Conversation

AlbertoAlmuinha
Copy link
Contributor

Hi @mdancho84

In this PR I have added the arguments that we discussed in Slack (up to 5) to be able to tune the dimensions of the smoothers. As you can see in this example, each term has the dimension passed as an argument (in order). I have also created the parameters in the file "dials-gen_additive_mod_params.R" to be able to tune.

I haven't had time to test yet but everything should work for tuning (I think). So you can play around a bit and see what you think.

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

m4_monthly_extended <- m4_monthly %>%
    group_by(id) %>%
    future_frame(.length_out = 24, .bind_data = TRUE) %>%
    mutate(lag_24 = lag(value, 24)) %>%
    ungroup() %>%
    mutate(date_num = as.numeric(date)) %>%
    mutate(date_month = month(date))


m4_monthly_train  <- m4_monthly_extended %>% drop_na

m4_monthly_future <- m4_monthly_extended %>% filter(is.na(value))

splits <- time_series_split(m4_monthly_train, assess = 24, cumulative = TRUE)

model_fit_gam <- gen_additive_mod(mode = "regression", smoother_dim_term1 = 3, 
                                  smoother_dim_term2 = 3, smoother_dim_term3 = 6) %>%
    set_engine("gam", method = "REML") %>%
    fit(value ~ s(date_month, by = id) 
        + s(date_num, by = id) 
        + s(date_num, date_month, by = id)
        + id,
        data = training(splits))

model_fit_gam

calibration_tbl <- modeltime_table(
    model_fit_gam
) %>%
    modeltime_calibrate(testing(splits), quiet = FALSE) 

calibration_tbl %>% modeltime_accuracy()

calibration_tbl %>%
    modeltime_forecast(
        new_data    = testing(splits),
        actual_data = m4_monthly_train,
        keep_data   = TRUE
    ) %>%
    group_by(id) %>%
    plot_modeltime_forecast(.facet_ncol = 2)

calibration_tbl %>%
    modeltime_refit(m4_monthly_train) %>%
    modeltime_forecast(
        new_data    = m4_monthly_future,
        actual_data = m4_monthly_train,
        keep_data   = TRUE 
    ) %>%
    group_by(id) %>%
    plot_modeltime_forecast()

@AlbertoAlmuinha AlbertoAlmuinha closed this by deleting the head repository Jul 23, 2024
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

Successfully merging this pull request may close these issues.

1 participant