Skip to content

Commit c5136b3

Browse files
committed
inheritParams, correct print, test adjust subset
1 parent 28db575 commit c5136b3

File tree

3 files changed

+48
-39
lines changed

3 files changed

+48
-39
lines changed

R/step_adjust_latency.R

+17-26
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@
99
#' useful in realtime and pseudo-prospective forecasting for data where there is
1010
#' some delay between the day recorded and when that data is available.
1111
#'
12-
#' @param recipe A recipe object. The step will be added to the
13-
#' sequence of operations for this recipe.
14-
#' @param ... One or more selector functions to choose variables for this step.
15-
#' See [recipes::selections()] for more details. Typically you will not need
16-
#' to set this manually, as the necessary adjustments will be done for the
17-
#' predictors and outcome.
1812
#' @param method a character. Determines the method by which the
1913
#' forecast handles latency. The options are:
2014
#' - `"extend_ahead"`: Lengthen the ahead so that forecasting from the last
@@ -52,22 +46,12 @@
5246
#' `forecast_date` is determined either via the `fixed_latency`, or is set to
5347
#' the `epi_df`'s `as_of` value if `fixed_latency` is also `NULL`.
5448
#' @param role For model terms created by this step, what analysis role should
55-
#' they be assigned? `lag` is default a predictor while `ahead` is an outcome.
56-
#' It should be correctly inferred and not need setting
57-
#' @param trained A logical to indicate if the quantities for preprocessing have
58-
#' been estimated.
59-
#' @param columns A character string of column names to be adjusted; these
60-
#' should be the original columns, and not the derived ones
49+
#' they be assigned? `lag` is a predictor while `ahead` is an outcome. It
50+
#' should be correctly inferred and not need setting
6151
#' @param default Determines what fills empty rows
6252
#' left by leading/lagging (defaults to NA).
63-
#' @param skip A logical. Should the step be skipped when the
64-
#' recipe is baked by [bake()]? While all operations are baked
65-
#' when [prep()] is run, some operations may not be able to be
66-
#' conducted on new data (e.g. processing the outcome variable(s)).
67-
#' Care should be taken when using `skip = TRUE` as it may affect
68-
#' the computations for subsequent operations.
69-
#' @param id A unique identifier for the step
7053
#' @template step-return
54+
#' @inheritParams recipes::step_lag
7155
#'
7256
#' @details The step assumes that the pipeline has already applied either
7357
#' `step_epi_ahead` or `step_epi_lag` depending on the value of `"method"`,
@@ -189,6 +173,7 @@ step_adjust_latency <-
189173
shift_cols = relevant_shifts,
190174
default = default,
191175
keys = epi_keys(recipe),
176+
columns = columns,
192177
skip = skip,
193178
id = id
194179
)
@@ -197,7 +182,7 @@ step_adjust_latency <-
197182

198183
step_adjust_latency_new <-
199184
function(terms, role, trained, forecast_date, latency, shift_cols, time_type, default,
200-
keys, method, epi_keys_checked, skip, id) {
185+
keys, method, epi_keys_checked, columns, skip, id) {
201186
step(
202187
subclass = "adjust_latency",
203188
terms = terms,
@@ -210,6 +195,7 @@ step_adjust_latency_new <-
210195
shift_cols = shift_cols,
211196
default = default,
212197
keys = keys,
198+
columns = columns,
213199
skip = skip,
214200
id = id
215201
)
@@ -220,7 +206,7 @@ step_adjust_latency_new <-
220206
#' @importFrom glue glue
221207
prep.step_adjust_latency <- function(x, training, info = NULL, ...) {
222208
# get the columns used, even if it's all of them
223-
terms_used <- x$columns
209+
terms_used <- x$terms
224210
if (length(terms_used) == 0) {
225211
terms_used <- info %>%
226212
filter(role == "raw") %>%
@@ -274,6 +260,7 @@ prep.step_adjust_latency <- function(x, training, info = NULL, ...) {
274260
keys = x$keys,
275261
method = x$method,
276262
epi_keys_checked = x$epi_keys_checked,
263+
columns = recipes_eval_select(latency_cols$original_name, training, info),
277264
skip = x$skip,
278265
id = x$id
279266
)
@@ -301,14 +288,18 @@ print.step_adjust_latency <-
301288
if (!is.null(x$forecast_date)) {
302289
conj <- "with forecast date"
303290
extra_text <- x$forecast_date
304-
} else if (!is.null(x$shift_cols)) {
305-
conj <- "with latencies"
306-
extra_text <- x$shift_cols
291+
} else if (!is.null(x$latency)) {
292+
conj <- if (length(x$latency == 1)) {
293+
"with latency"
294+
} else {
295+
"with latencies"
296+
}
297+
extra_text <- x$latency
307298
} else {
308-
conj <- ""
299+
conj <- "with latency"
309300
extra_text <- "set at train time"
310301
}
311-
print_epi_step(terms, NULL, x$trained, x$method,
302+
print_epi_step(terms, terms, x$trained, x$method,
312303
conjunction = conj,
313304
extra_text = extra_text
314305
)

man/step_adjust_latency.Rd

+11-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-step_adjust_latency.R

+20
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,24 @@ test_that("epi_adjust_latency correctly extends the lags", {
225225

226226
test_that("`step_adjust_latency` only allows one instance of itself", {})
227227

228+
test_that("`step_adjust_latency` only uses the columns specified in the `...`", {
229+
r5 <- epi_recipe(x) %>%
230+
step_epi_lag(death_rate, lag = c(0, 6, 11)) %>%
231+
step_epi_lag(case_rate, lag = c(1, 5)) %>%
232+
step_epi_ahead(death_rate, ahead = ahead) %>%
233+
step_adjust_latency(case_rate, method = "extend_lags")
234+
235+
fit5 <- slm_fit(r5, data = real_x)
236+
expect_equal(names(fit5$fit$fit$fit$coefficients), c("(Intercept)", "lag_0_death_rate", "lag_6_death_rate", "lag_11_death_rate", "lag_6_case_rate", "lag_10_case_rate"))
237+
})
238+
228239
test_that("setting fixed_* works for `step_adjust_latency`", {})
240+
241+
test_that("printing step_adjust_latency results in expected output", {
242+
r5 <- epi_recipe(x) %>%
243+
step_epi_lag(death_rate, lag = c(0, 6, 11)) %>%
244+
step_epi_lag(case_rate, lag = c(1, 5)) %>%
245+
step_epi_ahead(death_rate, ahead = ahead) %>%
246+
step_adjust_latency(case_rate, method = "extend_lags")
247+
expect_snapshot(r5)
248+
})

0 commit comments

Comments
 (0)