From 818f6dbe1b6c8eaa408e32381d0f82d50de61ab4 Mon Sep 17 00:00:00 2001 From: MMenchero Date: Thu, 17 Oct 2024 00:29:36 -0600 Subject: [PATCH 1/2] feat: infer business-day frequency --- R/infer_frequency.R | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/R/infer_frequency.R b/R/infer_frequency.R index 1372dc5..716dbe3 100644 --- a/R/infer_frequency.R +++ b/R/infer_frequency.R @@ -35,7 +35,13 @@ infer_frequency <- function(df, freq){ } dates_diff <- diff(dates) dates_table <- table(dates_diff) - mode <- as.numeric(names(which.max(dates_table))) + sorted_dates <- sort(dates_table, decreasing = TRUE) + mode <- as.numeric(names(sorted_dates[1])) + if(length(sorted_dates) > 1){ + mode2 <- as.numeric(names(sorted_dates[2])) # second most frequent value + }else{ + mode2 <- NA + } freq_list = list( list(alias = "Y", value = c(365,366)), @@ -52,6 +58,12 @@ infer_frequency <- function(df, freq){ } } + if(!is.na(mode2) && freq == "D") { # check if daily is business-day + if(mode2 == 3) { + freq <- "B" + } + } + message(paste0("Frequency chosen: ", freq)) return(freq) From 4d2f1834611d747bad9c5b4b984448d20ed3da95 Mon Sep 17 00:00:00 2001 From: MMenchero Date: Thu, 17 Oct 2024 21:41:05 -0600 Subject: [PATCH 2/2] docs: remove message in vignette saying business-day frequencies can't be inferred --- vignettes/special-topics.Rmd | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/vignettes/special-topics.Rmd b/vignettes/special-topics.Rmd index f3bfedc..137c009 100644 --- a/vignettes/special-topics.Rmd +++ b/vignettes/special-topics.Rmd @@ -79,7 +79,7 @@ The frequency parameter is crucial when working with time series data because it | Second-level | s | | Business day | B | -In this table, QS and MS stand for quarter and month start, while QE and ME stand for quarter and month end. Hourly and subhourly frequencies can be preceded by an integer, such as "6h", "10min" or "30s". **Only the aliases "min" and "S" are allowed for minute and second-level frequencies**. +In this table, QS and MS stand for quarter and month start, while QE and ME stand for quarter and month end. Hourly and subhourly frequencies can be preceded by an integer, such as "6h", "10min" or "30s". **Only the aliases "min" and "s" are allowed for minute and second-level frequencies**. The default value of the frequency parameter is `NULL`. When this parameter is not specified, `nixtlar` will attempt to determine the frequency of your data. @@ -90,8 +90,6 @@ fcst <- nixtlar::nixtla_client_forecast(df, h = 8, level = c(80,95)) # freq = "h # infer the frequency when `freq` is not specified ``` -**Currently, `nixtlar` can't infer business day frequency, so you must set it directly using `freq="B"`.** - ```{r, include=FALSE} options(original_options) end_vignette()