generated from pharmaverse/admiraltemplate
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ad_adce.R
163 lines (135 loc) · 4.15 KB
/
ad_adce.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
# Name: ADCE
#
# Label: Clinical Event Analysis Dataset
#
# Input: CE, ADSL, VS
library(admiral)
library(dplyr)
library(lubridate)
library(admiralvaccine)
library(pharmaversesdtm)
# Load source datasets
# Use e.g. haven::read_sas to read in .sas7bdat, or other suitable functions
# as needed and assign to the variables below.
# For illustration purposes read in admiral test data
data("ce_vaccine")
data("admiralvaccine_adsl")
adsl <- admiralvaccine_adsl
ce <- ce_vaccine
# When SAS datasets are imported into R using haven::read_sas(), missing
# character values from SAS appear as "" characters in R, instead of appearing
# as NA values. Further details can be obtained via the following link:
# https://pharmaverse.github.io/admiral/articles/admiral.html#handling-of-missing-values # nolint
ce <- convert_blanks_to_na(ce)
adsl <- convert_blanks_to_na(adsl)
# Derivations
# Get CE records
adce01 <- ce %>%
filter(CECAT == "REACTOGENICITY")
# Get list of ADSL vars required for derivations
adsl_vars <- exprs(TRTSDT, TRTEDT)
# Create period dataset - for joining period information onto CE records
# Need to remove datetime variables as otherwise causes duplicate issues
adsl2 <- adsl %>%
select(-c(starts_with("AP") & ends_with("DTM")))
adperiods <- create_period_dataset(
adsl2,
new_vars = exprs(APERSDT = APxxSDT, APEREDT = APxxEDT)
)
# Derive analysis dates/days
adce02 <- adce01 %>%
# join ADSL to CE
derive_vars_merged(
dataset_add = adsl,
new_vars = adsl_vars,
by = exprs(STUDYID, USUBJID)
) %>%
## Derive analysis start time
## Proposed imputations depending on situation: no needed -> highest imputation = “n”
## some missing dates: highest imputation = “D”
derive_vars_dt(
dtc = CESTDTC,
new_vars_prefix = "AST",
highest_imputation = "n"
) %>%
## Derive analysis end time
derive_vars_dt(
dtc = CEENDTC,
new_vars_prefix = "AEN",
highest_imputation = "n"
) %>%
## Derive analysis start relative day and analysis end relative day
derive_vars_dy(
reference_date = TRTSDT,
source_vars = exprs(ASTDT, AENDT)
)
adce03 <-
derive_vars_joined(
adce02,
dataset_add = adperiods,
by_vars = exprs(STUDYID, USUBJID),
filter_join = ASTDT >= APERSDT & ASTDT <= APEREDT,
join_type = "all"
) %>%
mutate(
APERSTDY = as.integer(ASTDT - APERSDT) + 1,
AREL = CEREL
)
adce04 <- adce03 %>%
## depending on collection of TOXGR or SEV in CE domain
## Analysis variant of ASEV and ASEVN
mutate(
ASEV = CESEV,
ASEVN = as.integer(factor(ASEV,
levels = c("MILD", "MODERATE", "SEVERE", "DEATH THREATENING")
))
) %>%
## Derive occurrence flags: first occurrence of most severe solicited AE
## - Company specific
restrict_derivation(
derivation = derive_var_extreme_flag,
args = params(
by_vars = exprs(USUBJID, APERIOD),
order = exprs(desc(ASEVN), ASTDY, CEDECOD, CESEQ),
new_var = AOCC01FL,
mode = "first"
),
filter = !is.na(APERIOD) & !is.na(ASEV)
)
adce05 <- adce04 %>%
## Derive ASEQ
derive_var_obs_number(
new_var = ASEQ,
by_vars = exprs(STUDYID, USUBJID),
order = exprs(CEDECOD, CELAT, CETPTREF, APERIOD),
check_type = "error"
) %>%
## Derive analysis duration (value and unit)
derive_vars_duration(
new_var = ADURN,
new_var_unit = ADURU,
start_date = ASTDT,
end_date = AENDT,
in_unit = "days",
out_unit = "days",
add_one = TRUE,
trunc_out = FALSE
)
# Get list of ADSL vars, list is trial specific and needs to be adjusted when using the template
adsl_list <- adsl %>%
select(STUDYID, USUBJID, TRT01A, TRT01P, AGE, AGEU, SEX, RACE, COUNTRY, ETHNIC, SITEID, SUBJID)
# Join ADSL_list with CE
adce <- adce05 %>%
derive_vars_merged(
dataset_add = adsl_list,
by_vars = exprs(STUDYID, USUBJID)
)
admiralvaccine_adce <- adce
# Save output ----
dir <- tools::R_user_dir("admiralvaccine_templates_data", which = "cache")
# Change to whichever directory you want to save the dataset in
if (!file.exists(dir)) {
# Create the folder
dir.create(dir, recursive = TRUE, showWarnings = FALSE)
}
save(admiralvaccine_adce, file = file.path(dir, "adce.rda"), compress = "bzip2")