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

feat: auto detect date_type when not provided #154

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion R/prep_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions R/prep_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion man/prep_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testthat/test-date_type.R
Original file line number Diff line number Diff line change
@@ -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())))
})