-
Notifications
You must be signed in to change notification settings - Fork 5
/
addDeath.R
179 lines (168 loc) · 5.1 KB
/
addDeath.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
# Copyright 2024 DARWIN EU (C)
#
# This file is part of PatientProfiles
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#' Add date of death for individuals. Only death within the same observation
#' period than `indexDate` will be observed.
#'
#' @param x Table with individuals in the cdm.
#' @param indexDate Variable in x that contains the window origin.
#' @param censorDate Name of a column to stop followup.
#' @param window window to consider events over.
#' @param deathDateName name of the new column to be added.
#' @param name Name of the new table, if NULL a temporary table is returned.
#'
#' @return table x with the added column with death information added.
#' @export
#'
#' @examples
#' \donttest{
#' cdm <- mockPatientProfiles()
#' cdm$cohort1 %>%
#' addDeathDate()
#' mockDisconnect(cdm = cdm)
#' }
#'
addDeathDate <- function(x,
indexDate = "cohort_start_date",
censorDate = NULL,
window = c(0, Inf),
deathDateName = "date_of_death",
name = NULL) {
addDeath(
x = x,
value = "date",
indexDate = indexDate,
censorDate = censorDate,
window = window,
deathName = deathDateName,
name = name
)
}
#' Add days to death for individuals. Only death within the same observation
#' period than `indexDate` will be observed.
#'
#' @param x Table with individuals in the cdm.
#' @param indexDate Variable in x that contains the window origin.
#' @param censorDate Name of a column to stop followup.
#' @param window window to consider events over.
#' @param deathDaysName name of the new column to be added.
#' @param name Name of the new table, if NULL a temporary table is returned.
#'
#' @return table x with the added column with death information added.
#' @export
#'
#' @examples
#' \donttest{
#' cdm <- mockPatientProfiles()
#' cdm$cohort1 %>%
#' addDeathDays()
#' mockDisconnect(cdm = cdm)
#' }
#'
addDeathDays <- function(x,
indexDate = "cohort_start_date",
censorDate = NULL,
window = c(0, Inf),
deathDaysName = "days_to_death",
name = NULL) {
addDeath(
x = x,
value = "days",
indexDate = indexDate,
censorDate = censorDate,
window = window,
deathName = deathDaysName,
name = name
)
}
#' Add flag for death for individuals. Only death within the same observation
#' period than `indexDate` will be observed.
#'
#' @param x Table with individuals in the cdm.
#' @param indexDate Variable in x that contains the window origin.
#' @param censorDate Name of a column to stop followup.
#' @param window window to consider events over.
#' @param deathFlagName name of the new column to be added.
#' @param name Name of the new table, if NULL a temporary table is returned.
#'
#' @return table x with the added column with death information added.
#' @export
#'
#' @examples
#' \donttest{
#' cdm <- mockPatientProfiles()
#' cdm$cohort1 %>%
#' addDeathFlag()
#' mockDisconnect(cdm = cdm)
#' }
#'
addDeathFlag <- function(x,
indexDate = "cohort_start_date",
censorDate = NULL,
window = c(0, Inf),
deathFlagName = "death",
name = NULL) {
addDeath(
x = x,
value = "flag",
indexDate = indexDate,
censorDate = censorDate,
window = window,
deathName = deathFlagName,
name = name
)
}
addDeath <- function(x,
value,
indexDate,
censorDate,
window,
deathName,
name) {
# input validation
cdm <- omopgenerics::cdmReference(x)
checkCdm(cdm, tables = "death")
if (!indexDate %in% colnames(x)) {
cli::cli_abort("{indexDate} variable not found in table")
}
if (!is.list(window)) {
window <- list(window)
}
if (length(window) != 1) {
cli::cli_abort("Only one time window can be provided")
}
checkWindow(window)
window <- purrr::list_c(window)
if (deathName %in% colnames(x)) {
cli::cli_warn("{deathName} variable already exists and will be overwritten")
x <- x |>
dplyr::select(!dplyr::all_of(deathName))
}
deathName <- checkSnakeCase(deathName)
x <- x |>
.addIntersect(
tableName = "death",
value = value,
indexDate = indexDate,
censorDate = censorDate,
window = window,
targetStartDate = "death_date",
targetEndDate = NULL,
order = "first",
nameStyle = deathName,
name = name
)
return(x)
}