-
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.
WIP: Added better support for tsibbles and improved function to infer…
… frequency.
- Loading branch information
Showing
17 changed files
with
220 additions
and
184 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ Depends: | |
LazyData: true | ||
Imports: | ||
httr2, | ||
lubridate, | ||
tsibble | ||
Suggests: | ||
httptest2, | ||
|
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,13 +1,15 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(find_frequency) | ||
export(prepare_multi_series) | ||
export(prepare_single_series) | ||
export(date_conversion) | ||
export(infer_frequency) | ||
export(prepare_data) | ||
export(set_token) | ||
export(timegpt_forecast) | ||
export(validate_token) | ||
importFrom(httr2,req_headers) | ||
importFrom(httr2,req_perform) | ||
importFrom(httr2,request) | ||
importFrom(httr2,resp_status) | ||
importFrom(lubridate,ymd) | ||
importFrom(lubridate,ymd_hms) | ||
importFrom(tsibble,is_tsibble) |
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,40 @@ | ||
#' Infer frequency of a tsibble and convert its index to date or string. | ||
#' | ||
#' @param df A tsibble. | ||
#' | ||
#' @return A list with the inferred frequency and df with the new index. | ||
#' @export | ||
#' | ||
date_conversion <- function(df){ | ||
cls <- class(df$ds)[1] | ||
|
||
if(cls == "integer"){ | ||
freq <- "Y" | ||
df$ds <- paste0(df$ds, "-01-01") | ||
|
||
}else if(cls %in% c("yearquarter", "yearmonth", "yearweek")){ | ||
freq <- switch(cls, | ||
yearquarter = "Q", | ||
yearmonth = "MS", | ||
yearweek = "W") | ||
df$ds <- as.Date(df$ds) | ||
df$ds <- as.character(df$ds) | ||
|
||
}else if(cls == "Date"){ | ||
freq <- "D" | ||
|
||
}else if(cls %in% c("POSIXct", "POSIXt")){ | ||
freq <- "H" | ||
|
||
}else{ | ||
freq <- NULL | ||
|
||
} | ||
|
||
if(!is.null(freq)){ | ||
message(paste0("Frequency chosen: ", freq)) | ||
} | ||
|
||
res <- list(df = df, freq = freq) | ||
return(res) | ||
} |
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,53 @@ | ||
#' Infer frequency of a data frame. | ||
#' | ||
#' @param df A data frame with time series data. | ||
#' | ||
#' @return The inferred frequency. | ||
#' @export | ||
#' | ||
infer_frequency <- function(df){ | ||
|
||
freq <- NA | ||
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") | ||
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 | ||
} | ||
} | ||
|
||
if(is.na(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.") | ||
} | ||
|
||
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
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,22 @@ | ||
#' Prepare time series data for TimeGPT's API | ||
#' | ||
#' @param df A tsibble or a data frame with time series data. | ||
#' | ||
#' @return A list with the time series data for TimeGPT's API. | ||
#' @export | ||
#' | ||
prepare_data <- function(df){ | ||
if("unique_id" %in% names(df)){ | ||
df <- df[, c("unique_id", "ds", "y")] | ||
y <- list( | ||
columns = names(df), | ||
data = lapply(1:nrow(df), function(i) as.list(df[i,])) | ||
) | ||
}else{ | ||
# only "ds" and "y" columns | ||
y <- df$y | ||
names(y) <- df$ds | ||
y <- as.list(y) | ||
} | ||
return(y) | ||
} |
This file was deleted.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.