Commit fa4e5f6e authored by Jonathan Broder's avatar Jonathan Broder
Browse files

GetEndpointsXT function and documentation

parent a7902625
#' Fetch endpoint summary data
#'
#' Obtain all endpoints from both AE and EN events
#'
#' @param con a database connection using the DBI package or \code{ASPREEDb()}
#' @param sectionPaths a list mapping section names, e.g. SectionA1, to a file path
#' @return A summary data frame of all endpoints coded in ASPREE or XT
#' @examples
#' endpoints <- con %>% GetEndpointsXT()
GetEndpointsXT <- function(con, sectionPaths = SDrivePaths())
{detoo
# Library packages
remotes::update_packages("rlang", ask=FALSE)
packages <- c("RODBC", "tidyverse", "DBI", "readstata13", "lubridate")
# load packages from library
lapply(packages, library, character.only=TRUE)
# Data from database
con <- dbConnect(odbc::odbc(), "ASPREE Data Live")
Endpoint <- DBI::dbReadTable(con, "vw_eac_SummaryData")
Death <- dbReadTable(con, "eac_Death")
Death_oth_trac_desc <- dbReadTable(con, "eac_tlkpDeathTrajectoryOther")
Death_mode_desc <- dbReadTable(con, "eac_tlkpDeathMode")
Pt_med_hist <- dbReadTable(con, "tblPtMedHistory")
Bleeding <- dbReadTable(con, "eac_ClinicalSignificantBleed")
EAC_desc <- dbReadTable(con, "tlkpEACLookups")
Stroke <- dbReadTable(con, "eac_Stroke")
Stroke_desc <- dbReadTable(con, "eac_tlkpStrokeBestDesc")
vw_eac_SummaryData <- dbReadTable(con, "vw_eac_SummaryData")
# Add other trajectory to death from EN events
temp <- Death %>%
left_join(Death_oth_trac_desc, by = c("TrajectoryOther" = "Code")) %>%
arrange(CodeId, CreatedDt) %>%
group_by(CodeId) %>%
mutate(Adj = row_number(),
Adj = ifelse(Adj == 1, "Adj1Death",
ifelse(Adj == 2, "Adj2Death", "Adj3Death"))) %>%
select(CodeId, Description, Adj) %>%
spread(Adj, Description) %>%
mutate(Adj1Death = ifelse(is.na(Adj1Death), Adj2Death, Adj1Death)) %>%
select(CodeId, Adj1Death, Adj3Death)
Endpoint <- Endpoint %>%
left_join(temp, by = c("CodeID" = "CodeId"))
# Add death mode for EN events
temp <- Death %>%
left_join(Death_mode_desc, by = c("Mode" = "Code")) %>%
arrange(CodeId, CreatedDt) %>%
group_by(CodeId) %>%
mutate(Adj = row_number(),
Adj = ifelse(Adj == 1, "Adj1DeathMode",
ifelse(Adj == 2, "Adj2DeathMode", "Adj3DeathMode"))) %>%
select(CodeId, Description, Adj) %>%
spread(Adj, Description) %>%
mutate(Adj1DeathMode = ifelse(is.na(Adj1DeathMode), Adj2DeathMode, Adj1DeathMode)) %>%
select(CodeId, Adj1DeathMode, Adj3DeathMode)
Endpoint <- Endpoint %>%
left_join(temp, by = c("CodeID" = "CodeId"))
# Add hemorragic stroke subtypes from AE events
temp <- Stroke %>%
arrange(CodeId, CreatedDt) %>%
group_by(CodeId) %>%
mutate(Adj = row_number(),
Adj = ifelse(Adj == 1, "Adj1Hem",
ifelse(Adj == 2, "Adj2Hem", "Adj3Hem"))) %>%
select(CodeId, Haemorrhagic, Adj) %>%
spread(Adj, Haemorrhagic) %>%
mutate(Adj1Hem = ifelse(is.na(Adj1Hem), Adj2Hem, Adj1Hem)) %>% # prioritise the first adj over the second adj, only use the second adj if the first is missing
select(CodeId, Adj1Hem, Adj3Hem) # prioritise the first adj over the second adj
Endpoint <- Endpoint %>%
left_join(temp, by = c("CodeID" = "CodeId"))
# Add stroke sybtype from EN events (both ischemic and hemorragic strokes)
temp <- Stroke %>%
left_join(Stroke_desc, by = c("BestDesc" = "Code")) %>%
arrange(CodeId, CreatedDt) %>%
group_by(CodeId) %>%
mutate(Adj = row_number(),
Adj = ifelse(Adj == 1, "Adj1StrokeEN",
ifelse(Adj == 2, "Adj2StrokeEN", "Adj3StrokeEN"))) %>%
select(CodeId, Description, Adj) %>%
spread(Adj, Description) %>%
select(CodeId, Adj1StrokeEN, Adj2StrokeEN, Adj3StrokeEN)
Endpoint <- Endpoint %>%
left_join(temp, by = c("CodeID" = "CodeId"))
# Select variables
Endpoint <- Endpoint[, c("SubjectID", "EndpointDate", "DetectedDt",
"AdjStatus", "AdjStatusDesc", "AdjSubStatusDesc", "CodeID",
"DMCEndpointType", "CommitteeDesc", "AdjOutcomeDesc",
"AdjSubClass", "AdjSubClassDesc", "CancerType", "CancerTypeDesc", "EnNum",
"Adj1_ISST", "Adj3_ISST", "Adj1Hem", "Adj3Hem", "Adj1StrokeEN", "Adj2StrokeEN", "Adj3StrokeEN", "Adj1Death", "Adj3Death", "Adj1DeathMode", "Adj3DeathMode")]
# EventType
Endpoint$EventType <- NA
Endpoint$EventType[which(Endpoint$DMCEndpointType == 5)] <- 1
Endpoint$EventType[which(Endpoint$DMCEndpointType == 4)] <- 2
Endpoint$EventType[which(Endpoint$DMCEndpointType == 2)] <- 3
Endpoint$EventType[which(Endpoint$DMCEndpointType == 6)] <- 4
Endpoint$EventType[which(Endpoint$DMCEndpointType == 99)] <- 5
Endpoint$EventType[which(Endpoint$DMCEndpointType == 8)] <- 6
Endpoint$EventType[which(Endpoint$DMCEndpointType == 20)] <- 7
Endpoint$EventType[which(Endpoint$DMCEndpointType == 21)] <- 8
Endpoint$EventType[which(Endpoint$DMCEndpointType == 22)] <- 9
Endpoint$EventType[which(Endpoint$DMCEndpointType == 23)] <- 10
Endpoint$EventType[which(Endpoint$DMCEndpointType == 9)] <- 11
Endpoint$EventType[which(Endpoint$DMCEndpointType == 2009)] <- 12
Endpoint$EventType[which(Endpoint$DMCEndpointType == 3)] <- 13
# AdjOutcome
Endpoint$AdjOutcome <- NA
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Admission to care physical disability endpoint reached")] <- 1
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Cancer Related Death")] <- 2
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Cancer related death ")] <- 2
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Cardiovascular Death")] <- 3
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Cardiovascular Related Death")] <- 3
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Clinically Significant Bleeding Death")] <- 4
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Clinically Significant Bleeding Related Death")] <- 4
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Clinically Significant Bleeding Endpoint ")] <- 5
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Coronary Heart Disease Death ")] <- 6
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Coronary Heart Disease Death - cardiac failure")] <- 6 # added for EN events
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Coronary Heart Disease Death - myocardical infarction")] <- 6 # added for EN events
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Coronary Heart Disease Death - other coronary death")] <- 6 # added for EN events
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Coronary Heart Disease Death - sudden cardiac death")] <- 6 # added for EN events
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Coronary Heart Disease Death - rapid cardiac death")] <- 6 # added for EN events
Endpoint$AdjOutcome[which(Endpoint$DMCEndpointType == 99 & is.na(Endpoint$AdjOutcomeDesc) & is.na(Endpoint$EnNum))] <- 7 # added for AE and EN events
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Dementia Endpoint ")] <- 8
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Depression (CES-D; Incident)")] <- 9
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Depression (CES-D; Persistent)")] <- 10
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Depression (CES-D; Recurrent)")] <- 11
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Hemorrhagic Stroke")] <- 12
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Hospitalisation for Heart Failure Endpoint ")] <- 13
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Hospitalisation for depression endpoint")] <- 14
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Ischemic Stroke")] <- 15
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Ischemic Stroke with hemorrhagic transformation")] <- 16 # not present for EN events
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "MI Endpoint (Acute, Recent or Evolving MI)")] <- 17
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "MI Endpoint (Established MI)")] <- 18
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Metastatic Cancer Endpoint ")] <- 19
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Non-Metastatic Cancer Endpoint")] <- 20
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Not Cancer Endpoint - ASPREE criteria not met")] <- 21
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Not Cancer Endpoint - Cancer not present")] <- 22
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Not Cancer Endpoint - Carcinoma in Situ")] <- 23
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Not Clinically Significant Bleeding Endpoint ")] <- 24
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Not Dementia Endpoint - Reason discordant")] <- 25
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Not Dementia Endpoint - DSM-iv criteria not satisfied")] <- 26
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Not Dementia Endpoint - Insufficient data")] <- 27
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Not Hospitalisation for Heart Failure Endpoint ")] <- 28
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Not MI Endpoint - ASPREE criteria not met")] <- 29
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Not MI Endpoint - No MI")] <- 30
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Not depression endpoint - not principal diagnosis")] <- 31
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Not depression endpoints - depression not present")] <- 32
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Not physical disability endpoint- insufficient documentation to confirm physical disability")] <- 33
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Not physical disability endpoint- no persistent physical disability present")] <- 34
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Not stroke endpoint - event does not meet stroke criteria (NO intra-cerebral bleed)")] <- 35
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Other death")] <- 36
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Other Death")] <- 36
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Physical Disability (persistent loss of Katz ADL)")] <- 37
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Stroke Death ")] <- 38
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Stroke Related Death - unknown stroke type")] <- 38 # added for EN events
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Hemorrhagic Stroke Related Death")] <- 38 # added for EN events
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Ischemic Stroke Related Death")] <- 38 # added for EN events
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Stroke type uncertain")] <- 39
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Sub-arachnoid hemorrhage Stroke")] <- 40
Endpoint$AdjOutcome[which(Endpoint$AdjOutcomeDesc == "Hematological Cancer Endpoint")] <- 42
# AdjOutcomeSubClass
Endpoint$AdjOutcomeSubClass <- NA
Endpoint$AdjOutcomeSubClass[which(Endpoint$CancerType == 1)] <- 1
Endpoint$AdjOutcomeSubClass[which(Endpoint$CancerType == 2)] <- 2
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjOutcomeDesc=="Hematological Cancer Endpoint")] <- 2 # added for EN events
Endpoint$AdjOutcomeSubClass[which(Endpoint$CancerType == 3)] <- 3
Endpoint$AdjOutcomeSubClass[which(Endpoint$CancerType == 4)] <- 4
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjSubClassDesc == "Cardiac Failure")] <- 5
Endpoint[which(Endpoint$AdjOutcomeDesc == "Coronary Heart Disease Death - cardiac failure"), c("AdjOutcomeSubClass")] <- 5 # added for EN events
Endpoint$AdjOutcomeSubClass[which(Endpoint$CancerType == 5)] <- 6
Endpoint$AdjOutcomeSubClass[which(Endpoint$CancerType == 6)] <- 7
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjSubClassDesc == "Intraventricular hemorrhage (Non- traumatic)")] <- 8
Endpoint$AdjOutcomeSubClass[which((Endpoint$AdjSubClass == 4) & (Endpoint$AdjOutcome == 5))] <- 8
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjSubClassDesc == "Extradural hemorrhage (Traumatic)")] <- 9
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjSubClassDesc == "GI (Lower) ")] <- 10
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjSubClassDesc == "GI (Upper) ")] <- 11
Endpoint$AdjOutcomeSubClass[which(Endpoint$CancerType == 7)] <- 12
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjSubClassDesc == "Intraventricular hemorrhage (Non- traumatic)")] <- 14
Endpoint$AdjOutcomeSubClass[which(Endpoint$CancerType == 8)] <- 15
Endpoint$AdjOutcomeSubClass[which(Endpoint$CancerType == 9)] <- 16
Endpoint$AdjOutcomeSubClass[which(Endpoint$CancerType == 10)] <- 17
Endpoint$AdjOutcomeSubClass[which(Endpoint$CancerType == 11)] <- 18
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjSubClassDesc == "Myocardial Infarction ")] <- 19
Endpoint[which(Endpoint$AdjOutcomeDesc == "Coronary Heart Disease Death - myocardical infarction"), c("AdjOutcomeSubClass")] <- 19 # added for EN events
Endpoint$AdjOutcomeSubClass[which((Endpoint$AdjSubClass == 5) & (Endpoint$AdjOutcome == 5))] <- 20
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjSubClassDesc == "Other Coronary Death " & Endpoint$AdjOutcomeDesc == "Coronary Heart Disease Death ")] <- 21
Endpoint[which(Endpoint$AdjOutcomeDesc == "Coronary Heart Disease Death - other coronary death"), c("AdjOutcomeSubClass")] <- 21 # added for EN events
Endpoint$AdjOutcomeSubClass[which(Endpoint$CancerType == 12)] <- 22
Endpoint$AdjOutcomeSubClass[which(Endpoint$CancerType == 13)] <- 23
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjSubClassDesc == "Parenchymal hematoma (Non- traumatic)")] <- 24
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjSubClassDesc == "Parenchymal hematoma (Traumatic)")] <- 25
Endpoint$AdjOutcomeSubClass[which(Endpoint$CancerType == 14)] <- 26
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjSubClassDesc == "Rapid Cardiac Death" & Endpoint$AdjOutcomeDesc == "Coronary Heart Disease Death ")] <- 27
Endpoint[which(Endpoint$AdjOutcomeDesc == "Coronary Heart Disease Death - rapid cardiac death"), c("AdjOutcomeSubClass")] <- 27 # added for aspre
Endpoint$AdjOutcomeSubClass[which(Endpoint$CancerType == 15)] <- 28
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjSubClassDesc == "Gastric/Gastroesophageal")] <- 28 # added for EN events, but this is for Gastric/Gastroesophageal
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjSubClassDesc == "Subarachnoid hemorrhage (Non- traumatic)")] <- 29
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjSubClassDesc == "Subarachnoid hemorrhage (Traumatic)")] <- 30
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjSubClassDesc == "Subdural hemorrhage (Non- traumatic)")] <- 31
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjSubClassDesc == "Subdural hemorrhage (Traumatic)")] <- 32
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjSubClassDesc == "Sudden Cardiac Death" & Endpoint$AdjOutcomeDesc == "Coronary Heart Disease Death ")] <- 33
Endpoint[which(Endpoint$AdjOutcomeDesc == "Coronary Heart Disease Death - sudden cardiac death"), c("AdjOutcomeSubClass")] <- 33 # added for EN events
Endpoint$AdjOutcomeSubClass[which(Endpoint$CancerType == 16)] <- 34
Endpoint$AdjOutcomeSubClass[which(Endpoint$CancerType == 98)] <- 35
Endpoint <- Endpoint %>%
mutate(Hem_subtype = ifelse(Adj3Hem == 1, 36,
ifelse(Adj3Hem == 2, 37,
ifelse(Adj3Hem == 3, 38,
ifelse(Adj3Hem == 4, 20, NA)))),
Hem_subtype = ifelse((Adj1Hem == 1 & is.na(Adj3Hem)), 36,
ifelse((Adj1Hem == 2 & is.na(Adj3Hem)) , 37,
ifelse((Adj1Hem == 3 & is.na(Adj3Hem)), 38,
ifelse((Adj1Hem == 4 & is.na(Adj3Hem)), 20, Hem_subtype)))),
AdjOutcomeSubClass = ifelse(AdjOutcomeDesc == "Hemorrhagic Stroke" & is.na(AdjOutcomeSubClass), Hem_subtype, AdjOutcomeSubClass))
Endpoint <- Endpoint %>%
mutate(Hem_subtype_EN = ifelse(Adj3StrokeEN == "Lobar hemorrhage", 36,
ifelse(Adj3StrokeEN == "Basal ganglionic hemorrhage", 37,
ifelse(Adj3StrokeEN == "Brain stem hemorrhage", 38,
ifelse(Adj3StrokeEN == "Hemorrhage from other anatomical location", 20, NA)))),
Hem_subtype_EN = ifelse((Adj1StrokeEN == "Lobar hemorrhage" | Adj2StrokeEN == "Lobar hemorrhage") & is.na(Adj3StrokeEN), 36,
ifelse((Adj1StrokeEN == "Basal ganglionic hemorrhage" | Adj2StrokeEN == "Basal ganglionic hemorrhage") & is.na(Adj3StrokeEN), 37,
ifelse((Adj1StrokeEN == "Brain stem hemorrhage" | Adj2StrokeEN == "Brain stem hemorrhage") & is.na(Adj3StrokeEN), 38,
ifelse((Adj1StrokeEN == "Hemorrhage from other anatomical location" | Adj2StrokeEN == "Hemorrhage from other anatomical location") & is.na(Adj3StrokeEN), 20, Hem_subtype_EN)))),
AdjOutcomeSubClass = ifelse(AdjOutcomeDesc == "Hemorrhagic Stroke" & is.na(AdjOutcomeSubClass), Hem_subtype_EN, AdjOutcomeSubClass)) # Added for EN events
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjOutcomeDesc == "Hemorrhagic Stroke Related Death")] <- 41 # added for EN events
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjOutcomeDesc == "Ischemic Stroke Related Death")] <- 42 # added for EN events
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjOutcomeDesc == "Stroke Related Death - unknown stroke type")] <- 44 # added for EN events
Endpoint <- Endpoint %>%
mutate(Other_death_subtype_EN = ifelse(Adj3Death == "Blood coagulation disorder", 45,
ifelse(Adj3Death == "Dementia", 46,
ifelse(Adj3Death == "GI disease", 47,
ifelse(Adj3Death == "Liver disease", 48,
ifelse(Adj3Death == "Multi-organ failure/non-specific disease of aging", 49,
ifelse(Adj3Death == "Neurological disease/disorder (not stroke)", 50,
ifelse(Adj3Death == "Unknown" | Adj3Death == "No information available" | Adj3Death == "Other", 51,
ifelse(Adj3Death == "Renal disease", 52,
ifelse(Adj3Death == "Respiratory disease (excluding pneumonia)" | Adj3Death == "Pneumonia", 53,
ifelse(Adj3Death == "Sepsis/Infection", 54,
ifelse(Adj3Death == "Suicide", 55,
ifelse(Adj3Death == "Trauma", 56, NA)))))))))))),
Other_death_subtype_EN = ifelse(Adj1Death == "Blood coagulation disorder" & is.na(Adj3Death), 45,
ifelse(Adj1Death == "Dementia" & is.na(Adj3Death), 46,
ifelse(Adj1Death == "GI disease" & is.na(Adj3Death), 47,
ifelse(Adj1Death == "Liver disease" & is.na(Adj3Death), 48,
ifelse(Adj1Death == "Multi-organ failure/non-specific disease of aging" & is.na(Adj3Death), 49,
ifelse(Adj1Death == "Neurological disease/disorder (not stroke)" & is.na(Adj3Death), 50,
ifelse((Adj1Death == "Unknown" | Adj1Death == "No information available" | Adj1Death == "Other") & is.na(Adj3Death), 51,
ifelse(Adj1Death == "Renal disease" & is.na(Adj3Death), 52,
ifelse((Adj1Death == "Respiratory disease (excluding pneumonia)" | Adj1Death == "Pneumonia") & is.na(Adj3Death), 53,
ifelse(Adj1Death == "Sepsis/Infection" & is.na(Adj3Death), 54,
ifelse(Adj1Death == "Suicide" & is.na(Adj3Death), 55,
ifelse(Adj1Death == "Trauma" & is.na(Adj3Death), 56, Other_death_subtype_EN)))))))))))),
AdjOutcomeSubClass = ifelse(AdjOutcomeDesc == "Other Death" & is.na(AdjOutcomeSubClass), Other_death_subtype_EN, AdjOutcomeSubClass)) # added for EN events
Endpoint <- Endpoint %>%
mutate(Ischemic_subtype = ifelse(Adj3_ISST == "Cardioembolism", 57,
ifelse(Adj3_ISST == "Large artery atherosclerosis", 58,
ifelse(Adj3_ISST == "Small Vessel Occlusion", 59,
ifelse(Adj3_ISST == "Undetermined etiology", 60,
ifelse(Adj3_ISST == "Other (determined) etiology ", 61, NA))))),
Ischemic_subtype = ifelse(Adj1_ISST == "Cardioembolism" & is.na(Adj3_ISST), 57,
ifelse(Adj1_ISST == "Large artery atherosclerosis" & is.na(Adj3_ISST), 58,
ifelse(Adj1_ISST == "Small Vessel Occlusion" & is.na(Adj3_ISST), 59,
ifelse(Adj1_ISST == "Undetermined etiology" & is.na(Adj3_ISST), 60,
ifelse(Adj1_ISST == "Other (determined) etiology " & is.na(Adj3_ISST), 61, Ischemic_subtype))))),
AdjOutcomeSubClass = ifelse((AdjOutcomeDesc == "Ischemic Stroke" | AdjOutcomeDesc == "Ischemic Stroke with hemorrhagic transformation") & is.na(AdjOutcomeSubClass), Ischemic_subtype, AdjOutcomeSubClass))
Endpoint <- Endpoint %>%
mutate(Ischemic_subtype_EN = ifelse(Adj3StrokeEN == "Cardioembolic ischemia/infarction", 57,
ifelse(Adj3StrokeEN == "Large artery athersclerosis ischemia/infarction", 58,
ifelse(Adj3StrokeEN == "Small vessel occlusion causing ischemia/infarction", 59,
ifelse(Adj3StrokeEN == "Undetermined etiology causing ischemia/infarction", 60,
ifelse(Adj3StrokeEN == "Other (determined etiology) causing ischemia/infarction", 61, NA))))),
Ischemic_subtype_EN = ifelse((Adj1StrokeEN == "Cardioembolic ischemia/infarction" | Adj2StrokeEN == "Cardioembolic ischemia/infarction") & is.na(Adj3StrokeEN), 57,
ifelse((Adj1StrokeEN == "Large artery athersclerosis ischemia/infarction" | Adj2StrokeEN == "Large artery athersclerosis ischemia/infarction") & is.na(Adj3StrokeEN), 58,
ifelse((Adj1StrokeEN == "Small vessel occlusion causing ischemia/infarction" | Adj2StrokeEN == "Small vessel occlusion causing ischemia/infarction") & is.na(Adj3StrokeEN), 59,
ifelse((Adj1StrokeEN == "Undetermined etiology causing ischemia/infarction" | Adj2StrokeEN == "Undetermined etiology causing ischemia/infarction") & is.na(Adj3StrokeEN), 60,
ifelse((Adj1StrokeEN == "Other (determined etiology) causing ischemia/infarction" | Adj2StrokeEN == "Other (determined etiology) causing ischemia/infarction") & is.na(Adj3StrokeEN), 61, Ischemic_subtype_EN))))),
AdjOutcomeSubClass = ifelse((AdjOutcomeDesc == "Ischemic Stroke" | AdjOutcomeDesc == "Ischemic Stroke with hemorrhagic transformation") & is.na(AdjOutcomeSubClass), Ischemic_subtype_EN, AdjOutcomeSubClass)) # added for EN events
Endpoint$AdjOutcomeSubClass[which(Endpoint$AdjOutcomeDesc == "Other")] <- 62 # added for AE and EN events
# AdjOutcomeModeDeath
Endpoint <- Endpoint %>%
mutate(AdjOutcomeModeDeath = ifelse(Adj3DeathMode == "Cancer (primary tumour)", 1,
ifelse(Adj3DeathMode == "Dementia", 2,
ifelse(Adj3DeathMode == "Hemorrhage", 3,
ifelse(Adj3DeathMode == "Heart failure", 4,
ifelse(Adj3DeathMode == "Infection", 5,
ifelse(Adj3DeathMode == "Metastasis to vital organ", 6,
ifelse(Adj3DeathMode == "Myocardial Infarction", 7,
ifelse(Adj3DeathMode == "Stroke", 8,
ifelse(Adj3DeathMode == "Thrombosis/thromboembolism", 9,
ifelse(Adj3DeathMode == "Toxicity from anti-cancer agent or other drug", 10,
ifelse(Adj3DeathMode == "Unknown", 11,
ifelse(Adj3DeathMode == "Other", 12, NA)))))))))))),
AdjOutcomeModeDeath = ifelse(Adj1DeathMode == "Cancer (primary tumour)" & is.na(Adj3DeathMode), 1,
ifelse(Adj1DeathMode == "Dementia" & is.na(Adj3DeathMode), 2,
ifelse(Adj1DeathMode == "Hemorrhage" & is.na(Adj3DeathMode), 3,
ifelse(Adj1DeathMode == "Heart failure" & is.na(Adj3DeathMode), 4,
ifelse(Adj1DeathMode == "Infection" & is.na(Adj3DeathMode), 5,
ifelse(Adj1DeathMode == "Metastasis to vital organ" & is.na(Adj3DeathMode), 6,
ifelse(Adj1DeathMode == "Myocardial Infarction" & is.na(Adj3DeathMode), 7,
ifelse(Adj1DeathMode == "Stroke" & is.na(Adj3DeathMode), 8,
ifelse(Adj1DeathMode == "Thrombosis/thromboembolism" & is.na(Adj3DeathMode), 9,
ifelse(Adj1DeathMode == "Toxicity from anti-cancer agent or other drug" & is.na(Adj3DeathMode), 10,
ifelse(Adj1DeathMode == "Unknown" & is.na(Adj3DeathMode), 11,
ifelse(Adj1DeathMode == "Other" & is.na(Adj3DeathMode), 12, AdjOutcomeModeDeath)))))))))))))
# MetastaticRecurrence
## Extract baseline cancer history
cancer_type <- Pt_med_hist %>% select(SubjectID, starts_with("CR_"), -ends_with("_TR")) %>%
mutate(CR_BLA = ifelse(CR_BLA >= 49, "BLA", NA),
CR_BLO = ifelse(CR_BLO >= 49, "BLO", NA),
CR_BRE = ifelse(CR_BRE >= 49, "BRE", NA),
CR_CER = ifelse(CR_CER >= 49, "CER", NA),
CR_COL = ifelse(CR_COL >= 49, "COL", NA),
CR_GAL = ifelse(CR_GAL >= 49, "GAL", NA),
CR_MEL = ifelse(CR_MEL >= 49, "MEL", NA),
CR_OVA = ifelse(CR_OVA >= 49, "OVA", NA),
CR_PRO = ifelse(CR_PRO >= 49, "PRO", NA),
CR_THY = ifelse(CR_THY >= 49, "THY", NA),
CR_OTH = ifelse(CR_OTH >= 49, "OTH", NA),
CR_BRA = ifelse(CR_BRA >= 49, "BRA", NA),
CR_KID = ifelse(CR_KID >= 49, "KID", NA),
CR_LIV = ifelse(CR_LIV >= 49, "LIV", NA),
CR_LUN = ifelse(CR_LUN >= 49, "LUN", NA),
CR_MYE = ifelse(CR_MYE >= 49, "MYE", NA),
CR_PAN = ifelse(CR_PAN >= 49, "PAN", NA),
CR_STO = ifelse(CR_STO >= 49, "STO", NA),
type = paste(CR_BLA, CR_BLO, CR_CER, CR_BRE, CR_COL, CR_GAL, CR_MEL, CR_OVA, CR_PRO, CR_THY, CR_OTH, CR_BRA, CR_KID, CR_LIV, CR_LUN, CR_MYE, CR_PAN, CR_STO))
Endpoint <- Endpoint %>%
left_join(cancer_type %>% select(SubjectID, type), by = "SubjectID") %>% # Add cancer history
# change AdjOutcomeSubclass to char
mutate(AdjOutcomeSubClass_char = ifelse(AdjOutcomeSubClass == 1, "BLA",
ifelse(AdjOutcomeSubClass == 2, "BLO",
ifelse(AdjOutcomeSubClass == 4, "BRE",
ifelse(AdjOutcomeSubClass == 6, "CER",
ifelse(AdjOutcomeSubClass == 7, "COL",
ifelse(AdjOutcomeSubClass == 12, "GAL",
ifelse(AdjOutcomeSubClass == 18, "MEL",
ifelse(AdjOutcomeSubClass == 22, "OVA",
ifelse(AdjOutcomeSubClass == 26, "PRO",
ifelse(AdjOutcomeSubClass == 34, "THY",
ifelse(AdjOutcomeSubClass %in% seq(62, 71, 1) | AdjOutcomeSubClass == 35, "OTH",
ifelse(AdjOutcomeSubClass == 3, "BRA",
ifelse(AdjOutcomeSubClass == 15, "KID",
ifelse(AdjOutcomeSubClass == 16, "LIV",
ifelse(AdjOutcomeSubClass == 17, "LUN",
ifelse(AdjOutcomeSubClass == 2, "BLO",
ifelse(AdjOutcomeSubClass == 23, "PAN",
ifelse(AdjOutcomeSubClass == 28, "STO", NA)))))))))))))))))),
History = ifelse(str_detect(type, AdjOutcomeSubClass_char), 1, 0),
History = ifelse(is.na(History), 0, History)) %>%
arrange(SubjectID, EndpointDate, AdjOutcomeSubClass) %>%
group_by(SubjectID) %>%
mutate(Cancer = ifelse(AdjOutcome == 19 | AdjOutcome == 20, 1, NA), # cancer adj
Cancer_number = ifelse(Cancer == 1, row_number(Cancer), NA), # cancer number (1st, 2nd, etc)
Metastatic_cancer = ifelse(AdjOutcome == 19, 1, NA), # metastatic cancer adj
Metastatic_number = ifelse(Metastatic_cancer == 1, row_number(Metastatic_cancer), NA)) %>% # metastatic cancer number (1st, 2nd, etc)
group_by(SubjectID, AdjOutcomeSubClass_char) %>%
mutate(Cancer_number_same_type = ifelse(Cancer == 1, row_number(Cancer), NA)) %>% # cancer number of the same cancer type
mutate(MetastaticRecurrence = ifelse(History == 0 & AdjOutcome == 19 & AdjOutcomeSubClass != 2 & Cancer_number_same_type == 1, 0, # no history of same cancer type, metastatic cancer, not blood cancer and 1st occurrence of a cancer type
ifelse(History == 1 & AdjOutcome == 19 & Cancer_number_same_type == 1, 1, # history of same cancer type, metastatic cancer and 1st occurrence of a cancer type
ifelse(AdjOutcome == 19 & AdjOutcomeSubClass == 2, 2, # metastatic cancer and blood cancer
ifelse(History == 0 & AdjOutcome == 19 & AdjOutcomeSubClass != 2 & Cancer_number_same_type > 1, 3, # no history of same cancer type, metastatic cancer, not blood cancer and 2nd onwards occurrence of a cancer type
ifelse(History == 1 & AdjOutcome == 19 & AdjOutcomeSubClass != 2 & Cancer_number_same_type > 1 & Metastatic_number == 1, 4, NA)))))) # history of same cancer type, not blood cancer, 2nd onwards occurrence of a cancer type, but 1st instance of metastatic cancer
Endpoint$EndpointDt <- Endpoint$EndpointDate
Cutoff <- as.Date("2018-01-31")
Endpoint$XTEvent <- NA
Endpoint$XTEvent[which(Endpoint$DetectedDt > Cutoff)] <- 1
Endpoint <- Endpoint[which(!is.na(Endpoint$EventType)), ]
Endpoint <- Endpoint[, c("SubjectID", "CodeID", "EndpointDt",
"DetectedDt", "XTEvent", "AdjStatus", "AdjStatusDesc", "EventType", "AdjOutcome",
"AdjOutcomeSubClass", "AdjOutcomeModeDeath", "MetastaticRecurrence")]
Endpoint$Status_Sum <- NA
Endpoint$Status_Sum[which(Endpoint$AdjStatus == 31)] <- 1
Endpoint$Status_Sum[which(Endpoint$AdjStatus %in% c(30, 32))] <- 2
Endpoint$Status_Sum[which(Endpoint$AdjStatus %in% c(21, 1002, 29, 33, 20))] <- 3
Endpoint$Status_Sum[which(Endpoint$AdjStatus %in% c(5, 4, 2, 1, 1001, 10, 83))] <- 4
Dementia <- DBI::dbReadTable(con, "vwDementiaAssessment_AE")
Dementia <- Dementia[, c("SubjectID", "CodeID", "Status",
"Cause", "VisitDate")]
Dementia <- Dementia[which(Dementia$Status != 1), ]
Endpoint <- merge(Endpoint, Dementia, by = c("SubjectID", "CodeID"), all = TRUE)
Endpoint$DA <- NA
Endpoint$DA[which(!is.na(Endpoint$VisitDate))] <- 1
Endpoint$DA[which((Endpoint$SubjectID == 40005) & (Endpoint$CodeID == 11534))] <- 1
Endpoint$DA[which((is.na(Endpoint$VisitDate)) & (Endpoint$Status == 2))] <- 2
Endpoint$DA[which((is.na(Endpoint$VisitDate)) & (Endpoint$Status == 3))] <- 3
Endpoint$DA[which((is.na(Endpoint$VisitDate)) & (Endpoint$Status == 0))] <- 4
Endpoint$DA[which((is.na(Endpoint$VisitDate)) & (Endpoint$AdjStatus == 83))] <- 5
Endpoint$DA[which((is.na(Endpoint$VisitDate)) & (is.na(Endpoint$DA)) & (Endpoint$AdjStatus == 4))] <- 5
Endpoint$DA[which((is.na(Endpoint$VisitDate)) & (Endpoint$Status == 17))] <- 6
Endpoint$DA[which((is.na(Endpoint$VisitDate)) & (Endpoint$Status == 18))] <- 7
Endpoint <- MergeCountryBySubjectId(Endpoint, sectionPaths)
Endpoint <- Endpoint[which(!is.na(Endpoint$EventType)),]
F2 <- read.csv(sectionPaths$SectionF2_DMC)
F2 <- F2[, c("SubjectID", "CodeID")]
F2$ASP_Endpoint <- 1
Endpoint <- merge(F2, Endpoint, by = c("CodeID", "SubjectID"), all = TRUE)
return(Endpoint)
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/GetEndpointsXT.R
\name{GetEndpointsXT}
\alias{GetEndpointsXT}
\title{Fetch endpoint summary data}
\usage{
GetEndpointsXT(con, sectionPaths = SDrivePaths())
}
\arguments{
\item{con}{a database connection using the DBI package or \code{ASPREEDb()}}
\item{sectionPaths}{a list mapping section names, e.g. SectionA1, to a file path}
}
\value{
A summary data frame of all endpoints coded in ASPREE or XT
}
\description{
Obtain all endpoints from both AE and EN events
}
\examples{
endpoints <- con \%>\% GetEndpointsXT()
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment