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

Allow for custom model #1

Open
Patrikios opened this issue Apr 10, 2024 · 0 comments
Open

Allow for custom model #1

Patrikios opened this issue Apr 10, 2024 · 0 comments

Comments

@Patrikios
Copy link

Firstly, kudos on the package.

I twould be good to add the potential to allow for an adjusted modell to feed into the cv process.
For instance we could feed it with custom holidays from the prophet internal dataset and add regressors as follows:

tune_prophet_custom <- function (df, horizon, units, args.list, regressor_to_use = NULL, ...){
  available.args <- c("changepoint.prior.scale", "seasonality.prior.scale",
                      "holidays.prior.scale", "seasonality.mode", "changepoint.range")
  if (!all(names(args.list) %in% available.args)) {
    stop(paste("parameters that can be tuned are:", paste(paste0("\"",
                                                                 available.args, "\""), collapse = ", ")))
  }
  if (length(args.list) == 0) {
    stop("argument \"args.list\" cannot empty")
  }
  dots <- list(...)
  all_params <- expand.grid(args.list)
  tuning <- lapply(1:nrow(all_params), function(i) {
    #arg <- c(list(df = df), dplyr::slice(all_params, i))
    # I added
    arg <- dplyr::slice(all_params, i)
    arg$yearly.seasonality <- TRUE
    arg$weekly.seasonality <- TRUE
    arg$daily.seasonality <- FALSE

    m <- do.call(prophet, arg)
    m <- add_country_holidays(m, country_name = "DE")

    if (is.character(regressor_to_use) && length(regressor_to_use) != 0L) {
      for(i in regressor_to_use) {
        m <- add_regressor(m, i)
      }
    }
    m <- fit.prophet(m, df)
    #
    cv.arg <- c(list(model = m, horizon = horizon, units = units),
                dots)
    cv <- do.call(prophetuneR::par_cross_validation, cv.arg)
    pm <- dplyr::select(prophet::performance_metrics(cv, rolling_window = 1),
                        -horizon)
    return(pm)
  })
  result <- cbind(all_params, do.call(rbind, tuning))
  return(result)
}

But mybe a more generic way would be suitable.

What do you think?

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