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", diff --git a/R/prep_data.R b/R/prep_data.R index 14419b93..847492b9 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..6e807588 100644 --- a/R/prep_models.R +++ b/R/prep_models.R @@ -802,6 +802,35 @@ 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, ...) { + dates <- as.Date(dates, origin = "1970-01-01", ...) + 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.} 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()))) +})