-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from Nixtla/v2-version
chore: release v0.6.0
- Loading branch information
Showing
105 changed files
with
151,157 additions
and
316,148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#' Generate output dates for forecast method. | ||
#' This is a private function of 'nixtlar' | ||
#' | ||
#' @param df_info A data frame that is created by the forecast method with the last dates of every unique id. | ||
#' @param freq The frequency of the data, as a period or offset alias. | ||
#' @param h The forecast horizon. | ||
#' | ||
#' @return A data frame with dates for the forecast. | ||
#' @export | ||
#' @keywords internal | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' dates_df <- .generate_output_dates(df_info, freq, h) | ||
#' } | ||
#' | ||
.generate_output_dates <- function(df_info, freq, h) { | ||
new_dates <- lapply(1:nrow(df_info), function(i) { | ||
start_date <- df_info$dates[i] | ||
r_freq <- .r_frequency(freq) | ||
|
||
if(freq == "QE") { | ||
# End of quarter dates are: "YYY-03-31", "YYYY-06-30", "YYYY-09-30" and "YYYY-12-31". | ||
dt <- seq(from = start_date, by = "quarter", length.out = h+1) | ||
month <- lubridate::month(start_date) | ||
if (month %in% c(3, 12)) { | ||
dt <- ifelse(lubridate::month(dt) %in% c(7, 10), dt - lubridate::days(1), dt) | ||
} else { | ||
dt <- ifelse(lubridate::month(dt) %in% c(3, 12), dt + lubridate::days(1), dt) | ||
} | ||
} else if(freq == "ME") { | ||
dt <- seq(from = start_date + lubridate::days(1), by = r_freq, length.out = h+1) - lubridate::days(1) | ||
} else { | ||
dt <- seq(from = start_date, by = r_freq, length.out = h+1) | ||
} | ||
|
||
dt[2:length(dt)] | ||
}) | ||
|
||
dates_df <- data.frame(lapply(new_dates, as.POSIXct)) | ||
|
||
ids <- df_info$unique_id | ||
if(inherits(df_info$unique_id, "numeric") | inherits(df_info$unique_id, "integer")){ | ||
ids <- as.character(df_info$unique_id) | ||
} | ||
names(dates_df) <- ids | ||
|
||
return(dates_df) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#' Retrieve parameters for 'TimeGPT' model | ||
#' This is a private function of 'nixtlar' | ||
#' | ||
#' @param model Model to use, either "timegpt-1" or "timegpt-1-long-horizon". | ||
#' @param freq Frequency of the data. | ||
#' | ||
#' @return A list with the model's input size and horizon | ||
#' @export | ||
#' @keywords internal | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' .get_model_params() | ||
#' } | ||
#' | ||
.get_model_params <- function(model, freq){ | ||
|
||
payload_params <- list( | ||
model = model, | ||
freq = freq | ||
) | ||
|
||
req <- httr2::request("https://api.nixtla.io/model_params") |> | ||
httr2::req_headers( | ||
"accept" = "application/json", | ||
"content-type" = "application/json", | ||
"authorization" = paste("Bearer", .get_api_key()) | ||
) |> | ||
httr2::req_user_agent("nixtlar") |> | ||
httr2::req_body_json(data = payload_params) |> | ||
httr2::req_retry( | ||
max_tries = 6, | ||
is_transient = .transient_errors | ||
) | ||
|
||
resp <- req |> | ||
httr2::req_perform() |> | ||
httr2::resp_body_json() | ||
|
||
model_params <- list( | ||
input_size = resp$detail$input_size, | ||
horizon = resp$detail$horizon | ||
) | ||
|
||
return(model_params) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,70 @@ | ||
#' Infer frequency of a data frame. | ||
#' | ||
#' @param df A data frame with time series data. | ||
#' @param freq The frequency of the data as specified by the user; NULL otherwise. | ||
#' | ||
#' @return The inferred frequency. | ||
#' @export | ||
#' | ||
#' @examples | ||
#' df <- nixtlar::electricity | ||
#' infer_frequency(df) | ||
#' freq <- NULL | ||
#' infer_frequency(df, freq) | ||
#' | ||
infer_frequency <- function(df){ | ||
freq <- NULL | ||
dates <- sort(unique(df$ds)) | ||
|
||
# Check if it's hourly data | ||
nchrs <- lapply(as.character(dates), nchar) | ||
ntable <- sort(table(unlist(nchrs))) | ||
nmode <- ntable[length(ntable)] | ||
nmode <- as.numeric(names(nmode)) | ||
|
||
if(nmode > 10){ | ||
freq <- "H" # We'll assume hourly data | ||
message("Frequency chosen: H") | ||
infer_frequency <- function(df, freq){ | ||
if(!is.null(freq)){ | ||
return(freq) | ||
} | ||
|
||
# If it's not hourly data, check the time differences in days | ||
ddiff <- diff(as.Date(dates)) | ||
table <- sort(table(ddiff)) | ||
mode <- table[length(table)] | ||
mode <- as.numeric(names(mode)) | ||
|
||
freq_list = list( | ||
list(alias = "Y", value = c(365,366)), | ||
list(alias = "Q", value = c(91,92)), | ||
list(alias = "MS", value = c(30,31)), | ||
list(alias = "W", value = c(7)), | ||
list(alias = "D", value = c(1)) | ||
) | ||
|
||
for(i in 1:length(freq_list)){ | ||
if(mode %in% freq_list[i][[1]]$value){ | ||
freq <- freq_list[i][[1]]$alias | ||
num_chars <- nchar(as.character(df$ds[1])) | ||
|
||
if(num_chars <= 10){ | ||
# assumes dates in format YYYY-MM-DD | ||
dates <- lubridate::ymd(sort(unique(df$ds))) | ||
dates_diff <- diff(dates) | ||
dates_table <- table(dates_diff) | ||
mode <- as.numeric(names(which.max(dates_table))) | ||
|
||
freq_list = list( | ||
list(alias = "Y", value = c(365,366)), | ||
list(alias = "Q", value = c(91,92)), | ||
list(alias = "MS", value = c(30,31)), | ||
list(alias = "W", value = c(7)), | ||
list(alias = "D", value = c(1)) | ||
) | ||
|
||
for(item in freq_list){ | ||
if(mode %in% item$value){ | ||
freq <- item$alias | ||
break | ||
} | ||
} | ||
|
||
message(paste0("Frequency chosen: ", freq)) | ||
return(freq) | ||
|
||
}else{ | ||
# assumes dates in format YYYY-MM-DD hh:mm:ss | ||
dates <- lubridate::ymd_hms(sort(unique(df$ds))) | ||
dates_diff <- diff(dates) | ||
dates_table <- table(dates_diff) | ||
mode <- as.numeric(names(which.max(dates_table))) | ||
|
||
units <- attr(dates_diff, "units") | ||
|
||
freq <- switch( | ||
units, | ||
"hours" = ifelse(mode == 1, "h", paste0(mode, "h")), | ||
"mins" = ifelse(mode == 1, "min", paste0(mode, "min")), | ||
"secs" = ifelse(mode == 1, "s", paste0(mode, "s")) | ||
) | ||
|
||
message(paste0("Frequency chosen: ", freq)) | ||
return(freq) | ||
} | ||
|
||
if(is.null(freq)){ | ||
freq <- "D" | ||
message("I'm not sure about the frequency of the data. Will default to daily (D). Please provide it if you know it.") | ||
stop("I can't figure out the frequency of the data. Please specify it with the `freq` parameter") | ||
} | ||
|
||
message(paste0("Frequency chosen: ", freq)) | ||
|
||
return(freq) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.