-
Notifications
You must be signed in to change notification settings - Fork 7
/
check_ae_withdr_ds_discon.R
120 lines (99 loc) · 4.1 KB
/
check_ae_withdr_ds_discon.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
#' @title Check if an AE leading to drug being withdrawn is reflected in DS
#'
#' @description This checks that if there is an AE with AEACN="DRUG WITHDRAWN" then there should be a treatment discontinuation
#' record indicated by DS.DSSCAT
#'
#' @param AE Adverse Events SDTM dataset with variables USUBJID, AEACN
#' @param DS Disposition SDTM dataset with variables USUBJID, DSCAT, DSSCAT
#' @param TS Trial Summary SDTM dataset with variables TSPARMCD, TSVAL
#' @param preproc An optional company specific preprocessing script
#' @param ... Other arguments passed to methods
#'
#' @return boolean value if check failed or passed with 'msg' attribute if the
#' test failed
#'
#' @importFrom dplyr %>% filter select
#' @importFrom tidyselect any_of
#' @export
#'
#' @author Yuliia Bahatska
#'
#' @examples
#'
#' AE <- data.frame(
#' USUBJID = 1:6,
#' AEACN = c("DRUG WITHDRAWN",NA,NA,NA,NA,NA),
#' AETOXGR = c(NA,NA,NA,NA,"5",NA),
#' AEDECOD=c("NAUSEA","HEADACHE"),
#' AESPID = "FORMNAME-R:5/L:5XXXX"
#' )
#' DS <- data.frame(
#' USUBJID = 1:3,
#' DSCAT="DISPOSITION EVENT",
#' DSSCAT="STUDY TREATMENT",
#' DSDECOD=c("COMPLETED","ADVERSE EVENT","DEATH")
#' )
#'
#' TS <- data.frame(
#' TSPARMCD="TRT",
#' TSVAL="CHECK"
#' )
#'
#' check_ae_withdr_ds_discon(AE,DS,TS)
#' check_ae_withdr_ds_discon(AE,DS,TS,preproc=roche_derive_rave_row)
#'
#' DS$DSSCAT = NULL
#'
#' check_ae_withdr_ds_discon(AE,DS,TS)
#'
check_ae_withdr_ds_discon <- function(AE,DS,TS,preproc=identity,...){
###First check that required variables exist and return a message if they don't
if( AE %lacks_any% c("USUBJID","AEACN")){
fail(lacks_msg(AE, c("USUBJID","AEACN")))
}else if( DS %lacks_any% c("USUBJID","DSSCAT","DSCAT","DSDECOD")){
fail(lacks_msg(DS, c("USUBJID","DSSCAT","DSCAT","DSDECOD")))
} else if( TS %lacks_any% c("TSPARMCD","TSVAL")){
fail(lacks_msg(DS, c("TSPARMCD","TSVAL")))
}else{
#calculate number of drugs in the study
agent_num<-filter(TS,(TSPARMCD %in% c("TRT","COMPTRT" ))) %>% nrow()
#if a study is not single agent the check won't be executed
if (agent_num != 1)
{
fail("This check is only applicable for single agent studies, but based on TS domain this study is not single agent or study type cannot be determined. ")
}
#only run for single agent studies
else if (agent_num==1)
{
# in ae keep rows where the drug was withdrawn
#Apply company specific preprocessing function
AE = preproc(AE,...)
ae0 <- subset(AE,AE$AEACN=="DRUG WITHDRAWN",) %>%
select(any_of(c("USUBJID", "AEACN","AEDECOD","RAVE")))
# find matching patients in DS
DS = preproc(DS,...)
ds0 <- subset(DS, (DS$USUBJID %in% ae0$USUBJID) )
ds1 <- subset(ds0, (grepl("DISCON", toupper(ds0$DSSCAT)) | toupper(ds0$DSSCAT)=='TREATMENT COMPLETION/EARLY DISCONTINUATION' | toupper(ds0$DSSCAT)=='TREATMENT EARLY DISCONTINUATION/COMPLETION'
|(toupper(ds0$DSSCAT)=="STUDY TREATMENT")) & toupper(ds0$DSDECOD)!="COMPLETED" & grepl("DISPO", toupper(ds0$DSCAT)),) %>%
select(any_of(c("USUBJID", "DSSCAT", "DSCAT","RAVE")))
# check which patients have TREATMENT DISCON FORM
mydfprep <- merge(unique(ds1), ae0, "USUBJID", all.y=TRUE)
## to fix the following line to use the subset function
mydf <- subset(mydfprep, is_sas_na(mydfprep$DSSCAT))
rownames(mydf)=NULL
###Print to report
### Return message if no records with missing TREATMENT DISCON form (i.e., DS.DSSCAT includes "TREATMENT DISCON" and DS.DSCAT includes "DISPO")
if(nrow(mydf)==0){
pass()
### Return subset dataframe if there are records with missing TREATMENT DISCON
}else if(nrow(mydf)>0){
fail(
msg = paste("There are",
length(unique(mydf$USUBJID)),
"patient(s) where AE data treatment discontinuation but no treatment discontinuation record in DS. "),
data = mydf
)
}
}
}
}