forked from pharmaverse/admiral
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathderive_var_lstalvdt.R
239 lines (233 loc) · 6.83 KB
/
derive_var_lstalvdt.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
#' Derive Last Known Alive Date
#'
#' Add the last known alive date (`LSTALVDT`) to the dataset.
#'
#' @param dataset Input dataset
#'
#' The `USUBJID` variable is expected.
#'
#' @param ... Source of known alive dates. A `lstalvdt_source()` object is
#' expected.
#'
#' @param subject_keys Variables to uniquely identify a subject
#'
#' A list of quosures where the expressions are symbols as returned by
#' `vars()` is expected.
#'
#' @details The following steps are performed to create the output dataset:
#'
#' \enumerate{ \item For each source dataset the observations as specified by
#' the `filter` element are selected. Then for each patient the last
#' observation (with respect to `date`) is selected.
#'
#' \item The `LSTALVDT` variable is set to the variable specified by the
#' \code{date} element. If the date variable is a datetime variable, only
#' the datepart is copied. If the source variable is a character variable, it
#' is converted to a date. If the date is incomplete, it is imputed as
#' specified by the \code{date_imputation} element.
#'
#' \item The variables specified by the \code{traceability_vars} element are
#' added.
#'
#' \item The selected observations of all source datasets are combined into a
#' single dataset.
#'
#' \item For each patient the last observation (with respect to the `LSTALVDT`
#' variable) from the single dataset is selected and the new variable is
#' merged to the input dataset. }
#'
#' @author Stefan Bundfuss
#'
#' @return The input dataset with the `LSTALVDT` variable added.
#'
#' @keywords derivation adsl
#'
#' @export
#'
#' @examples
#' library(dplyr, warn.conflicts = FALSE)
#' library(admiral.test)
#' data("dm")
#' data("ae")
#' data("lb")
#' data("adsl")
#'
#' ae_start <- lstalvdt_source(
#' dataset = ae,
#' date = AESTDTC,
#' date_imputation = "first"
#' )
#' ae_end <- lstalvdt_source(
#' dataset = ae,
#' date = AEENDTC,
#' date_imputation = "first"
#' )
#' lb_date <- lstalvdt_source(
#' dataset = lb,
#' date = LBDTC,
#' filter = nchar(LBDTC) >= 10
#' )
#' adsl_date <- lstalvdt_source(dataset = adsl, date = TRTEDT)
#'
#' dm %>%
#' derive_var_lstalvdt(ae_start, ae_end, lb_date, adsl_date) %>%
#' select(USUBJID, LSTALVDT)
#'
#' # derive last alive date and traceability variables
#' ae_start <- lstalvdt_source(
#' dataset = ae,
#' date = AESTDTC,
#' date_imputation = "first",
#' traceability_vars = vars(
#' LALVDOM = "AE",
#' LALVSEQ = AESEQ,
#' LALVVAR = "AESTDTC"
#' )
#' )
#'
#' ae_end <- lstalvdt_source(
#' dataset = ae,
#' date = AEENDTC,
#' date_imputation = "first",
#' traceability_vars = vars(
#' LALVDOM = "AE",
#' LALVSEQ = AESEQ,
#' LALVVAR = "AEENDTC"
#' )
#' )
#' lb_date <- lstalvdt_source(
#' dataset = lb,
#' date = LBDTC,
#' filter = nchar(LBDTC) >= 10,
#' traceability_vars = vars(
#' LALVDOM = "LB",
#' LALVSEQ = LBSEQ,
#' LALVVAR = "LBDTC"
#' )
#' )
#'
#' adsl_date <- lstalvdt_source(
#' dataset = adsl,
#' date = TRTEDT,
#' traceability_vars = vars(
#' LALVDOM = "ADSL",
#' LALVSEQ = NA_integer_,
#' LALVVAR = "TRTEDTM"
#' )
#' )
#'
#' dm %>%
#' derive_var_lstalvdt(ae_start, ae_end, lb_date, adsl_date) %>%
#' select(USUBJID, LSTALVDT, LALVDOM, LALVSEQ, LALVVAR)
derive_var_lstalvdt <- function(dataset,
...,
subject_keys = vars(STUDYID, USUBJID)) {
assert_data_frame(dataset)
assert_vars(subject_keys)
sources <- list(...)
assert_list_of(sources, "lstalvdt_source")
add_data <- vector("list", length(sources))
for (i in seq_along(sources)) {
if (i > 1) {
warn_if_inconsistent_list(
base = sources[[i - 1]]$traceability_vars,
compare = sources[[i]]$traceability_vars,
list_name = "lstalvdt_source()",
i = i
)
}
date <- quo_get_expr(sources[[i]]$date)
add_data[[i]] <- sources[[i]]$dataset %>%
filter_if(sources[[i]]$filter) %>%
filter_extreme(
order = vars(!!date),
by_vars = subject_keys,
mode = "last",
check_type = "none"
)
if (is.Date(add_data[[i]][[as_string(date)]])) {
add_data[[i]] <- transmute(
add_data[[i]],
!!!subject_keys,
!!!sources[[i]]$traceability_vars,
LSTALVDT = !!date
)
} else if (is.instant(add_data[[i]][[as_string(date)]])) {
add_data[[i]] <- transmute(
add_data[[i]],
!!!subject_keys,
!!!sources[[i]]$traceability_vars,
LSTALVDT = date(!!date)
)
} else {
add_data[[i]] <- transmute(
add_data[[i]],
!!!subject_keys,
!!!sources[[i]]$traceability_vars,
LSTALVDT = convert_dtc_to_dt(
!!date,
date_imputation = sources[[i]]$date_imputation
)
)
}
}
all_data <- add_data %>%
bind_rows() %>%
filter(!is.na(LSTALVDT)) %>%
filter_extreme(
by_vars = subject_keys,
order = vars(LSTALVDT),
mode = "last",
check_type = "none"
)
left_join(dataset, all_data, by = vars2chr(subject_keys))
}
#' Create an `lstalvdt_source` object
#'
#' @param dataset A data.frame containing a source dataset.
#'
#' @param filter An unquoted condition for filtering `dataset`.
#'
#' @param date A variable providing a date where the patient was known to be
#' alive. A date, a datetime, or a character variable containing ISO 8601
#' dates can be specified. An unquoted symbol is expected.
#'
#' @param date_imputation A string defining the date imputation for `date`.
#' See `date_imputation` parameter of `impute_dtc()` for valid values.
#'
#' @param traceability_vars A named list returned by `vars()` defining the
#' traceability variables, e.g. `vars(LALVDOM = "AE", LALVSEQ = AESEQ, LALVVAR
#' = "AESTDTC")`. The values must be a symbol, a character string, or `NA`.
#'
#' @param date_var Deprecated, please use `date` instead.
#'
#' @author Stefan Bundfuss
#'
#' @keywords source_specifications
#'
#' @export
#'
#' @return An object of class "lstalvdt_source".
lstalvdt_source <- function(dataset,
filter = NULL,
date,
date_imputation = NULL,
traceability_vars = NULL,
date_var = deprecated()) {
if (!missing(date_var)) {
deprecate_warn("0.3.0", "lstalvdt_source(date_var = )", "lstalvdt_source(date = )")
date <- enquo(date_var)
}
if (!is.null(date_imputation)) {
assert_that(is_valid_date_entry(date_imputation))
}
out <- list(
dataset = assert_data_frame(dataset),
filter = assert_filter_cond(enquo(filter), optional = TRUE),
date = assert_symbol(enquo(date)),
date_imputation = date_imputation,
traceability_vars = assert_varval_list(traceability_vars, optional = TRUE)
)
class(out) <- c("lstalvdt_source", "list")
out
}