Skip to content

Commit 0383b2e

Browse files
authored
Merge pull request #87 from pharmaverse/85_ADAE_example
closes #85 adae example
2 parents b1135bd + 45e05a1 commit 0383b2e

File tree

3 files changed

+480
-0
lines changed

3 files changed

+480
-0
lines changed

adam/adae.R

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
## ----r setup, message=FALSE, warning=FALSE, results='hold'--------------------
2+
library(metacore)
3+
library(metatools)
4+
library(pharmaversesdtm)
5+
library(pharmaverseadam)
6+
library(admiral)
7+
library(xportr)
8+
library(dplyr)
9+
library(lubridate)
10+
library(stringr)
11+
library(reactable)
12+
13+
# Read in input data
14+
adsl <- pharmaverseadam::adsl
15+
ae <- pharmaversesdtm::ae
16+
ex <- pharmaversesdtm::ex
17+
18+
# When SAS datasets are imported into R using haven::read_sas(), missing
19+
# character values from SAS appear as "" characters in R, instead of appearing
20+
# as NA values. Further details can be obtained via the following link:
21+
# https://pharmaverse.github.io/admiral/articles/admiral.html#handling-of-missing-values
22+
ae <- convert_blanks_to_na(ae)
23+
ex <- convert_blanks_to_na(ex)
24+
25+
## ----r echo=TRUE--------------------------------------------------------------
26+
# ---- Load Specs for Metacore ----
27+
metacore <- spec_to_metacore(
28+
path = "./metadata/safety_specs.xlsx",
29+
# All datasets are described in the same sheet
30+
where_sep_sheet = FALSE
31+
) %>%
32+
select_dataset("ADAE")
33+
34+
## ----r------------------------------------------------------------------------
35+
# Select required ADSL variables
36+
adsl_vars <- exprs(TRTSDT, TRTEDT, DTHDT)
37+
38+
# Join ADSL variables with VS
39+
adae <- ae %>%
40+
derive_vars_merged(
41+
dataset_add = adsl,
42+
new_vars = adsl_vars,
43+
by_vars = exprs(STUDYID, USUBJID)
44+
)
45+
46+
## ----r------------------------------------------------------------------------
47+
# Derive ASTDT/ASTDTF/ASTDY and AENDT/AENDTF/AENDY
48+
adae <- adae %>%
49+
derive_vars_dt(
50+
new_vars_prefix = "AEN",
51+
dtc = AEENDTC,
52+
date_imputation = "last",
53+
highest_imputation = "M", # imputation is performed on missing days or months
54+
flag_imputation = "auto" # to automatically create AENDTF variable
55+
) %>%
56+
derive_vars_dt(
57+
new_vars_prefix = "AST",
58+
dtc = AESTDTC,
59+
highest_imputation = "M", # imputation is performed on missing days or months
60+
flag_imputation = "auto", # to automatically create ASTDTF variable
61+
min_dates = exprs(TRTSDT), # apply a minimum date for the imputation
62+
max_dates = exprs(AENDT) # apply a maximum date for the imputation
63+
) %>%
64+
derive_vars_dy(
65+
reference_date = TRTSDT,
66+
source_vars = exprs(ASTDT, AENDT)
67+
)
68+
69+
## ----r------------------------------------------------------------------------
70+
# Derive ADURN/ADURU
71+
adae <- adae %>%
72+
derive_vars_duration(
73+
new_var = ADURN,
74+
new_var_unit = ADURU,
75+
start_date = ASTDT,
76+
end_date = AENDT
77+
)
78+
79+
## ----r------------------------------------------------------------------------
80+
# Derive LDOSEDT
81+
# In our ex data the EXDOSFRQ (frequency) is "QD" which stands for once daily
82+
# If this was not the case then we would need to use the admiral::create_single_dose_dataset() function
83+
# to generate single doses from aggregate dose information
84+
# Refer to https://pharmaverse.github.io/admiral/reference/create_single_dose_dataset.html
85+
ex <- ex %>%
86+
derive_vars_dt(
87+
dtc = EXENDTC,
88+
new_vars_prefix = "EXEN"
89+
)
90+
91+
adae <- adae %>%
92+
derive_vars_joined(
93+
dataset_add = ex,
94+
by_vars = exprs(STUDYID, USUBJID),
95+
order = exprs(EXENDT),
96+
new_vars = exprs(LDOSEDT = EXENDT),
97+
join_vars = exprs(EXENDT),
98+
join_type = "all",
99+
filter_add = (EXDOSE > 0 | (EXDOSE == 0 & str_detect(EXTRT, "PLACEBO"))) & !is.na(EXENDT),
100+
filter_join = EXENDT <= ASTDT,
101+
mode = "last"
102+
)
103+
104+
## ----r------------------------------------------------------------------------
105+
# Derive TRTEMFL and ONTRTFL
106+
adae <- adae %>%
107+
derive_var_trtemfl(
108+
start_date = ASTDT,
109+
end_date = AENDT,
110+
trt_start_date = TRTSDT,
111+
trt_end_date = TRTEDT
112+
) %>%
113+
derive_var_ontrtfl(
114+
start_date = ASTDT,
115+
ref_start_date = TRTSDT,
116+
ref_end_date = TRTEDT,
117+
ref_end_window = 30
118+
)
119+
120+
## ----r------------------------------------------------------------------------
121+
# Derive AOCCIFL
122+
adae <- adae %>%
123+
# create temporary numeric ASEVN for sorting purpose
124+
mutate(TEMP_AESEVN = as.integer(factor(AESEV, levels = c("SEVERE", "MODERATE", "MILD")))) %>%
125+
derive_var_extreme_flag(
126+
new_var = AOCCIFL,
127+
by_vars = exprs(STUDYID, USUBJID),
128+
order = exprs(TEMP_AESEVN, ASTDT, AESEQ),
129+
mode = "first"
130+
)
131+
132+
## ----r------------------------------------------------------------------------
133+
queries <- admiral::queries %>%
134+
filter(PREFIX %in% c("CQ01", "SMQ02"))
135+
136+
## ----r------------------------------------------------------------------------
137+
# Derive CQ01NAM and SMQ02NAM
138+
adae <- adae %>%
139+
derive_vars_query(dataset_queries = queries)
140+
141+
## ----r eval=TRUE--------------------------------------------------------------
142+
adae <- adae %>%
143+
derive_vars_merged(
144+
dataset_add = select(adsl, !!!negate_vars(adsl_vars)),
145+
by_vars = exprs(STUDYID, USUBJID)
146+
)
147+
148+
## ----r checks, warning=FALSE, message=FALSE-----------------------------------
149+
dir <- tempdir() # Specify the directory for saving the XPT file
150+
151+
adae %>%
152+
drop_unspec_vars(metacore) %>% # Drop unspecified variables from specs
153+
check_variables(metacore) %>% # Check all variables specified are present and no more
154+
check_ct_data(metacore, na_acceptable = TRUE) %>% # Checks all variables with CT only contain values within the CT
155+
order_cols(metacore) %>% # Orders the columns according to the spec
156+
sort_by_key(metacore) %>% # Sorts the rows by the sort keys
157+
xportr_type(metacore, domain = "ADAE") %>% # Coerce variable type to match spec
158+
xportr_length(metacore) %>% # Assigns SAS length from a variable level metadata
159+
xportr_label(metacore) %>% # Assigns variable label from metacore specifications
160+
xportr_df_label(metacore) %>% # Assigns dataset label from metacore specifications
161+
xportr_write(file.path(dir, "adae.xpt"), metadata = metacore, domain = "ADAE")
162+

0 commit comments

Comments
 (0)