-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest-codelist.R
More file actions
286 lines (251 loc) · 8.16 KB
/
test-codelist.R
File metadata and controls
286 lines (251 loc) · 8.16 KB
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
# Suppress cli output during testing
options(cli.default_handler = function(...) {})
spec <- metacore::spec_to_metacore(metacore::metacore_example("p21_mock.xlsx"), quiet = TRUE)
dm_spec <- select_dataset(spec, "DM", quiet = TRUE)
load(metacore::metacore_example("pilot_ADaM.rda"))
adsl_spec <- metacore %>% select_dataset("ADSL", quiet = TRUE)
dm <- haven::read_xpt(metatools_example("dm.xpt"))
test_that("create_subgrps", {
expect_equal(
create_subgrps(c(1:10), c("<2", "2-5", ">5")),
c("<2", "2-5", "2-5", "2-5", "2-5", ">5", ">5", ">5", ">5", ">5")
)
expect_equal(
create_subgrps(c(1:10), c("<=2", ">2-5", ">5")),
c("<=2", "<=2", ">2-5", ">2-5", ">2-5", ">5", ">5", ">5", ">5", ">5")
)
expect_equal(
create_subgrps(c(1:10), c("<2", "2-<5", ">=5")),
c("<2", "2-<5", "2-<5", "2-<5", ">=5", ">=5", ">=5", ">=5", ">=5", ">=5")
)
expect_equal(
create_subgrps(c(1:10, NA), c("<2", "2-5", ">5")),
c("<2", "2-5", "2-5", "2-5", "2-5", ">5", ">5", ">5", ">5", ">5", NA)
)
})
test_that("create_var_from_codelist", {
data <- tibble::tribble(
~USUBJID, ~VAR1, ~VAR2,
1, "M", "Male",
2, "F", "Female",
3, "F", "Female",
4, "U", "Unknown",
5, "M", "Male",
)
manual_data <- data %>%
mutate(SEX = VAR1)
expect_equal(create_var_from_codelist(data, dm_spec, VAR2, SEX), manual_data)
expect_equal(create_var_from_codelist(data, dm_spec, "VAR2", "SEX"), manual_data)
manual_data2 <- data %>%
mutate(SEX = VAR2)
expect_equal(
create_var_from_codelist(data, dm_spec, VAR1, SEX, decode_to_code = FALSE),
manual_data2
)
# Test numeric
num_out <- dm %>%
filter(ARMCD != "Scrnfail") %>%
mutate(TRT01P = ARM) %>%
select(TRT01P, ARMCD) %>%
create_var_from_codelist(adsl_spec, TRT01P, TRT01PN) %>%
head() %>%
pull(TRT01PN)
expect_equal(num_out, c(0, 0, 81, 54, 81, 0))
# Test provide custom codelist
load(metacore::metacore_example("pilot_ADaM.rda"))
adlb_spec <- metacore::select_dataset(metacore, "ADLBC", quiet = TRUE)
data <- tibble::tibble(
PARAMCD = c("ALB", "ALP", "ALT", "DUMMY", "DUMMY2")
)
compare <- tibble::tibble(
PARAMCD = c("ALB", "ALP", "ALT", "DUMMY", "DUMMY2"),
PARAM = c("Albumin (g/L)", "Alkaline Phosphatase (U/L)", "Alanine Aminotransferase (U/L)", NA, NA)
)
create_var_from_codelist(
data = data,
metacore = adlb_spec,
input_var = PARAMCD,
out_var = PARAM,
codelist = get_control_term(adlb_spec, PARAMCD),
decode_to_code = FALSE,
strict = FALSE
) |>
select(PARAMCD, PARAM) |>
expect_equal(compare)
# Test character variable warning where arg `strict == TRUE` / single issue case
create_var_from_codelist(
data = data,
metacore = adlb_spec,
input_var = PARAMCD,
out_var = PARAM,
codelist = get_control_term(adlb_spec, PARAMCD),
decode_to_code = FALSE,
strict = TRUE
) |>
expect_warning()
# Test character variable warning where arg `strict == TRUE` / multiple issue case
data <- tibble::tibble(
PARAMCD = c("ALB", "ALP", "ALT", "DUMMY", "DUMMY2")
)
create_var_from_codelist(
data = data,
metacore = adlb_spec,
input_var = PARAMCD,
out_var = PARAM,
codelist = get_control_term(adlb_spec, PARAMCD),
decode_to_code = FALSE,
strict = TRUE
) |>
expect_warning()
# Test numeric variable used as input_var (strict == FALSE)
data2 <- tibble::tibble(
PARAMN = c(18, 19, 20, 99)
)
compare2 <- tibble::tibble(
PARAMN = c(18, 19, 20, 99),
PARAM = c("Sodium (mmol/L)", "Potassium (mmol/L)", "Chloride (mmol/L)", NA)
)
create_var_from_codelist(
data = data2,
metacore = adlb_spec,
input_var = PARAMN,
out_var = PARAM,
codelist = get_control_term(adlb_spec, PARAMN),
decode_to_code = FALSE,
strict = FALSE
) |>
select(PARAMN, PARAM) |>
expect_equal(compare2)
# Test numeric variable used as input_var / single issue case (strict == TRUE)
create_var_from_codelist(
data = data2,
metacore = adlb_spec,
input_var = PARAMN,
out_var = PARAM,
codelist = get_control_term(adlb_spec, PARAMN),
decode_to_code = FALSE,
strict = TRUE
) |>
expect_warning()
# Test numeric variable used as input_var / multiple issue case (strict == TRUE)
data3 <- tibble::tibble(
PARAMN = c(18, 19, 20, 99, 999)
)
create_var_from_codelist(
data = data3,
metacore = adlb_spec,
input_var = PARAMN,
out_var = PARAM,
codelist = get_control_term(adlb_spec, PARAMN),
decode_to_code = FALSE,
strict = TRUE
) |>
expect_warning()
# Test for Variable not in specs
expect_error(create_var_from_codelist(data, spec, VAR2, FOO))
})
test_that("create_cat_var", {
# Create manual dataset to check against
man_dat <- tibble::tribble(
~AGEGR1, ~n,
"65-80", 172,
"<65", 42,
">80", 92,
)
man_dat_labs <- tibble::tribble(
~AGEGR2, ~n,
"18-64 years", 42,
"65-80 years", 172,
">80 years", 92,
)
# Grouping col only
auto_dat <- create_cat_var(dm, adsl_spec, AGE, AGEGR1) %>%
group_by(AGEGR1) %>%
dplyr::summarise(n = dplyr::n())
expect_equal(auto_dat, man_dat)
# Grouping Column and Numeric Decode
grp_num_dat <- create_cat_var(dm, adsl_spec, AGE, AGEGR1, AGEGR1N)
grp_num_dat %>%
group_by(AGEGR1) %>%
dplyr::summarise(n = dplyr::n()) %>%
expect_equal(auto_dat)
grp_num_dat %>%
pull(AGEGR1N) %>%
unique() %>%
expect_equal(c(1:3))
# Grouping column and numeric decode, build from decode == TRUE
decode_num_dat <- create_cat_var(dm, adsl_spec, AGE, AGEGR2, AGEGR2N, TRUE)
decode_num_dat %>%
group_by(AGEGR2) %>%
dplyr::summarise(n = dplyr::n()) %>%
expect_equal(man_dat_labs)
decode_num_dat %>%
pull(AGEGR2N) %>%
unique() %>%
expect_equal(c(1:3))
# Test error 'unable to decipher group definition'
bad_ct <- adsl_spec$codelist |>
filter(name == "AGEGR1") |>
pull(codes) |>
purrr::pluck(1) |>
tibble::add_row(code = "DUMMY", decode = "DUMMY")
codelist <- adsl_spec$codelist |> filter(name == "AGEGR1")
codelist$codes <- list(bad_ct)
spec2 <- suppressWarnings(metacore::metacore(
adsl_spec$ds_spec,
adsl_spec$ds_vars,
adsl_spec$var_spec,
adsl_spec$value_spec,
adsl_spec$derivations,
codelist = codelist,
supp = adsl_spec$supp
)) %>%
select_dataset("ADSL", quiet = TRUE)
create_cat_var(dm, spec2, AGE, AGEGR1, AGEGR1N, TRUE) |>
expect_error("Unable to decipher the following group definition: DUMMY. Please check your controlled terminology.")
# Test error 'group definitions are not exclusive'
bad_ct <- adsl_spec$codelist |>
filter(name == "AGEGR1") |>
pull(codes) |>
purrr::pluck(1) |>
tibble::add_row(code = "18-64", decode = "18-64 years")
codelist <- adsl_spec$codelist |> filter(name == "AGEGR1")
codelist$codes <- list(bad_ct)
spec2 <- suppressWarnings(metacore::metacore(
adsl_spec$ds_spec,
adsl_spec$ds_vars,
adsl_spec$var_spec,
adsl_spec$value_spec,
adsl_spec$derivations,
codelist = codelist,
supp = adsl_spec$supp
)) %>%
select_dataset("ADSL", quiet = TRUE)
create_cat_var(dm, spec2, AGE, AGEGR1, AGEGR1N, create_from_decode = TRUE) |>
expect_error("Group definitions are not exclusive. Please check your controlled terminology")
# Test error 'value exists that is not defined in controlled terminology
dm2 <- dm |>
tibble::add_row(AGE = 15) |>
tibble::add_row(AGE = 16)
x <- create_cat_var(dm2, adsl_spec, AGE, AGEGR2, create_from_decode = TRUE) |>
expect_warning()
# Test errors
expect_error(create_cat_var(dm, spec, AGE, ARM))
})
test_that("convert_var_to_fct", {
# Codelist variable
convert_var_to_fct(dm, dm_spec, SEX) %>%
pull(SEX) %>%
levels() %>%
expect_equal(c("F", "M", "U"))
# Param list variable
convert_var_to_fct(dm, dm_spec, ARM) %>%
pull(ARM) %>%
levels() %>%
expect_equal(c("Screen Failure", "Placebo", "Xanomeline Low Dose", "Xanomeline High Dose"))
# Errors
# Note although AELLT isn't in DM it will fail because it is an
# external lib before it fails for not being the dataset
expect_error(convert_var_to_fct(dm, dm_spec, AELLT))
expect_error(convert_var_to_fct(dm, dm_spec, FOO))
})