Skip to content

Commit a2acbdf

Browse files
authored
Merge pull request #85 from cmu-delphi/km-kill-lags-good2
Added requested changes
2 parents 25dcd5f + 043618d commit a2acbdf

File tree

3 files changed

+42
-55
lines changed

3 files changed

+42
-55
lines changed

R/epi_shift_internal.R

+22-29
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616
#' they be assigned?
1717
#' @param trained A logical to indicate if the quantities for
1818
#' preprocessing have been estimated.
19-
#' @param lag,ahead A vector of nonnegative integers. Each specified column will
20-
#' be the lag or lead for each value in the vector. The use of negative
21-
#' integers will not throw an error and may still work, but is advised against
22-
#' as it may have unexpected results. Hence, a warning will be shown if the
23-
#' user inputs at least one negative integer value. However, the use of
24-
#' non-integer values will throw an error.
19+
#' @param lag,ahead A vector of integers. Each specified column will
20+
#' be the lag or lead for each value in the vector. Lag integers must be
21+
#' nonnegative, while ahead integers must be positive.
22+
#' @param prefix A prefix to indicate what type of variable this is
2523
#' @param default Determines what fills empty rows
2624
#' left by leading/lagging (defaults to NA).
2725
#' @param keys A character vector of the keys in an epi_df
@@ -33,6 +31,7 @@
3331
#' conducted on new data (e.g. processing the outcome variable(s)).
3432
#' Care should be taken when using `skip = TRUE` as it may affect
3533
#' the computations for subsequent operations.
34+
#' @param id A unique identifier for the step
3635
#' @template step-return
3736
#'
3837
#' @details The step assumes that the data are already _in the proper sequential
@@ -56,25 +55,26 @@ step_epi_lag <-
5655
role = "predictor",
5756
trained = FALSE,
5857
lag = 1,
58+
prefix = "lag_",
5959
default = NA,
6060
keys = epi_keys(recipe),
6161
columns = NULL,
62-
skip = FALSE) {
63-
if (any(lag<0)) {
64-
warning("Negative lag value; you may get unexpected results")
65-
}
62+
skip = FALSE,
63+
id = rand_id("epi_lag")) {
64+
stopifnot("Lag values must be nonnegative integers" =
65+
all(lag>=0 & lag == as.integer(lag)))
6666

6767
step_epi_shift(recipe,
6868
...,
6969
role = role,
7070
trained = trained,
7171
shift = lag,
72-
prefix = "lag_",
72+
prefix = prefix,
7373
default = default,
7474
keys = keys,
7575
columns = columns,
7676
skip = skip,
77-
id = rand_id("epi_lag")
77+
id = id
7878
)
7979
}
8080

@@ -89,25 +89,27 @@ step_epi_ahead <-
8989
role = "outcome",
9090
trained = FALSE,
9191
ahead = 1,
92+
prefix = "ahead_",
9293
default = NA,
9394
keys = epi_keys(recipe),
9495
columns = NULL,
95-
skip = FALSE) {
96-
if (any(ahead<0)) {
97-
warning("Negative ahead value; you may get unexpected results")
98-
}
96+
skip = FALSE,
97+
id = rand_id("epi_ahead")) {
98+
99+
stopifnot("Ahead values must be positive integers" =
100+
all(ahead>0 & ahead == as.integer(ahead)))
99101

100102
step_epi_shift(recipe,
101103
...,
102104
role = role,
103105
trained = trained,
104106
shift = -ahead,
105-
prefix = "ahead_",
107+
prefix = prefix,
106108
default = default,
107109
keys = keys,
108110
columns = columns,
109111
skip = skip,
110-
id = rand_id("epi_ahead")
112+
id = id
111113
)
112114
}
113115

@@ -176,15 +178,7 @@ prep.step_epi_shift <- function(x, training, info = NULL, ...) {
176178

177179
#' @export
178180
bake.step_epi_shift <- function(object, new_data, ...) {
179-
is_lag <- object$prefix == "lag_"
180-
if (!all(object$shift == as.integer(object$shift))) {
181-
error_msg <- paste0("step_epi_",
182-
ifelse(is_lag,"lag","ahead"),
183-
" requires ",
184-
ifelse(is_lag,"'lag'","'ahead'"),
185-
" argument to be integer valued.")
186-
rlang::abort(error_msg)
187-
}
181+
is_lag <- object$shift >= 0
188182
grid <- tidyr::expand_grid(col = object$columns, shift_val = object$shift) %>%
189183
dplyr::mutate(newname = glue::glue(
190184
paste0("{object$prefix}","{abs(shift_val)}","_{col}")
@@ -217,8 +211,7 @@ bake.step_epi_shift <- function(object, new_data, ...) {
217211
#' @export
218212
print.step_epi_shift <-
219213
function(x, width = max(20, options()$width - 30), ...) {
220-
## TODO add printing of the shifts
221-
title <- ifelse(x$prefix == "lag_","Lagging","Leading") %>%
214+
title <- ifelse(x$shift >= 0,"Lagging","Leading") %>%
222215
paste0(": ", abs(x$shift),",")
223216
recipes::print_step(x$columns, x$terms, x$trained, title, width)
224217
invisible(x)

man/step_epi_shift.Rd

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

tests/testthat/test-epi_shift_internal.R

+7-18
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,23 @@ slm_fit <- function(recipe, data = x) {
1818
}
1919

2020
test_that("Values for ahead and lag must be integer values", {
21-
r1 <- epi_recipe(x) %>%
22-
step_epi_ahead(death_rate, ahead = 3.6) %>%
23-
step_epi_lag(death_rate, lag = 1.9)
2421
expect_error(
25-
slm_fit(r1)
22+
r1 <- epi_recipe(x) %>%
23+
step_epi_ahead(death_rate, ahead = 3.6) %>%
24+
step_epi_lag(death_rate, lag = 1.9)
2625
)
2726
})
2827

29-
test_that("A negative lag value should be warned against", {
30-
expect_warning(
28+
test_that("A negative lag value should should throw an error", {
29+
expect_error(
3130
r2 <- epi_recipe(x) %>%
3231
step_epi_ahead(death_rate, ahead = 7) %>%
3332
step_epi_lag(death_rate, lag = -7)
3433
)
3534
})
3635

37-
test_that("A negative ahead value should be warned against", {
38-
expect_warning(
36+
test_that("A nonpositive ahead value should throw an error", {
37+
expect_error(
3938
r3 <- epi_recipe(x) %>%
4039
step_epi_ahead(death_rate, ahead = -7) %>%
4140
step_epi_lag(death_rate, lag = 7)
@@ -52,16 +51,6 @@ test_that("Values for ahead and lag cannot be duplicates", {
5251
)
5352
})
5453

55-
xxx <- x %>%
56-
mutate(`..y` = lead(death_rate,7),
57-
lag_7_death_rate = lag(death_rate,7),
58-
lag_14_death_rate = lag(death_rate, 14)) %>%
59-
rename(lag_0_death_rate = death_rate)
60-
61-
lm1 <- lm(`..y` ~ lag_0_death_rate + lag_7_death_rate + lag_14_death_rate,
62-
data = xxx)
63-
64-
6554
test_that("Check that epi_lag shifts applies the shift", {
6655
r5 <- epi_recipe(x) %>%
6756
step_epi_ahead(death_rate, ahead = 7) %>%

0 commit comments

Comments
 (0)