From cd3b3d24a4212308c10795ad3ef02d678d6ec3db Mon Sep 17 00:00:00 2001 From: laresbernardo Date: Mon, 26 Feb 2024 12:36:46 +0100 Subject: [PATCH 1/3] feat: auto detect date_type when not provided --- DESCRIPTION | 2 +- R/prep_data.R | 2 +- R/prep_models.R | 28 ++++++++++++++++++++++++++++ man/prep_data.Rd | 2 +- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index eaf30d1e..8228925b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: finnts Title: Microsoft Finance Time Series Forecasting Framework -Version: 0.4.0.9000 +Version: 0.4.0.9001 Authors@R: c(person(given = "Mike", family = "Tokic", diff --git a/R/prep_data.R b/R/prep_data.R index 3d3533d0..cf78a7fc 100644 --- a/R/prep_data.R +++ b/R/prep_data.R @@ -8,7 +8,7 @@ #' @param combo_variables List of column headers within input data to be used to separate individual time series. #' @param target_variable The column header formatted as a character value within input data you want to forecast. #' @param date_type The date granularity of the input data. Finn accepts the following as a character string: -#' day, week, month, quarter, year. +#' day, week, month, quarter, year. Default will detect automatically based on the difference of the first two dates. #' @param forecast_horizon Number of periods to forecast into the future. #' @param external_regressors List of column headers within input data to be used as features in multivariate models. #' @param hist_start_date Date value of when your input_data starts. Default of NULL uses earliest date value in diff --git a/R/prep_models.R b/R/prep_models.R index 78e1e349..65f719d7 100644 --- a/R/prep_models.R +++ b/R/prep_models.R @@ -802,6 +802,34 @@ model_hyperparameters <- function(run_info, ) } +#' Detect date type +#' +#' @param dates Dates vector. Will use first two values to calculate difference. +#' +#' @return Returns date_type +#' @noRd +get_date_type <- function(dates) { + dayInterval <- as.integer(difftime( + dates[2], + dates[1], + units = "days" + )) + date_type <- if (dayInterval == 1) { + "day" + } else if (dayInterval == 7) { + "week" + } else if (dayInterval %in% 28:31) { + "month" + } else if (round(dayInterval/4) %in% 28:31) { + "quarter" + } else if (dayInterval %in% 365:366) { + "year" + } else { + stop("Dates data has to be daily, weekly, quarterly, monthly, or yearly") + } + return(date_type) +} + #' Gets the right frequency numbers #' #' @param date_type year, quarter, month, week, day diff --git a/man/prep_data.Rd b/man/prep_data.Rd index c0e0dd71..0646e63a 100644 --- a/man/prep_data.Rd +++ b/man/prep_data.Rd @@ -41,7 +41,7 @@ Can also include external regressors for both historical and future data.} \item{target_variable}{The column header formatted as a character value within input data you want to forecast.} \item{date_type}{The date granularity of the input data. Finn accepts the following as a character string: -day, week, month, quarter, year.} +day, week, month, quarter, year. Default will detect automatically based on the difference of the first two dates.} \item{forecast_horizon}{Number of periods to forecast into the future.} From 58d9e290960522b16645d7d280e7f668f9633145 Mon Sep 17 00:00:00 2001 From: laresbernardo Date: Sat, 16 Mar 2024 10:35:35 +0100 Subject: [PATCH 2/3] tests: include tests + feat: allow integers as dates --- R/prep_models.R | 3 ++- tests/testthat/test-date_type.R | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/test-date_type.R diff --git a/R/prep_models.R b/R/prep_models.R index 65f719d7..6e807588 100644 --- a/R/prep_models.R +++ b/R/prep_models.R @@ -808,7 +808,8 @@ model_hyperparameters <- function(run_info, #' #' @return Returns date_type #' @noRd -get_date_type <- function(dates) { +get_date_type <- function(dates, ...) { + dates <- as.Date(dates, origin = "1970-01-01", ...) dayInterval <- as.integer(difftime( dates[2], dates[1], diff --git a/tests/testthat/test-date_type.R b/tests/testthat/test-date_type.R new file mode 100644 index 00000000..e6b748b3 --- /dev/null +++ b/tests/testthat/test-date_type.R @@ -0,0 +1,10 @@ +# Check get_date_type() with different datasets + +test_that("get_date_type() detects date types correctly", { + # Using m750 dataset + expect_equal("month", get_date_type(modeltime::m750$date)) + # Passing dates as integer values + expect_equal("day", get_date_type((Sys.Date()-1):Sys.Date())) + # Passing dates as Date values + expect_equal("day", get_date_type(as.Date((Sys.Date()-1):Sys.Date()))) +}) From 41383afc45bfe0a9e5207637d71a7eb1d850dd3f Mon Sep 17 00:00:00 2001 From: laresbernardo Date: Sat, 16 Mar 2024 10:37:09 +0100 Subject: [PATCH 3/3] docs: update to version 0.4.0.9003 --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 612fe224..2f4a1e22 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: finnts Title: Microsoft Finance Time Series Forecasting Framework -Version: 0.4.0.9002 +Version: 0.4.0.9003 Authors@R: c(person(given = "Mike", family = "Tokic",