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

Make function ts_model_spec_tuner #104

Closed
spsanderson opened this issue Oct 26, 2021 · 0 comments · Fixed by #106
Closed

Make function ts_model_spec_tuner #104

spsanderson opened this issue Oct 26, 2021 · 0 comments · Fixed by #106
Assignees
Labels

Comments

@spsanderson
Copy link
Owner

ts_model_spec_tuner <- function(.parsnip_engine = NULL){
  
  # * Tidyeval ----
  pe <- base::as.character(.parsnip_engine)
  
  # * Checks ----
  if(!pe %in% c("auto_arima","auto_arima_xgboost",
    "ets","croston","theta",
    "stlm_ets","tbats","stlm_arima",
    "nnetar",
    "prophet","prophet_xgboost",
    "lm","glmnet","stan","spark","keras",
    "earth","xgboost")){
    stop(call. = FALSE, base::paste0("The parameter (.parsnip_engine) value of: ", pe, ", is not supported."))
  }
  
  if(pe %in% c("auto_arima","stlm_ets","tbats","stlm_arima")){
    stop(call. = FALSE, base::paste0("The parameter (.parsnip_engine) value of: ", pe, ", is an auto tuned model spec already."))
  }
  
  if(pe %in% c("lm")){
    stop(call. = FALSE, base::paste0("The parameter (.parsnip_engine) value of: ", pe, ", has no tuning parameters."))
  }
  
  # * Model Spec Tuner ----
  if (pe == "auto_arima_xgboost"){
    mst <- modeltime::arima_boost(
      trees            = tune::tune()
      , min_n          = tune::tune()
      , tree_depth     = tune::tune()
      , learn_rate     = tune::tune()
      , loss_reduction = tune::tune()
      , sample_size    = tune::tune()
      , stop_iter      = tune::tune()
    ) %>%
      parsnip::set_engine(pe)
  } else if (pe == "ets"){
    mst <- modeltime::exp_smoothing(
      seasonal_period = "auto"
      , error         = "auto"
      , trend         = "auto"
      , season        = "auto"
      , damping       = "auto"
      , smooth_level    = tune::tune()
      , smooth_trend    = tune::tune()
      , smooth_seasonal = tune::tune()
    ) %>%
      parsnip::set_engine(pe)
  } else if (pe == "croston"){
    mst <- modeltime::exp_smoothing(
      seasonal_period = "auto"
      , error         = "auto"
      , trend         = "auto"
      , season        = "auto"
      , damping       = "auto"
      , smooth_level    = tune::tune()
      , smooth_trend    = tune::tune()
      , smooth_seasonal = tune::tune()
    ) %>%
      parsnip::set_engine(pe)
  } else if (pe == "theta"){
    mst <- modeltime::exp_smoothing(
      seasonal_period = "auto"
      , error         = "auto"
      , trend         = "auto"
      , season        = "auto"
      , damping       = "auto"
      , smooth_level    = tune::tune()
      , smooth_trend    = tune::tune()
      , smooth_seasonal = tune::tune()
    ) %>%
      parsnip::set_engine(pe)
  } else if (pe == "nnetar"){
    mst <- modeltime::nnetar_reg(
      seasonal_period   = "auto"
      , non_seasonal_ar = tune::tune()
      , seasonal_ar     = tune::tune()
      , hidden_units    = tune::tune()
      , num_networks    = tune::tune()
      , penalty         = tune::tune()
      , epochs          = tune::tune()
    ) %>%
      parsnip::set_engine(pe)
  } else if (pe == "prophet"){
    mst <- modeltime::prophet_reg(
      changepoint_num      = tune::tune()
      , changepoint_range  = tune::tune()
      , seasonality_yearly = "auto"
      , seasonality_weekly = "auto"
      , seasonality_daily  = "auto"
      , prior_scale_changepoints = tune::tune()
      , prior_scale_seasonality  = tune::tune()
      , prior_scale_holidays     = tune::tune()
    ) %>%
      parsnip::set_engine(pe)
  } else if (pe == "prophet_xgboost"){
    mst <- modeltime::prophet_boost(
      changepoint_num      = tune::tune()
      , changepoint_range  = tune::tune()
      , seasonality_yearly = FALSE
      , seasonality_weekly = FALSE
      , seasonality_daily  = FALSE
      , prior_scale_changepoints = tune::tune()
      , prior_scale_seasonality  = tune::tune()
      , prior_scale_holidays     = tune::tune()
      , trees                    = tune::tune()
      , min_n                    = tune::tune()
      , tree_depth               = tune::tune()
      , learn_rate               = tune::tune()
      , loss_reduction           = tune::tune()
      , sample_size              = tune::tune()
      , stop_iter                = tune::tune()
    ) %>%
      parsnip::set_engine(pe)
  } else if (pe == "glmnet") {
    mst <- parsnip::linear_reg(
      penalty   = tune::tune()
      , mixture = tune::tune()
    ) %>%
      parsnip::set_engine(pe)
  } else if (pe == "stan"){
    mst <- parsnip::linear_reg() %>%
      parsnip::set_engine(
        engine   = pe
        , chains = tune::tune()
        , iter   = tune::tune()
        , seed   = 123
      )
  } else if (pe == "spark"){
    mst <- parsnip::linear_reg(
      penalty   = tune::tune()
      , mixture = tune::tune()
    ) %>%
      parsnip::set_engine(pe)
  } else if (pe == "keras"){
    mst <- parsnip::linear_reg(
      penalty   = tune::tune()
      , mixture = tune::tune()
    ) %>%
      parsnip::set_engine(pe)
  } else if (pe == "earth"){
    mst <- parsnip::mars(
      num_terms = tune::tune()
      , prod_degree = tune::tune()
    ) %>%
      parsnip::set_engine(pe)
  } else if (pe == "xgboost"){
    mst <- parsnip::boost_tree(
      mtry             = tune::tune()
      , trees          = tune::tune()
      , min_n          = tune::tune()
      , tree_depth     = tune::tune()
      , learn_rate     = tune::tune()
      , loss_reduction = tune::tune()
      , sample_size    = tune::tune()
    ) %>%
      parsnip::set_engine(pe)
  }
  
  # * Return ----
  return(mst)
  
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant