-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.R
316 lines (246 loc) · 20.2 KB
/
helpers.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# Alexandria Jensen
# 23 September 2019
# Helper functions for the LIIA REDCap shiny app
`%notin%` <- Negate(`%in%`)
getData<-function(redcap_api_token) {
# Create the REDCap connection using the API Token provided
Sys.setenv(REDCap_API_URI = "https://redcap.ucdenver.edu/api/")
Sys.setenv(REDCap_API_TOKEN = as.character(redcap_api_token))
# Patch until REDCapExporter can be updated and pushed to CRAN.
h <- curl::new_handle()
h <- curl::handle_setform(h,
token = Sys.getenv("REDCap_API_TOKEN"),
content = "record",
format = "csv")
records_raw <- curl::curl_fetch_memory(Sys.getenv("REDCap_API_URI"), handle = h)
# records_raw <- export_content("record")
# myData <- as.data.frame(records_raw)
myData <- read.csv(text = rawToChar(records_raw$content))
# metadata_raw <- export_content("metadata")
# metadata <- as.data.frame(metadata_raw)
h <- curl::handle_setform(h, content = "metadata")
metadata_raw <- curl::curl_fetch_memory(Sys.getenv("REDCap_API_URI"), handle = h)
metadata <- read.csv(text = rawToChar(metadata_raw$content))
# Subsetting the data to make it more manageable
records_keepVars<-c("study_id","demo_first_name","demo_last_name","demo_dob","demo_phone",
"demo_email","demo_sex","demo_race___0","demo_race___1","demo_race___2",
"demo_race___3","demo_race___4","demo_race___5","demo_race___9",
"demo_ethnicity","demo_handedness","demo_educ_yrs","with_inelig_choice","withd_consen_yesno",
"with_inelig_yesno", "with_inelig_dthdte","with_inelig_detail","consent_version___0", "consent_version___1",
"consent_version___2", "consent_version___3", "consent_version___4", "consent_version___5", "consent_date_apr19",
"consent_date_aug19", "consent_date_aug17_prior", "consent_date_aug17_post", "consent_date_apr12_prior",
"consent_date_apr12_post")
# Records event: demographics and withdrawal/ineligibility
rec_myData<-myData[records_keepVars]
#put NAs for empty cells and since demo data exists on separate line of multiple study id lines, fill in before dropping on demos
rec_myData<-rec_myData %>%
mutate_all(na_if,"") %>%
group_by(study_id) %>%
fill("consent_version___0", "consent_version___1",
"consent_version___2", "consent_version___3", "consent_version___4", "consent_version___5", "consent_date_apr19",
"consent_date_aug19", "consent_date_aug17_prior", "consent_date_aug17_post", "consent_date_apr12_prior",
"consent_date_apr12_post", .direction = "up")
rec_myData<-rec_myData %>%
drop_na(study_id,demo_first_name,demo_last_name)
# Screening event: screening/consent
screen_keepVars<-c("study_id","consent_scrnfail","consent_scrnfail_det",
"consent_yesno","consent_audio_rp","consent_audio_me",
"consent_future","consent_b12_results")
screen_myData<-myData[screen_keepVars]
screen_myData<-screen_myData %>%
mutate_all(na_if,"")
screen_myData<-screen_myData[!is.na(screen_myData$consent_scrnfail) |
!is.na(screen_myData$consent_yesno),]
# Baseline event: innate immune history, header, LP, consensus conference
base_keepVars<-c("study_id","redcap_event_name","immune_date_base","head_day1_date",
"head_visit_comp","lp_date","consensus_date","consensus_conference_complete")
base_myData<-myData[base_keepVars]
base_myData<-base_myData %>%
mutate_all(na_if,"")
base_myData<-base_myData %>%
drop_na(study_id,immune_date_base)
base_myData<-base_myData[base_myData$redcap_event_name=="baseline_visit_arm_1",]
base_myData %<>% dplyr::select(-c(redcap_event_name))
# Survey events: innate immune history
survey_keepVars<-c("study_id","redcap_event_name","innate_immune_history_form_survey_complete")
survey_myData<-myData[survey_keepVars]
survey_myData<-survey_myData %>%
mutate_all(na_if,"")
survey_myData<-survey_myData %>%
drop_na(study_id,innate_immune_history_form_survey_complete)
survey_6mo_myData<-survey_myData[survey_myData$redcap_event_name=="6_month_survey_fol_arm_1",]
survey_6mo_myData %<>% dplyr::rename(survey_complete_6mon=innate_immune_history_form_survey_complete) %>%
dplyr::select(-c(redcap_event_name))
survey_12mo_myData<-survey_myData[survey_myData$redcap_event_name=="12_month_survey_fo_arm_1",]
survey_12mo_myData %<>% dplyr::rename(survey_complete_12mon=innate_immune_history_form_survey_complete) %>%
dplyr::select(-c(redcap_event_name))
survey_18mo_myData<-survey_myData[survey_myData$redcap_event_name=="18_month_survey_fo_arm_1",]
survey_18mo_myData %<>% dplyr::rename(survey_complete_18mon=innate_immune_history_form_survey_complete) %>%
dplyr::select(-c(redcap_event_name))
# Follow-up event: innate immune history, header, LP, consensus conference
fu_keepVars<-c("study_id","redcap_event_name","immune_date_fu","head_day1_date","head_visit_comp",
"lp_date","consensus_date_fu","consensus_conference_followup_complete")
fu_myData<-myData[fu_keepVars]
fu_myData<-fu_myData %>%
mutate_all(na_if,"")
fu_myData<-fu_myData %>%
drop_na(study_id,immune_date_fu)
fu_myData<-fu_myData[fu_myData$redcap_event_name=="2_year_follow_up_v_arm_1",]
fu_myData %<>% dplyr::rename(head_visit_comp_fu=head_visit_comp,lp_date_fu=lp_date,
head_day1_date_fu=head_day1_date,consensus_date_fu=consensus_date_fu,
consensus_conference_fu_complete=consensus_conference_followup_complete) %>%
dplyr::select(-c(redcap_event_name))
# Combining the records, screening, and baseline datasets
myData_merge<-merge(rec_myData,base_myData,by="study_id",all.x=TRUE)
myData_merge<-merge(myData_merge,fu_myData,by="study_id",all.x=TRUE)
myData_merge<-merge(myData_merge,survey_6mo_myData,by="study_id",all.x=TRUE)
myData_merge<-merge(myData_merge,survey_12mo_myData,by="study_id",all.x=TRUE)
myData_merge<-merge(myData_merge,survey_18mo_myData,by="study_id",all.x=TRUE)
myData_merge<-merge(myData_merge,screen_myData,by="study_id",all.x=TRUE)
# Character to numeric
myData_merge$demo_sex<-as.numeric(myData_merge$demo_sex)
myData_merge$demo_ethnicity<-as.numeric(myData_merge$demo_ethnicity)
myData_merge$demo_handedness<-as.numeric(myData_merge$demo_handedness)
myData_merge$demo_educ_yrs<-as.numeric(myData_merge$demo_educ_yrs)
myData_merge$consent_scrnfail<-as.numeric(myData_merge$consent_scrnfail)
myData_merge$consent_yesno<-as.numeric(myData_merge$consent_yesno)
myData_merge$with_inelig_choice<-as.numeric(myData_merge$with_inelig_choice)
# New demographic variables
myData_merge$demo_sex<-ifelse(myData_merge$demo_sex==1,"Female",
ifelse(myData_merge$demo_sex==0,"Male",
ifelse(myData_merge$demo_sex==9,"Prefer not to Answer",NA)))
myData_merge$demo_sex<- factor(myData_merge$demo_sex,levels=c("Female","Male","Prefer not to Answer"))
myData_merge$demo_ethnicity<-ifelse(myData_merge$demo_ethnicity==1,"Not Hispanic or Latino/a",
ifelse(myData_merge$demo_ethnicity==2,"Hispanic or Latino/a",
ifelse(myData_merge$demo_ethnicity==3,"Unknown",
ifelse(myData_merge$demo_ethnicity==9,"Prefer not to Answer",NA))))
myData_merge$demo_ethnicity<- factor(myData_merge$demo_ethnicity,levels=c("Not Hispanic or Latino/a","Hispanic or Latino/a",
"Unknown","Prefer not to Answer"))
myData_merge$demo_handedness<-ifelse(myData_merge$demo_handedness==0,"Right",
ifelse(myData_merge$demo_handedness==1,"Left",
ifelse(myData_merge$demo_handedness==2,"Ambidextrous",
ifelse(myData_merge$demo_handedness==9,"Prefer not to Answer",NA))))
myData_merge$demo_handedness<- factor(myData_merge$demo_handedness,levels=c("Right","Left","Ambidextrous",
"Prefer not to Answer"))
#Create different race variable for use in NIH and COMIRB demographics
#dataframe of all race columns, sum columns if >1 then more than one race, o.w . check which column is 1 and assign accordingly
myData_merge$num_race <- select(myData_merge,starts_with('demo_race__')) %>% rowSums()
myData_merge$demo_race_final <- ifelse(myData_merge$num_race>1,"More than one race",
ifelse(myData_merge$num_race==1 & myData_merge$demo_race___0==1,"Native American",
ifelse(myData_merge$num_race==1 & myData_merge$demo_race___1==1,"Asian",
ifelse(myData_merge$num_race==1 & myData_merge$demo_race___2==1,"Black",
ifelse(myData_merge$num_race==1 & myData_merge$demo_race___3==1,"White",
ifelse(myData_merge$num_race==1 & myData_merge$demo_race___4==1,"Pacific Islander",
ifelse(myData_merge$num_race==1 & (myData_merge$demo_race___5==1 | myData_merge$demo_race___9==1),"Unknown",
ifelse(myData_merge$num_race==0,"Not Reported",NA))))))))
# Creating new race categories
myData_merge$demo_race_NatAmer<-factor(ifelse(myData_merge$demo_race___0==1,"Yes","No"),levels=c("Yes","No"))
myData_merge$demo_race_Asian<-factor(ifelse(myData_merge$demo_race___1==1,"Yes","No"),levels=c("Yes","No"))
myData_merge$demo_race_Black<-factor(ifelse(myData_merge$demo_race___2==1,"Yes","No"),levels=c("Yes","No"))
myData_merge$demo_race_Cauc<-factor(ifelse(myData_merge$demo_race___3==1,"Yes","No"),levels=c("Yes","No"))
myData_merge$demo_race_PacIsl<-factor(ifelse(myData_merge$demo_race___4==1,"Yes","No"),levels=c("Yes","No"))
myData_merge$demo_race_Unkn<-factor(ifelse(myData_merge$demo_race___5==1,"Yes","No"),levels=c("Yes","No"))
myData_merge$demo_race_NoAns<-factor(ifelse(myData_merge$demo_race___9==1,"Yes","No"),levels=c("Yes","No"))
# Numeric to yes/no optional measures consent
myData_merge$consent_audio_rp<-ifelse(myData_merge$consent_audio_rp==1,"Yes",
ifelse(myData_merge$consent_audio_rp==0,"No",NA))
myData_merge$consent_audio_me<-ifelse(myData_merge$consent_audio_me==1,"Yes",
ifelse(myData_merge$consent_audio_me==0,"No",NA))
myData_merge$consent_future<-ifelse(myData_merge$consent_future==1,"Yes",
ifelse(myData_merge$consent_future==0,"No",NA))
myData_merge$consent_b12_results<-ifelse(myData_merge$consent_b12_results==1,"Yes",
ifelse(myData_merge$consent_b12_results==0,"No",NA))
# Creating a new death date variable
myData_merge$death_date<-as.character(as.Date(myData_merge$with_inelig_dthdte))
# Dropping old race and death date variables
drop_rcdth_vars<-names(myData_merge) %in% c("demo_race___0","demo_race___1",
"demo_race___2","demo_race___3",
"demo_race___4","demo_race___5",
"demo_race___9","with_inelig_dthdte")
myData_final<-myData_merge[!drop_rcdth_vars]
# Current age and time diff work
myData_final$curr_age<-round(as.numeric(difftime(Sys.Date(),myData_final$demo_dob,
units="days"))/364.25,2)
myData_final$time_diff<-difftime(Sys.Date(),myData_final$lp_date,units="days")
# Next appt
myData_final$next_appt<-ifelse(myData_final$time_diff>=150 &
myData_final$time_diff<=210,"6 Month Survey",
ifelse(myData_final$time_diff>=335 &
myData_final$time_diff<=395, "12 Month Survey",
ifelse(myData_final$time_diff>=515 &
myData_final$time_diff<=575, "18 Month Survey",
ifelse(myData_final$time_diff>=638 &
myData_final$time_diff<=819,"2 Year Follow Up",
ifelse(myData_final$time_diff>=820,"Overdue 2 Year Follow Up",NA)))))
myData_final$next_appt_date<-ifelse(myData_final$next_appt=="6 Month Survey",as.Date(myData_final$lp_date)+180,
ifelse(myData_final$next_appt=="12 Month Survey",as.Date(myData_final$lp_date)+365,
ifelse(myData_final$next_appt=="18 Month Survey",as.Date(myData_final$lp_date)+545,
ifelse(myData_final$next_appt=="2 Year Follow Up" | myData_final$next_appt=="Overdue 2 Year Follow Up",as.Date(myData_final$lp_date)+730,NA))))
myData_final$next_appt_date_format<-paste0(lubridate::month(as.Date(myData_final$next_appt_date,origin="1970-01-01"),label=TRUE)," ",
lubridate::day(as.Date(myData_final$next_appt_date,origin="1970-01-01")),", ",
lubridate::year(as.Date(myData_final$next_appt_date,origin="1970-01-01")))
# Study status
# myData_final$status_int<-ifelse(is.na(myData_final$with_inelig_choice) & myData_final$consent_scrnfail==0,
# "Actively Enrolled",
# ifelse(is.na(myData_final$consent_scrnfail) & myData_final$with_inelig_choice==1,
# "Study Withdrawal",
# ifelse(myData_final$consent_scrnfail==0 & myData_final$with_inelig_choice==1,
# "Study Withdrawal",
# ifelse(myData_final$consent_scrnfail==1,
# "Screen Fail",
# ifelse(myData_final$with_inelig_choice==2,
# "Study Ineligibility",
# ifelse(myData_final$with_inelig_choice==3,
# "Participant Death",NA))))))
myData_final$status_int<-ifelse(myData_final$consent_scrnfail==1,
"Screen Fail",
ifelse(is.na(myData_final$with_inelig_choice) & myData_final$consent_scrnfail==0,
"Actively Enrolled",
ifelse(myData_final$with_inelig_choice==1 & myData_final$withd_consen_yesno==1,
"Consent Withdrawn",
ifelse(myData_final$with_inelig_choice==1 & myData_final$withd_consen_yesno==0,
"LTFU",
ifelse(myData_final$with_inelig_choice==2,
"Study Ineligibility",
ifelse(myData_final$with_inelig_choice==3,
"Participant Death",NA))))))
myData_final$complete_study <- ifelse(is.na(myData_final$head_visit_comp_fu) | myData_final$head_visit_comp_fu==0,0,1)
myData_final$status <- ifelse(myData_final$complete_study==1,"Completed Study",myData_final$status_int)
myData_final$with_inelig_detail<-as.character(myData_final$with_inelig_detail)
myData_final$consent_scrnfail_det<-as.character(myData_final$consent_scrnfail_det)
myData_final$comments<-ifelse(!is.na(myData_final$with_inelig_detail),myData_final$with_inelig_detail,
ifelse(!is.na(myData_final$consent_scrnfail_det),myData_final$consent_scrnfail_det,NA))
# Who is due for consensus conference?
myData_final$cons_conf_due <- ifelse(myData_final$head_visit_comp==1 & (is.na(myData_final$consensus_conference_complete) | myData_final$consensus_conference_complete==0),"Baseline",
ifelse(myData_final$head_visit_comp_fu==1 & (is.na(myData_final$consensus_conference_fu_complete) | myData_final$consensus_conference_fu_complete==0),"Follow-up",NA))
# Classifying state of each participant in the study
myData_final$base_visit_comp <- ifelse(myData_final$head_visit_comp==0 | is.na(myData_final$head_visit_comp),"No",
ifelse(myData_final$head_visit_comp==1,"Yes",NA))
myData_final$fu_visit_comp <- ifelse(myData_final$head_visit_comp_fu==0 | is.na(myData_final$head_visit_comp_fu),"No",
ifelse(myData_final$head_visit_comp_fu==1,"Yes",NA))
myData_final$base_lp_comp <- ifelse(is.na(myData_final$lp_date),"No","Yes")
myData_final$fu_lp_comp <- ifelse(is.na(myData_final$lp_date_fu),"No","Yes")
myData_final$base_class <- ifelse(!is.na(myData_final$head_day1_date) & myData_final$base_lp_comp=="No" & myData_final$base_visit_comp=="No","Screened, No LP",
ifelse(!is.na(myData_final$head_day1_date) & myData_final$base_lp_comp=="Yes" & myData_final$base_visit_comp=="No","Screened, LP, Not Finished",
ifelse(!is.na(myData_final$head_day1_date) & myData_final$base_lp_comp=="Yes" & myData_final$base_visit_comp=="Yes","Baseline Visit Completed",NA)))
myData_final$fu_class <- ifelse(!is.na(myData_final$head_day1_date_fu) & myData_final$fu_visit_comp=="No","F/U Started, Not Complete",
ifelse(!is.na(myData_final$head_day1_date_fu) & myData_final$fu_lp_comp=="Yes" & myData_final$fu_visit_comp=="Yes","F/U Completed w/ LP",
ifelse(!is.na(myData_final$head_day1_date_fu) & myData_final$fu_lp_comp=="No" & myData_final$fu_visit_comp=="Yes","F/U Completed w/o LP",NA)))
# Next appt date - exclude people already started the next appt
myData_final$next_appt_final <- ifelse(myData_final$next_appt=="6 Month Survey" & is.na(myData_final$survey_complete_6mon),"6 Month Survey",
ifelse(myData_final$next_appt=="12 Month Survey" & is.na(myData_final$survey_complete_12mon),"12 Month Survey",
ifelse(myData_final$next_appt=="18 Month Survey" & is.na(myData_final$survey_complete_18mon),"18 Month Survey",
ifelse(myData_final$next_appt=="2 Year Follow Up" & is.na(myData_final$head_day1_date_fu),"2 Year Follow Up",
ifelse(myData_final$next_appt=="Overdue 2 Year Follow Up" & is.na(myData_final$head_day1_date_fu),"Overdue 2 Year Follow Up",NA)))))
#Get consent dates and version, stack all consent dates in 1 with an coalesce
myData_final$consent_date_agg = dplyr::coalesce(myData_final$consent_date_apr19, myData_final$consent_date_aug19, myData_final$consent_date_aug17_prior, myData_final$consent_date_aug17_post,
myData_final$consent_date_apr12_prior, myData_final$consent_date_apr12_post)
myData_final$consent_vers_agg= case_when(myData_final$consent_version___0 ==1 ~ "11 April 2019",
myData_final$consent_version___1 ==1 ~ "27 August 2019",
myData_final$consent_version___2 ==1 ~ "17 August 2020 (enrolled prior to July 2020)",
myData_final$consent_version___3 ==1 ~ "17 August 2020 (enrolled after July 2020)",
myData_final$consent_version___4 ==1 ~ "12 April 2021 (enrolled prior to July 2020)",
myData_final$consent_version___5 ==1 ~ "12 April 2021 (enrolled after July 2020)")
# Final dataset
myData_final
}