-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean-ema.R
314 lines (262 loc) · 15.1 KB
/
clean-ema.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
###############################################################################
# ABOUT:
# * Complete preparaton by type of EMA
# * Focus of this script is not on individual items within an EMA but on
# identifying which rows which are likely to result from one of the following
# circumstances:
# - EMAs (any type) which the software chose to launch and were
# successfully delivered/initiated but had indication of
# issues relating to the software
# - EMAs (any type) which the software chose to launch and were
# successfully delivered/initiated with no indication of
# issues relating to the software
# - self-initiated EMA button press where the software chose to NOT
# launch an EMA
###############################################################################
library(dplyr)
library(magrittr)
library(purrr)
library(assertthat)
path.pns.input_data <- Sys.getenv("path.pns.input_data")
path.pns.output_data <- Sys.getenv("path.pns.output_data")
path.pns.staged_data <- Sys.getenv("path.pns.staged_data")
path.pns.code <- Sys.getenv("path.pns.code")
path.shared.code <- Sys.getenv("path.shared.code")
#------------------------------------------------------------------------------
# Create time variables
#------------------------------------------------------------------------------
df.quit.dates <- readRDS(file = file.path(path.pns.staged_data, "quit_dates_final.RData"))
df.quit.dates <- df.quit.dates %>%
rename(start.study.hrts = start.study.date,
end.study.hrts = end.study.date,
quit.hrts = quit.date)
# Convert human-readable timestamps to UNIX timestamps
df.quit.dates <- df.quit.dates %>%
mutate(start.study.unixts = as.numeric(start.study.hrts),
end.study.unixts = as.numeric(end.study.hrts),
quit.unixts = as.numeric(quit.hrts)) %>%
select(id, callnumr,
start.study.hrts, quit.hrts, end.study.hrts,
start.study.unixts, quit.unixts, end.study.unixts, everything())
#------------------------------------------------------------------------------
# Read in raw data (already contains time variables at the EMA-level)
#------------------------------------------------------------------------------
list.all <- readRDS(file = file.path(path.pns.staged_data, "all_ema_processed.RData"))
#------------------------------------------------------------------------------
# Implement inclusion/exclusion criteria applicable to all datasets
# and create variables to use as filtering criteria in all datasets
#------------------------------------------------------------------------------
list.all.subset <- lapply(list.all, function(this.df, use.quit.dates = df.quit.dates){
# Merge quit date data with raw data; quit date data contains time variables
# at the person-level
this.df <- left_join(x = use.quit.dates, y = this.df, by = "id")
this.df <- this.df %>%
# Exclude all of a participant's data if they are tagged as exclude==1
filter(exclude==0) %>%
# Exclude EMAs delivered before start study time and EMAs delivered after
# end study time
filter((delivered.unixts>=start.study.unixts) & (delivered.unixts<=end.study.unixts))
this.df <- this.df %>%
# Tag each EMA as being viewed as delivered during the Pre-Quit period
# or delivered during the Post-Quit Period
mutate(use.as.postquit = if_else(delivered.unixts>=quit.unixts, 1, 0))
# Clean up
this.df <- this.df %>%
select(-exclude) %>%
select(id, callnumr,
start.study.hrts, quit.hrts, end.study.hrts,
start.study.unixts, quit.unixts, end.study.unixts,
record.id, assessment.type,
use.as.postquit, sensitivity,
delivered.hrts, begin.hrts, end.hrts, time.hrts,
delivered.unixts, begin.unixts, end.unixts, time.unixts,
record.status, with.any.response,
everything())
return(this.df)
})
#------------------------------------------------------------------------------
# Identify EMAs (any type) which the software chose to launch and were
# successfully delivered/initiated and had no indication of
# issues relating to the software
#------------------------------------------------------------------------------
list.clean.launched <- list()
# POST-QUIT MODE EMAs #################
list.clean.launched[["Post-Quit Random"]] <- list.all.subset[["Post-Quit Random"]] %>%
filter(with.any.response==1 | (with.any.response==0 & record.status=="Incomplete/Timed Out"))
list.clean.launched[["Post-Quit Urge"]] <- list.all.subset[["Post-Quit Urge"]] %>%
filter(with.any.response==1 | (with.any.response==0 & record.status=="Incomplete/Timed Out"))
list.clean.launched[["Post-Quit About to Slip Part One"]] <- list.all.subset[["Post-Quit About to Slip Part One"]] %>%
filter(with.any.response==1 | (with.any.response==0 & record.status=="Incomplete/Timed Out"))
list.clean.launched[["Post-Quit About to Slip Part Two"]] <- list.all.subset[["Post-Quit About to Slip Part Two"]] %>%
filter(with.any.response==1 | (with.any.response==0 & record.status=="Incomplete/Timed Out"))
list.clean.launched[["Post-Quit Already Slipped"]] <- list.all.subset[["Post-Quit Already Slipped"]] %>%
filter(with.any.response==1 | (with.any.response==0 & record.status=="Incomplete/Timed Out"))
# PRE-QUIT MODE EMAs ##################
list.clean.launched[["Pre-Quit Random"]] <- list.all.subset[["Pre-Quit Random"]] %>%
filter(with.any.response==1 | (with.any.response==0 & record.status=="Incomplete/Timed Out"))
list.clean.launched[["Pre-Quit Urge"]] <- list.all.subset[["Pre-Quit Urge"]] %>%
filter(with.any.response==1 | (with.any.response==0 & record.status=="Incomplete/Timed Out"))
list.clean.launched[["Pre-Quit Smoking Part One"]] <- list.all.subset[["Pre-Quit Smoking Part One"]] %>%
filter(with.any.response==1 | (with.any.response==0 & record.status=="Incomplete/Timed Out"))
list.clean.launched[["Pre-Quit Smoking Part Two"]] <- list.all.subset[["Pre-Quit Smoking Part Two"]] %>%
filter(with.any.response==1 | (with.any.response==0 & record.status=="Incomplete/Timed Out"))
saveRDS(list.clean.launched, file.path(path.pns.staged_data, "clean_launched.RData"))
#------------------------------------------------------------------------------
# Identify EMAs (any type) which the software chose to launch and were
# successfully delivered/initiated but had some indication of
# issues relating to the software
#------------------------------------------------------------------------------
list.dirty.launched <- list()
# POST-QUIT MODE EMAs #################
list.dirty.launched[["Post-Quit Random"]] <- list.all.subset[["Post-Quit Random"]] %>%
filter((with.any.response==0 & record.status=="Completed") | (with.any.response==0 & record.status=="FRAGMENT RECORD")) %>%
select(id, callnumr,
start.study.hrts, quit.hrts, end.study.hrts,
start.study.unixts, quit.unixts, end.study.unixts,
record.id, assessment.type,
use.as.postquit, sensitivity,
delivered.hrts, begin.hrts, end.hrts, time.hrts,
delivered.unixts, begin.unixts, end.unixts, time.unixts,
record.status, with.any.response)
list.dirty.launched[["Post-Quit Urge"]] <- list.all.subset[["Post-Quit Urge"]] %>%
filter((with.any.response==0 & record.status=="Completed") | (with.any.response==0 & record.status=="FRAGMENT RECORD")) %>%
select(id, callnumr,
start.study.hrts, quit.hrts, end.study.hrts,
start.study.unixts, quit.unixts, end.study.unixts,
record.id, assessment.type,
use.as.postquit, sensitivity,
delivered.hrts, begin.hrts, end.hrts, time.hrts,
delivered.unixts, begin.unixts, end.unixts, time.unixts,
record.status, with.any.response)
list.dirty.launched[["Post-Quit About to Slip Part One"]] <- list.all.subset[["Post-Quit About to Slip Part One"]] %>%
filter((with.any.response==0 & record.status=="Completed") | (with.any.response==0 & record.status=="FRAGMENT RECORD")) %>%
select(id, callnumr,
start.study.hrts, quit.hrts, end.study.hrts,
start.study.unixts, quit.unixts, end.study.unixts,
record.id, assessment.type,
use.as.postquit, sensitivity,
delivered.hrts, begin.hrts, end.hrts, time.hrts,
delivered.unixts, begin.unixts, end.unixts, time.unixts,
record.status, with.any.response)
list.dirty.launched[["Post-Quit About to Slip Part Two"]] <- list.all.subset[["Post-Quit About to Slip Part Two"]] %>%
filter((with.any.response==0 & record.status=="Completed") | (with.any.response==0 & record.status=="FRAGMENT RECORD")) %>%
select(id, callnumr,
start.study.hrts, quit.hrts, end.study.hrts,
start.study.unixts, quit.unixts, end.study.unixts,
record.id, assessment.type,
use.as.postquit, sensitivity,
delivered.hrts, begin.hrts, end.hrts, time.hrts,
delivered.unixts, begin.unixts, end.unixts, time.unixts,
record.status, with.any.response)
list.dirty.launched[["Post-Quit Already Slipped"]] <- list.all.subset[["Post-Quit Already Slipped"]] %>%
filter((with.any.response==0 & record.status=="Completed") | (with.any.response==0 & record.status=="FRAGMENT RECORD")) %>%
select(id, callnumr,
start.study.hrts, quit.hrts, end.study.hrts,
start.study.unixts, quit.unixts, end.study.unixts,
record.id, assessment.type,
use.as.postquit, sensitivity,
delivered.hrts, begin.hrts, end.hrts, time.hrts,
delivered.unixts, begin.unixts, end.unixts, time.unixts,
record.status, with.any.response)
# PRE-QUIT MODE EMAs ##################
list.dirty.launched[["Pre-Quit Random"]] <- list.all.subset[["Pre-Quit Random"]] %>%
filter((with.any.response==0 & record.status=="Completed") | (with.any.response==0 & record.status=="FRAGMENT RECORD")) %>%
select(id, callnumr,
start.study.hrts, quit.hrts, end.study.hrts,
start.study.unixts, quit.unixts, end.study.unixts,
record.id, assessment.type,
use.as.postquit, sensitivity,
delivered.hrts, begin.hrts, end.hrts, time.hrts,
delivered.unixts, begin.unixts, end.unixts, time.unixts,
record.status, with.any.response)
list.dirty.launched[["Pre-Quit Urge"]] <- list.all.subset[["Pre-Quit Urge"]] %>%
filter((with.any.response==0 & record.status=="Completed") | (with.any.response==0 & record.status=="FRAGMENT RECORD")) %>%
select(id, callnumr,
start.study.hrts, quit.hrts, end.study.hrts,
start.study.unixts, quit.unixts, end.study.unixts,
record.id, assessment.type,
use.as.postquit, sensitivity,
delivered.hrts, begin.hrts, end.hrts, time.hrts,
delivered.unixts, begin.unixts, end.unixts, time.unixts,
record.status, with.any.response)
list.dirty.launched[["Pre-Quit Smoking Part One"]] <- list.all.subset[["Pre-Quit Smoking Part One"]] %>%
filter((with.any.response==0 & record.status=="Completed") | (with.any.response==0 & record.status=="FRAGMENT RECORD")) %>%
select(id, callnumr,
start.study.hrts, quit.hrts, end.study.hrts,
start.study.unixts, quit.unixts, end.study.unixts,
record.id, assessment.type,
use.as.postquit, sensitivity,
delivered.hrts, begin.hrts, end.hrts, time.hrts,
delivered.unixts, begin.unixts, end.unixts, time.unixts,
record.status, with.any.response)
list.dirty.launched[["Pre-Quit Smoking Part Two"]] <- list.all.subset[["Pre-Quit Smoking Part Two"]] %>%
filter((with.any.response==0 & record.status=="Completed") | (with.any.response==0 & record.status=="FRAGMENT RECORD")) %>%
select(id, callnumr,
start.study.hrts, quit.hrts, end.study.hrts,
start.study.unixts, quit.unixts, end.study.unixts,
record.id, assessment.type,
use.as.postquit, sensitivity,
delivered.hrts, begin.hrts, end.hrts, time.hrts,
delivered.unixts, begin.unixts, end.unixts, time.unixts,
record.status, with.any.response)
df.dirty.launched <- bind_rows(list.dirty.launched)
saveRDS(df.dirty.launched, file.path(path.pns.staged_data, "dirty_launched.RData"))
#------------------------------------------------------------------------------
# Identify self-initiated EMA (any type) button press where the software chose
# to NOT launch an EMA
#------------------------------------------------------------------------------
list.bp <- list()
# POST-QUIT MODE EMAs #################
list.bp[["Post-Quit Urge"]] <- list.all.subset[["Post-Quit Urge"]] %>%
filter(with.any.response==0 & record.status=="CANCELLED") %>%
select(id, callnumr,
start.study.hrts, quit.hrts, end.study.hrts,
start.study.unixts, quit.unixts, end.study.unixts,
record.id, assessment.type,
use.as.postquit, sensitivity,
delivered.hrts, begin.hrts, end.hrts, time.hrts,
delivered.unixts, begin.unixts, end.unixts, time.unixts,
record.status, with.any.response)
list.bp[["Post-Quit About to Slip Part One"]] <- list.all.subset[["Post-Quit About to Slip Part One"]] %>%
filter(with.any.response==0 & record.status=="CANCELLED") %>%
select(id, callnumr,
start.study.hrts, quit.hrts, end.study.hrts,
start.study.unixts, quit.unixts, end.study.unixts,
record.id, assessment.type,
use.as.postquit, sensitivity,
delivered.hrts, begin.hrts, end.hrts, time.hrts,
delivered.unixts, begin.unixts, end.unixts, time.unixts,
record.status, with.any.response)
list.bp[["Post-Quit Already Slipped"]] <- list.all.subset[["Post-Quit Already Slipped"]] %>%
filter(with.any.response==0 & record.status=="CANCELLED") %>%
select(id, callnumr,
start.study.hrts, quit.hrts, end.study.hrts,
start.study.unixts, quit.unixts, end.study.unixts,
record.id, assessment.type,
use.as.postquit, sensitivity,
delivered.hrts, begin.hrts, end.hrts, time.hrts,
delivered.unixts, begin.unixts, end.unixts, time.unixts,
record.status, with.any.response)
# PRE-QUIT MODE EMAs ##################
list.bp[["Pre-Quit Urge"]] <- list.all.subset[["Pre-Quit Urge"]] %>%
filter(with.any.response==0 & record.status=="CANCELLED") %>%
select(id, callnumr,
start.study.hrts, quit.hrts, end.study.hrts,
start.study.unixts, quit.unixts, end.study.unixts,
record.id, assessment.type,
use.as.postquit, sensitivity,
delivered.hrts, begin.hrts, end.hrts, time.hrts,
delivered.unixts, begin.unixts, end.unixts, time.unixts,
record.status, with.any.response)
list.bp[["Pre-Quit Smoking Part One"]] <- list.all.subset[["Pre-Quit Smoking Part One"]] %>%
filter(with.any.response==0 & record.status=="CANCELLED") %>%
select(id, callnumr,
start.study.hrts, quit.hrts, end.study.hrts,
start.study.unixts, quit.unixts, end.study.unixts,
record.id, assessment.type,
use.as.postquit, sensitivity,
delivered.hrts, begin.hrts, end.hrts, time.hrts,
delivered.unixts, begin.unixts, end.unixts, time.unixts,
record.status, with.any.response)
df.bp <- bind_rows(list.bp)
saveRDS(df.bp, file.path(path.pns.staged_data, "buttonpress.RData"))