16
16
# ' they be assigned?
17
17
# ' @param trained A logical to indicate if the quantities for
18
18
# ' 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
25
23
# ' @param default Determines what fills empty rows
26
24
# ' left by leading/lagging (defaults to NA).
27
25
# ' @param keys A character vector of the keys in an epi_df
33
31
# ' conducted on new data (e.g. processing the outcome variable(s)).
34
32
# ' Care should be taken when using `skip = TRUE` as it may affect
35
33
# ' the computations for subsequent operations.
34
+ # ' @param id A unique identifier for the step
36
35
# ' @template step-return
37
36
# '
38
37
# ' @details The step assumes that the data are already _in the proper sequential
@@ -56,25 +55,26 @@ step_epi_lag <-
56
55
role = " predictor" ,
57
56
trained = FALSE ,
58
57
lag = 1 ,
58
+ prefix = " lag_" ,
59
59
default = NA ,
60
60
keys = epi_keys(recipe ),
61
61
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 )))
66
66
67
67
step_epi_shift(recipe ,
68
68
... ,
69
69
role = role ,
70
70
trained = trained ,
71
71
shift = lag ,
72
- prefix = " lag_ " ,
72
+ prefix = prefix ,
73
73
default = default ,
74
74
keys = keys ,
75
75
columns = columns ,
76
76
skip = skip ,
77
- id = rand_id( " epi_lag " )
77
+ id = id
78
78
)
79
79
}
80
80
@@ -89,25 +89,27 @@ step_epi_ahead <-
89
89
role = " outcome" ,
90
90
trained = FALSE ,
91
91
ahead = 1 ,
92
+ prefix = " ahead_" ,
92
93
default = NA ,
93
94
keys = epi_keys(recipe ),
94
95
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 )))
99
101
100
102
step_epi_shift(recipe ,
101
103
... ,
102
104
role = role ,
103
105
trained = trained ,
104
106
shift = - ahead ,
105
- prefix = " ahead_ " ,
107
+ prefix = prefix ,
106
108
default = default ,
107
109
keys = keys ,
108
110
columns = columns ,
109
111
skip = skip ,
110
- id = rand_id( " epi_ahead " )
112
+ id = id
111
113
)
112
114
}
113
115
@@ -176,15 +178,7 @@ prep.step_epi_shift <- function(x, training, info = NULL, ...) {
176
178
177
179
# ' @export
178
180
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
188
182
grid <- tidyr :: expand_grid(col = object $ columns , shift_val = object $ shift ) %> %
189
183
dplyr :: mutate(newname = glue :: glue(
190
184
paste0(" {object$prefix}" ," {abs(shift_val)}" ," _{col}" )
@@ -217,8 +211,7 @@ bake.step_epi_shift <- function(object, new_data, ...) {
217
211
# ' @export
218
212
print.step_epi_shift <-
219
213
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" ) %> %
222
215
paste0(" : " , abs(x $ shift )," ," )
223
216
recipes :: print_step(x $ columns , x $ terms , x $ trained , title , width )
224
217
invisible (x )
0 commit comments