-
Notifications
You must be signed in to change notification settings - Fork 3
/
step_expose.R
161 lines (141 loc) · 4.43 KB
/
step_expose.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
#' Create exposure records in a `recipes` step
#'
#' `step_expose()` creates a *specification* of a recipe step that will convert
#' a data frame of census-level records to exposure-level records.
#'
#' Policy year exposures are calculated as a default. To switch to calendar
#' exposures or another exposure length, use pass the appropriate arguments to
#' the `options` parameter.
#'
#' Policy numbers are dropped as a default whenever the recipe is baked. This
#' is done to prevent unintentional errors when the model formula includes
#' all variables (`y ~ .`). If policy numbers are required for any reason
#' (mixed effect models, identification, etc.), set `drop_pol_num` to `FALSE`.
#'
#' @inheritParams recipes::step_center
#' @inheritParams expose
#' @param options A named list of additional arguments passed to [expose()].
#' @param drop_pol_num Whether the `pol_num` column produced by [expose()]
#' should be dropped. Defaults to `TRUE`.
#'
#' @return An updated version of `recipe` with the new expose step added to the
#' sequence of any existing operations. For the `tidy` method, a `tibble` with
#' the columns `exposure_type`, `target_status`, `start_date`, and `end_date`.
#'
#' @examples
#'
#' expo_rec <- recipes::recipe(status ~ ., toy_census) |>
#' step_expose(end_date = "2022-12-31", target_status = "Surrender",
#' options = list(expo_length = "month")) |>
#' prep()
#'
#' recipes::juice(expo_rec)
#'
#' @seealso
#' [expose()]
#'
#' @export
step_expose <- function(recipe,
...,
role = NA,
trained = FALSE,
end_date,
start_date = as.Date("1900-01-01"),
target_status = NULL,
options = list(
cal_expo = FALSE,
expo_length = "year"),
drop_pol_num = TRUE,
skip = TRUE,
id = recipes::rand_id("expose")) {
if (length(rlang::enquos(...)) > 0) {
rlang::warn("No variable selectors are used for step_expose.")
}
if (!"cal_expo" %in% names(options)) options$cal_expo <- FALSE
if (!"expo_length" %in% names(options)) options$expo_length <- "year"
recipes::add_step(
recipe,
step_expose_new(
terms = rlang::enquos(...),
role = role,
trained = trained,
end_date = end_date,
start_date = start_date,
target_status = target_status,
options = options,
drop_pol_num = drop_pol_num,
skip = skip,
id = id
)
)
}
step_expose_new <- function(terms, role, trained, end_date, start_date,
target_status, options, drop_pol_num, skip, id) {
recipes::step(
subclass = "expose",
terms = terms,
role = role,
trained = trained,
end_date = end_date,
start_date = start_date,
target_status = target_status,
options = options,
drop_pol_num = drop_pol_num,
skip = skip,
id = id
)
}
#' @export
prep.step_expose <- function(x, training, info = NULL, ...) {
step_expose_new(
terms = x$terms,
role = x$role,
trained = TRUE,
end_date = x$end_date,
start_date = x$start_date,
target_status = x$target_status,
options = x$options,
drop_pol_num = x$drop_pol_num,
skip = x$skip,
id = x$id
)
}
#' @export
bake.step_expose <- function(object, new_data, ...) {
new_data <- rlang::exec("expose",
new_data,
object$end_date,
object$start_date,
object$target_status,
!!!object$options)
if (object$drop_pol_num) {
new_data |> select(-pol_num)
} else {
new_data
}
}
#' @export
print.step_expose <- function(x, width = max(20, options()$width - 30), ...) {
title <- glue::glue("Exposed data based on {if (x$options$cal_expo) 'calendar' else 'policy'} {x$options$expo_length}s{if (!is.null(x$target_status)) paste(' for target status', paste(x$target_status, collapse = ', ')) else ''} ")
recipes::print_step(
untr_obj = NULL,
tr_obj = NULL,
trained = x$trained,
title = title,
width = width
)
invisible(x)
}
#' @export
tidy.step_expose <- function(x, ...) {
tibble::tibble(
exposure_type = paste(
if (x$options$cal_expo) "calendar" else "policy",
x$options$expo_length,
sep = "_"
),
target_status = x$target_status,
start_date = x$start_date,
end_date = x$end_date
)
}