forked from business-science/modeltime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev-parse_index.R
108 lines (93 loc) · 2.98 KB
/
dev-parse_index.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#' Developer Tools for parsing date and date-time information
#'
#' These functions are designed to assist developers in extending the `modeltime`
#' package.
#'
#' @param data A data frame
#' @param period A period to calculate from the time index. Numeric values are returned as-is.
#' "auto" guesses a numeric value from the index. A time-based phrase (e.g. "7 days") calculates
#' the number of timestamps that typically occur within the time-based phrase.
#'
#' @return
#' - parse_index_from_data(): Returns a tibble containing the date or date-time column.
#' - parse_period_from_index(): Returns the numeric period from a tibble containing the index.
#'
#' @examples
#' library(dplyr)
#' library(timetk)
#'
#' predictors <- m4_monthly %>%
#' filter(id == "M750") %>%
#' select(-value)
#'
#' index_tbl <- parse_index_from_data(predictors)
#' index_tbl
#'
#' period <- parse_period_from_index(index_tbl, period = "1 year")
#' period
#'
#' @name parse_index
NULL
#' @rdname parse_index
#' @export
parse_index_from_data <- function(data) {
UseMethod("parse_index_from_data", data)
}
#' @export
parse_index_from_data.default <- function(data) {
rlang::abort(paste0("No method for class ", class(data)[1]))
}
#' @export
parse_index_from_data.data.frame <- function(data) {
idx <- NULL
tryCatch({
# Try to get a period from a user-provided index
idx_col <- timetk::tk_get_timeseries_variables(data)[1]
idx <- data %>% timetk::tk_index() # Will generate an error if no time series index
}, error = function(e) {
rlang::abort("No date or date-time variable provided. Please supply a date or date-time variable as a predictor.")
})
return(tibble::tibble(!! idx_col := idx))
}
#' @rdname parse_index
#' @export
parse_period_from_index <- function(data, period) {
UseMethod("parse_period_from_index", data)
}
#' @export
parse_period_from_index.default <- function(data, period) {
rlang::abort(paste0("No method for class ", class(data)[1]))
}
#' @export
parse_period_from_index.data.frame <- function(data, period) {
period <- switch_period(period)
# If character, parse period / If numeric, pass over
tryCatch({
if (is.character(period)) {
period <- tolower(period)
if (period != "none") {
idx <- data %>% timetk::tk_index()
period <- unique(idx) %>% sort() %>% timetk::tk_get_frequency(period, message = TRUE)
} else {
# period = "none"
message("Using period = 1 (no seasonal period).")
period <- 1
}
}
}, error = function(e) {
rlang::abort("The `period` argument could not be parsed.")
})
return(period)
}
switch_period <- function(period) {
if (tolower(period) == "daily") {
period <- "1 day"
}
if (tolower(period) == "weekly") {
period <- "1 week"
}
if (tolower(period) == "yearly") {
period <- "1 year"
}
return(period)
}