-
Notifications
You must be signed in to change notification settings - Fork 99
/
augment-tk_augment_lags.R
246 lines (206 loc) · 7.04 KB
/
augment-tk_augment_lags.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#' Add many lags to the data
#'
#' A handy function for adding multiple lagged columns to a data frame.
#' Works with `dplyr` groups too.
#'
#' @param .data A tibble.
#' @param .value One or more column(s) to have a transformation applied. Usage
#' of `tidyselect` functions (e.g. `contains()`) can be used to select multiple columns.
#' @param .lags One or more lags for the difference(s)
#' @param .names A vector of names for the new columns. Must be of same length as `.lags`.
#'
#'
#' @return Returns a `tibble` object describing the timeseries.
#'
#' @details
#'
#' __Lags vs Leads__
#'
#' A _negative lag_ is considered a lead. The `tk_augment_leads()` function is
#' identical to `tk_augment_lags()` with the exception that the
#' automatic naming convetion (`.names = 'auto'`) will convert column names with negative lags to
#' leads.
#'
#' __Benefits__
#'
#' This is a scalable function that is:
#'
#' - Designed to work with grouped data using `dplyr::group_by()`
#' - Add multiple lags by adding a sequence of lags using
#' the `.lags` argument (e.g. `.lags = 1:20`)
#'
#'
#' @seealso
#'
#' Augment Operations:
#'
#' - [tk_augment_timeseries_signature()] - Group-wise augmentation of timestamp features
#' - [tk_augment_holiday_signature()] - Group-wise augmentation of holiday features
#' - [tk_augment_slidify()] - Group-wise augmentation of rolling functions
#' - [tk_augment_lags()] - Group-wise augmentation of lagged data
#' - [tk_augment_differences()] - Group-wise augmentation of differenced data
#' - [tk_augment_fourier()] - Group-wise augmentation of fourier series
#'
#' Underlying Function:
#' - [lag_vec()] - Underlying function that powers `tk_augment_lags()`
#'
#' @examples
#' library(dplyr)
#'
#' # Lags
#' m4_monthly %>%
#' group_by(id) %>%
#' tk_augment_lags(contains("value"), .lags = 1:20)
#'
#' # Leads
#' m4_monthly %>%
#' group_by(id) %>%
#' tk_augment_leads(value, .lags = 1:-20)
#'
#' @name tk_augment_lags
NULL
#' @export
#' @rdname tk_augment_lags
tk_augment_lags <- function(.data,
.value,
.lags = 1,
.names = "auto") {
# Checks
column_expr <- enquo(.value)
if (rlang::quo_is_missing(column_expr)) stop(call. = FALSE, "tk_augment_lags(.value) is missing.")
# if (rlang::is_missing(.lags)) stop(call. = FALSE, "tk_augment_lags(.lags) is missing.")
UseMethod("tk_augment_lags", .data)
}
#' @export
tk_augment_lags.data.frame <- function(.data,
.value,
.lags = 1,
.names = "auto") {
# column_expr <- enquo(.value)
col_nms <- names(tidyselect::eval_select(rlang::enquo(.value), .data))
make_call <- function(col, lag_val) {
rlang::call2(
"lag_vec",
x = rlang::sym(col),
lag = lag_val,
.ns = "timetk"
)
}
grid <- expand.grid(
col = col_nms,
lag_val = .lags,
stringsAsFactors = FALSE
)
calls <- purrr::pmap(.l = list(grid$col, grid$lag_val), make_call)
if (any(.names == "auto")) {
newname <- paste0(grid$col, "_lag", grid$lag_val)
} else {
newname <- as.list(.names)
}
calls <- purrr::set_names(calls, newname)
ret <- tibble::as_tibble(dplyr::mutate(.data, !!!calls))
return(ret)
}
#' @export
tk_augment_lags.grouped_df <- function(.data,
.value,
.lags = 1,
.names = "auto") {
# Tidy Eval Setup
column_expr <- enquo(.value)
group_names <- dplyr::group_vars(.data)
.data %>%
tidyr::nest() %>%
dplyr::mutate(nested.col = purrr::map(
.x = data,
.f = function(df) tk_augment_lags(
.data = df,
.value = !! enquo(.value),
.lags = .lags,
.names = .names
)
)) %>%
dplyr::select(-data) %>%
tidyr::unnest(cols = nested.col) %>%
dplyr::group_by_at(.vars = group_names)
}
#' @export
tk_augment_lags.default <- function(.data,
.value,
.lags = 1,
.names = "auto") {
stop(paste0("`tk_augment_lags` has no method for class ", class(data)[[1]]))
}
#' @export
#' @rdname tk_augment_lags
tk_augment_leads <- function(.data,
.value,
.lags = -1,
.names = "auto") {
# Checks
column_expr <- enquo(.value)
if (rlang::quo_is_missing(column_expr)) stop(call. = FALSE, "tk_augment_leads(.value) is missing.")
# if (rlang::is_missing(.lags)) stop(call. = FALSE, "tk_augment_leads(.lags) is missing.")
UseMethod("tk_augment_leads", .data)
}
#' @export
tk_augment_leads.data.frame <- function(.data,
.value,
.lags = -1,
.names = "auto") {
# column_expr <- enquo(.value)
col_nms <- names(tidyselect::eval_select(rlang::enquo(.value), .data))
make_call <- function(col, lag_val) {
rlang::call2(
"lag_vec",
x = rlang::sym(col),
lag = lag_val,
.ns = "timetk"
)
}
grid <- expand.grid(
col = col_nms,
lag_val = .lags,
stringsAsFactors = FALSE
)
calls <- purrr::pmap(.l = list(grid$col, grid$lag_val), make_call)
if (any(.names == "auto")) {
newname <- paste0(grid$col, "_lag", grid$lag_val) %>%
stringr::str_replace_all("lag-","lead")
} else {
newname <- as.list(.names)
}
calls <- purrr::set_names(calls, newname)
ret <- tibble::as_tibble(dplyr::mutate(.data, !!!calls))
return(ret)
}
#' @export
tk_augment_leads.grouped_df <- function(.data,
.value,
.lags = -1,
.names = "auto") {
# Tidy Eval Setup
column_expr <- enquo(.value)
group_names <- dplyr::group_vars(.data)
.data %>%
tidyr::nest() %>%
dplyr::mutate(nested.col = purrr::map(
.x = data,
.f = function(df) tk_augment_leads(
.data = df,
.value = !! enquo(.value),
.lags = .lags,
.names = .names
)
)) %>%
dplyr::select(-data) %>%
tidyr::unnest(cols = nested.col) %>%
dplyr::group_by_at(.vars = group_names)
}
#' @export
tk_augment_leads.default <- function(.data,
.value,
.lags = 1,
.names = "auto") {
stop(paste0("`tk_augment_leads` has no method for class ", class(data)[[1]]))
}