-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsurvey-validation.R
247 lines (216 loc) · 8.52 KB
/
survey-validation.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
# Manual corrections to the records are documented in "S:\CCAN\CCANResEval\MIECHV\RedCap\Chomp\SurveyCorrections\GpavCorrections.csv"
# Be careful not to move this PHI file to somewhere unsafe.
rm(list=ls(all=TRUE)) #Clear the variables from previous runs.
# ---- load-sources ------------------------------------------------------------
source("./manipulation/ferry/survey-ferry.R")
# ---- load-packages -----------------------------------------------------------
library(magrittr)
requireNamespace("tibble")
requireNamespace("dplyr")
requireNamespace("DT")
requireNamespace("readr")
requireNamespace("testit")
# ---- declare-globals ---------------------------------------------------------
path_output <- "./data-phi-free/derived/survey-violation.csv" # Change this value for new validators.
project_id <- 302L
redcap_version <- "6.11.5"
default_arm <- 1L
interview_date_name <- "cdemo_date_1"
validation_check <- function( name, error_message, priority, passing_test ) {
# S3 object to check
l <- list()
class(l) <- "check"
l$name <- name
l$error_message <- error_message
l$priority <- priority
l$passing_test <- passing_test
return( l )
}
# ---- load-data ---------------------------------------------------------------
ds_interview <- retrieve_clients(filter_only_interview_started=TRUE) #Retrieve only those who have an interview.
# ---- tweak-data --------------------------------------------------------------
testit::assert("`interview_started` must be TRUE.", !is.na(ds_interview$interview_started) & ds_interview$interview_started)
# ---- assemble-checks ---------------------------------------------------------
# Add to this list for new validators.
checks <- list(
validation_check(
name = "record_id_no_white_space",
error_message = "'record_id' contains white space (that may be hard to see).",
priority = 1L,
passing_test = function( d ) {
!grepl("\\s", d$record_id, perl=T)
}
),
validation_check(
name = "interview_started_set",
error_message = "`interview_started` is not set. It must be TRUE or FALSE, but not missing.",
priority = 2L,
passing_test = function( d ) {
!is.na(d$interview_started)
}
),
validation_check(
name = "data_collector_set",
error_message = "`data_collector` is not set.",
priority = 2L,
passing_test = function( d ) {
!is.na(d$data_collector)
}
),
validation_check(
name = "wave_set",
error_message = "`wave` is not set.",
priority = 2L,
passing_test = function( d ) {
!is.na(d$wave)
}
),
validation_check(
name = "assess_validity",
error_message = "`assess_validity` is not set.",
priority = 2L,
passing_test = function( d ) {
!is.na(d$assess_validity)
}
),
validation_check(
name = "video_recording",
error_message = "Video recording status not recorded",
priority = 2L,
passing_test = function( d ) {
#IRB approval for video recording starting in Sept 2015.
!is.na(d$did_video_recording_occur) | (d$cdemo_date_1 > 2015-09-01)
}
),
validation_check(
name = "cwbs_missing",
error_message = "Interview was in home, but CWBS is missing.",
priority = 2L,
passing_test = function( d ) {
# If the interview is at home, than the CWBS should be present. An interview outside the home automatically passes.
ifelse(d$interview_in_home, !is.na(d$cwbs_a), TRUE)
}
),
validation_check(
name = "participant_gender",
error_message = "`participant_gender` is not set.",
priority = 2L,
passing_test = function( d ) {
!is.na(d$participant_gender)
}
),
validation_check(
name = "participant_mob",
error_message = "Participant is younger than 16.",
priority = 2L,
passing_test = function( d ) {
# !((as.Date(d$cdemo_date_1) - as.Date(d$participant_mob))/365 < 16)
age_in_years <- as.numeric(difftime(d$cdemo_date_1, d$participant_mob, units="days")/365.25)
return( age_in_years >= 16 )
}
),
validation_check(
name = "index_child",
error_message = "Participant is not pregnant, and index child DOB is missing",
priority = 1L,
passing_test = function( d ) {
!((!d$pregnant_current) & (d$cdemo_dob_c01 < 0))
}
),
validation_check(
name = "index_child_age",
error_message = "index child is over 60 months",
priority = 2L,
passing_test = function( d ) {
!(d$pregnant_current %in% 0 & d$cdemo_age_c01 > 72)
}
),
validation_check(
name = "child_count",
error_message = "The number of children must be at least 1.",
priority = 2L,
passing_test = function( d ) {
d$cdemo_num_children >= 1
}
),
validation_check(
name = "consent_received",
error_message = "The number IRB consent should be received within 45 days.",
priority = 2L,
passing_test = function( d ) {
(d$paperwork_completed_consent | (d$days_since_interview < 45))
}
)
) #End the list of checks.
# Change this path for new validators.
cat(
length(checks), " checks [have been defined](https://github.com/OuhscBbmc/P4/blob/master/analysis/survey-validation/survey-validation.R):\n\n 1. ",
paste(sapply(checks, function(check) check$name), collapse=",\n 1. ") , "."
)
extract_violation_info <- function( d_violation, check ) {
## Be careful not to add any fields, b/c they could contain PHI (especially fields misbehaving values.).
tibble::tibble(
check_name = check$name,
record_id = d_violation$record_id,
data_collector = d_violation$data_collector,
error_message = check$error_message,
priority = check$priority,
interview_date = d_violation[[interview_date_name]]
)
}
empty_violation <- function( ) {
tibble::tibble(
check_name = "all_passed",
record_id = 0L,
data_collector = "",
error_message = "No violations existed in the dataset",
priority = ""
)
}
# ---- execute-checks ----------------------------------------------------------
ds_violation_list <- list()
for( check in checks ) {
index <- length(ds_violation_list) + 1L
violations <- !check$passing_test(ds_interview)
ds_violation_single <- ds_interview %>%
dplyr::filter(violations)
if( nrow(ds_violation_single) > 0L ) {
ds_violation_list[[index]] <- extract_violation_info(ds_violation_single, check)
}
# rm(violations, ds_violation_single)
}
if( length( ds_violation_list) == 0L ) {
ds_violation <- empty_violation()
ds_violation_pretty <- ds_violation
} else {
ds_violation <- dplyr::bind_rows(ds_violation_list)
ds_violation_pretty <- ds_violation %>%
dplyr::mutate(
record_id = sprintf(
'<a href="https://bbmc.ouhsc.edu/redcap/redcap_v%s/DataEntry/grid.php?pid=%s&arm=%s&id=%s&page=participant_demographics" target="_blank">%s</a>',
redcap_version, project_id, default_arm, record_id, record_id
),
check_name = gsub("_", " ", check_name),
data_collector = gsub("_", " ", data_collector),
check_name = factor(check_name)
)
colnames(ds_violation_pretty) <- gsub("_", " ", colnames(ds_violation_pretty))
}
message(length(checks), " checks have been executed. ", nrow(ds_violation), " violation(s) were found.\n")
rm(check, ds_violation_list)
# ---- display-table ----------------------------------------------------------
DT::datatable(
data = ds_violation_pretty,
filter = "bottom",
caption = paste("Violations at", Sys.time()),
escape = FALSE,
options = list(pageLength = 30, dom = 'tip')
)
# The next line is purely for debugging. It displays the entire dataset.
# DT::datatable(ds_interview) # Don't leave this uncommented.
# ---- verify-values -----------------------------------------------------------
# testit::assert("All IDs should be nonmissing and positive.", all(!is.na(ds_interview$CountyID) & (ds_interview$CountyID>0)))
# ---- specify-columns-to-upload -------------------------------------------------
# ---- save-to-disk ------------------------------------------------------------
message("Saving list of violations to `", path_output, "`.")
readr::write_csv(ds_violation, path=path_output)