-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinterval_collapse.R
364 lines (335 loc) · 14.5 KB
/
interval_collapse.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#' @title Collapse an interval variable to the most detailed common set of
#' intervals
#'
#' @description Collapse an interval variable to the most detailed common set of
#' intervals available for each combination of `id_cols` in a dataset.
#' Aggregates the collapsed dataset to the common set of intervals.
#'
#' @inheritParams agg
#' @param dt \[`data.table()`\]\cr
#' Dataset containing the interval variable.
#' @param col_stem \[`character(1)`\]\cr
#' The name of the variable to collapse, should not include the '_start' or
#' '_end' suffix for the interval variable.
#' @param missing_dt_severity \[`character(1)`\]\cr
#' How severe should the consequences of missing intervals that prevent
#' collapsing to the most detailed common set of intervals be? Can be either
#' 'skip', 'stop', 'warning', 'message', or 'none'. If not "stop", then only the
#' intervals that can be correctly collapsed will be done.
#' @param include_missing \[`logical(1)`\]\cr
#' Whether to include missing intervals in the identified most detailed common
#' intervals. These missing intervals are not present in all combinations of
#' `id_cols`. Default is "FALSE".
#'
#' @return \[`data.table()`\] with `id_cols` and `value_cols` columns but with
#' the `col_stem` intervals reduced to only the most detailed common set of
#' intervals.
#'
#' @examples
#' id_cols <- c("year_start", "year_end", "sex", "age_start", "age_end")
#' value_cols <- c("value")
#'
#' # set up test input data.table
#' input_dt_male <- data.table::CJ(year_start = 2005, year_end = 2010,
#' sex = "male",
#' age_start = seq(0, 95, 5),
#' value = 25)
#' input_dt_male[age_start == 95, value := 5]
#' input_dt_female <- data.table::CJ(year_start = 2005:2009,
#' sex = "female",
#' age_start = seq(0, 95, 1),
#' value = 1)
#' gen_end(input_dt_female, setdiff(id_cols, c("year_end", "age_end")),
#' col_stem = "year", right_most_endpoint = 2010)
#' input_dt <- rbind(input_dt_male, input_dt_female)
#' gen_end(input_dt, setdiff(id_cols, "age_end"), col_stem = "age")
#' data.table::setkeyv(input_dt, id_cols)
#'
#'
#' collapsed_dt <- collapse_common_intervals(
#' dt = input_dt,
#' id_cols = id_cols,
#' value_cols = value_cols,
#' col_stem = "year"
#' )
#' collapsed_dt <- collapse_common_intervals(
#' dt = collapsed_dt,
#' id_cols = id_cols,
#' value_cols = value_cols,
#' col_stem = "age"
#' )
#'
#' @export
collapse_common_intervals <- function(dt,
id_cols,
value_cols,
col_stem,
agg_function = sum,
missing_dt_severity = "stop",
overlapping_dt_severity = "stop",
include_missing = FALSE) {
# Validate arguments ------------------------------------------------------
# check `col_stem` argument
assertthat::assert_that(assertthat::is.string(col_stem),
msg = "`col_stem` must be a string")
cols <- paste0(col_stem, "_", c("start", "end"))
# check `agg_function` argument
assertthat::assert_that(assertive::is_function(agg_function),
identical(agg_function, sum) |
identical(agg_function, prod),
msg = "`agg_function` must be either the 'sum' or
'prod' function")
# check `id_cols` argument
assertive::assert_is_character(id_cols)
error_msg <- paste0("`id_cols` must include '",
paste(cols, collapse = "', '"), "'")
assertthat::assert_that(all(cols %in% id_cols), msg = error_msg)
# check `value_cols` argument
assertive::assert_is_character(value_cols)
# check `dt` argument
assertive::assert_is_data.table(dt)
assertable::assert_colnames(dt, c(id_cols, value_cols), only_colnames = T,
quiet = T)
for (value_col in value_cols) {
assertive::assert_is_numeric(dt[[value_col]])
}
demUtils::assert_is_unique_dt(dt, id_cols)
for (col in cols) {
assertive::assert_is_numeric(dt[[col]])
}
# check `missing_dt_severity` argument
severity_choices <- c("skip", "stop", "warning","message", "none")
assertthat::assert_that(
assertthat::is.string(missing_dt_severity),
checkmate::checkChoice(missing_dt_severity, severity_choices),
msg = paste0("`missing_dt_severity` must be one of '",
paste(severity_choices, collapse = "', '"), "'")
)
# check `overlapping_dt_severity` argument
severity_choices <- c("skip", "stop", "warning","message", "none")
assertthat::assert_that(
assertthat::is.string(overlapping_dt_severity),
checkmate::checkChoice(overlapping_dt_severity, severity_choices),
msg = paste0("`overlapping_dt_severity` must be one of '",
paste(severity_choices, collapse = "', '"), "'")
)
# check `include_missing` argument
assertthat::assert_that(assertthat::is.flag(include_missing),
msg = "`include_missing` must be a logical")
# Identify and collapse to most detailed common intervals -----------------
original_col_order <- copy(names(dt))
original_keys <- copy(key(dt))
interval_id_cols <- id_cols[grepl("_start$|_end$", id_cols)]
categorical_id_cols <- id_cols[!id_cols %in% interval_id_cols]
by_id_cols <- id_cols[!id_cols %in% cols]
# check for overlapping intervals
if (overlapping_dt_severity != "skip") {
overlapping_dt <- dt[
, identify_overlapping_intervals(unique(.SD), identify_all_possible = overlapping_dt_severity != "none"),
.SDcols = cols, by = by_id_cols
]
data.table::setnames(overlapping_dt, c("start", "end"), cols)
overlapping_dt[, issue := "overlapping intervals present"]
empty_dt <- function(dt) nrow(dt) == 0
error_msg <-
paste0("Some overlapping intervals were identified in `dt`.\n",
"These will be automatically dropped.\n",
paste0(capture.output(overlapping_dt), collapse = "\n"))
assertive::assert_engine(empty_dt, overlapping_dt,
msg = error_msg, severity = overlapping_dt_severity)
# drop overlapping intervals
dt <- merge(dt, overlapping_dt, by = id_cols, all = T)
dt <- dt[is.na(issue)]
dt[, issue := NULL]
}
common_intervals <- identify_common_intervals(
dt,
id_cols,
col_stem,
include_missing = TRUE # these are identified below
)
data.table::setnames(common_intervals, cols, c("common_start", "common_end"))
collapsed_dt <- merge_common_intervals(dt, common_intervals, col_stem)
if (missing_dt_severity != "skip") {
# check for missing intervals
missing_dt <- collapsed_dt[
, identify_missing_intervals(unique(.SD), common_intervals),
.SDcols = cols, by = by_id_cols
]
data.table::setnames(missing_dt, c("start", "end"), cols)
empty_missing_dt <- function(dt) nrow(dt) == 0
error_msg <-
paste0("Some intervals in `dt` are missing making it impossible to collapse ",
"the desired column.\n",
paste0(capture.output(missing_dt), collapse = "\n"))
assertive::assert_engine(empty_missing_dt, missing_dt,
msg = error_msg, severity = missing_dt_severity)
# drop the common intervals that the missing intervals are part of
if (nrow(missing_dt) > 0) {
# determine the common intervals for the detailed missing dataset
full_missing_dt <- merge_common_intervals(
missing_dt,
common_intervals,
col_stem
)
full_missing_dt <- full_missing_dt[, c(if (include_missing) by_id_cols,
c("common_start", "common_end")),
with = F]
full_missing_dt <- unique(full_missing_dt)
# drop the common intervals that the missing intervals are part of
full_missing_dt[, drop := TRUE]
collapsed_dt <- merge(collapsed_dt, full_missing_dt, all = T,
by = c(if (include_missing) by_id_cols,
c("common_start", "common_end")))
collapsed_dt <- collapsed_dt[is.na(drop)]
}
}
# aggregate so that rows are all unique again
collapsed_dt[, c(cols) := NULL]
data.table::setnames(collapsed_dt, c("common_start", "common_end"), cols)
collapsed_dt <- collapsed_dt[, lapply(.SD, agg_function),
.SDcols = value_cols,
by = id_cols]
data.table::setcolorder(collapsed_dt, original_col_order)
data.table::setkeyv(collapsed_dt, original_keys)
return(collapsed_dt)
}
#' @title Helper functions for collapsing to the most detailed common intervals
#'
#' @description [`identify_common_intervals()`] identifies the most detailed
#' common set of intervals for a given interval variable and
#' [`merge_common_intervals()`] merges these on to the original dataset.
#'
#' @inheritParams collapse_common_intervals
#' @param id_cols \[`character()`\]\cr
#' ID columns that uniquely identify each row of `dt`. If 'NULL' then common
#' intervals across entire dataset are identified.
#'
#' @return [`identify_common_intervals()`] returns a \[`data.table()`\] with two
#' columns called '{col_stem}_start' and '{col_stem}_end' defining the most
#' detailed common set of intervals for the `col_stem` interval variable.
#'
#' @examples
#' id_cols <- c("year_start", "year_end", "sex", "age_start", "age_end")
#'
#' # set up test input data.table
#' input_dt_male <- data.table::CJ(year_start = 2005, year_end = 2010,
#' sex = "male",
#' age_start = seq(0, 95, 5),
#' value = 25)
#' input_dt_male[age_start == 95, value := 5]
#' input_dt_female <- data.table::CJ(year_start = 2005:2009,
#' sex = "female",
#' age_start = seq(0, 95, 1),
#' value = 1)
#' gen_end(input_dt_female, setdiff(id_cols, c("year_end", "age_end")),
#' col_stem = "year", right_most_endpoint = 2010)
#' input_dt <- rbind(input_dt_male, input_dt_female)
#' gen_end(input_dt, setdiff(id_cols, "age_end"), col_stem = "age")
#' data.table::setkeyv(input_dt, id_cols)
#'
#' common_intervals <- hierarchyUtils:::identify_common_intervals(
#' dt = input_dt,
#' id_cols = id_cols,
#' col_stem = "year"
#' )
#' data.table::setnames(common_intervals, c("year_start", "year_end"),
#' c("common_start", "common_end"))
#'
#' result_dt <- hierarchyUtils:::merge_common_intervals(
#' dt = input_dt,
#' common_intervals = common_intervals,
#' col_stem = "year"
#' )
#'
#' @rdname helper_common_intervals
identify_common_intervals <- function(dt,
id_cols,
col_stem,
include_missing = FALSE) {
cols <- paste0(col_stem, "_", c("start", "end"))
by_id_cols <- id_cols[!id_cols %in% cols]
if (is.null(id_cols)) {
intervals <- unique(dt[, cols, with = F])
intervals <- list(intervals, intervals)
} else {
# identify unique interval combinations in dataset
intervals <- unname(split(dt, by = by_id_cols))
intervals <- lapply(intervals, function(split_dt) {
split_dt <- split_dt[, cols, with = F]
data.table::setnames(split_dt, cols, c("start", "end"))
return(split_dt)
})
intervals <- unique(intervals)
intervals <- intervals[mapply(function(ints_dt) nrow(ints_dt) > 0, intervals)]
}
check_each_pair <- function(ints_dt1, ints_dt2) {
ints1 <- intervals::Intervals_full(as.matrix(ints_dt1),
closed = c(TRUE, FALSE))
ints2 <- intervals::Intervals_full(as.matrix(ints_dt2),
closed = c(TRUE, FALSE))
# reduce the intervals in each input by finding all the intervals that
# overlap at all with each other and combining them
common_ints <- unique(c(ints1, ints2))
overlap_mapping <- intervals::interval_overlap(
from = common_ints,
to = common_ints
)
while (any(sapply(overlap_mapping, length) > 1)) {
collapsed_ints_list <- lapply(1:length(overlap_mapping), function(i) {
intervals::interval_union(
from = common_ints[i],
to = common_ints[overlap_mapping[[i]]]
)
})
common_ints <- unique(Reduce(c, collapsed_ints_list))
overlap_mapping <- intervals::interval_overlap(
from = common_ints,
to = common_ints
)
}
# remove intervals that were missing from the original intervals
if (!include_missing) {
remove_intervals <- intervals::interval_union(
intervals::interval_complement(ints1),
intervals::interval_complement(ints2)
)
overlap_mapping <- intervals::interval_overlap(
from = common_ints,
to = remove_intervals
)
common_ints <- common_ints[sapply(overlap_mapping, length) == 0]
}
common_ints_dt <- data.table::as.data.table(common_ints)
data.table::setnames(common_ints_dt, c("start", "end"))
return(common_ints_dt)
}
# identify the most detailed common set of intervals
common_intervals <- Reduce(check_each_pair, intervals)
data.table::setnames(common_intervals, c("start", "end"), cols)
data.table::setkeyv(common_intervals, cols)
return(common_intervals)
}
#' @inheritParams collapse_common_intervals
#' @param common_intervals \[`data.table()`\]\cr
#' Common intervals returned by [`identify_common_intervals()`]
#'
#' @return [`identify_common_intervals()`] returns a \[`data.table()`\] with the
#' same columns and rows as originally in `dt`, with two additional columns
#' merged on from `common_intervals`. These new columns are called
#' 'common_start' and 'common_end' defining the most detailed common interval
#' each row maps to.
#'
#' @rdname helper_common_intervals
merge_common_intervals <- function(dt, common_intervals, col_stem) {
cols <- paste0(col_stem, "_", c("start", "end"))
data.table::setkeyv(common_intervals, c("common_start", "common_end"))
collapsed_dt <- data.table::foverlaps(
dt,
common_intervals,
by.x = cols,
by.y = c("common_start", "common_end")
)
collapsed_dt <- collapsed_dt[get(cols[1]) >= common_start & get(cols[2]) <= common_end]
return(collapsed_dt)
}