-
Notifications
You must be signed in to change notification settings - Fork 1
/
01-data-screening.Rmd
356 lines (275 loc) · 8.65 KB
/
01-data-screening.Rmd
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
---
title: "Data screening"
author: "Tristan Mahr"
date: "`r Sys.Date()`"
output:
github_document:
toc: true
toc_depth: 4
---
```{r setup, include = FALSE}
library("knitr")
opts_chunk$set(
cache.path = "assets/cache/01-",
fig.path = "assets/figure/01-",
warning = FALSE,
collapse = TRUE,
comment = "#>",
message = FALSE,
fig.width = 8,
fig.asp = 0.618,
dpi = 300,
out.width = "80%")
options(width = 100)
```
This script screens the eyetracking data by removing unreliable trials and
unreliable blocks of trials. It preserves the pairs of matched children. (If a
child is removed for have no reliable eyetracking data, their match is also
removed.) Counts of trials and children at each step of screening are printed
too.
## Set up
Load the data.
```{r}
library(dplyr, warn.conflicts = FALSE)
df_child <- readr::read_csv("data-raw/test-scores.csv") %>%
select(Group, Matching_PairNumber,
ChildID, ChildStudyID, Study, ResearchID, Female)
df_blocks <- readr::read_csv("data-raw/blocks.csv")
df_looks <- readr::read_csv("data-raw/looks.csv.gz")
df_trials <- readr::read_csv("data-raw/trials.csv")
```
In order to aggregate looks with my littlelisteners package, we need to define
a response coding definition so it knows what's a target, what's offscreen, etc.
```{r}
library(littlelisteners)
def <- create_response_def(
label = "LWL scheme",
primary = "Target",
others = "Distractor",
elsewhere = "tracked",
missing = NA)
```
Set data-screening options.
```{r}
screening <- list(
# determine amount of missing data between:
min_time = 250,
max_time = 1500,
# remove trials with more than ... proportion of missing data
max_na = .5,
# blocks should have at least this many trials
min_block = 12,
# conditions should have at least this many trials
min_condition = 6
)
```
## Remove blocks from the bad version of the experiment
The first version of the experiment ran too quickly; there wasn't much time
between the prompt and the reinforcer phrase.
```{r}
blocks_to_drop <- df_blocks %>%
filter(Block_Version != "Standard") %>%
select(BlockID:Block_Version)
knitr::kable(blocks_to_drop)
looks_good_version <- df_looks %>%
anti_join(blocks_to_drop)
```
## Find unreliable trials
We identify trials with more than `r screening$max_na * 100`%
missing data during a trial window (here `r screening$min_time`--
`r screening$max_time` ms).
```{r}
trial_quality <- looks_good_version %>%
left_join(df_child) %>%
# Offset by 20 ms because the data binned into 50ms bins: I.e., the frame at
# 285ms is part of the [285, 300, 315] ms bin, so that frame needs to part of
# the data screening.
filter(between(Time, screening$min_time - 20, screening$max_time + 20))
range(trial_quality$Time)
missing_data_per_trial <- trial_quality %>%
aggregate_looks(def, Group + Study + ResearchID +
Matching_PairNumber + TrialID ~ GazeByImageAOI) %>%
select(Group:TrialID, PropNA) %>%
print()
```
Count the number of bad trials that need to be excluded.
```{r}
# Using UQ to unquote `screening$max_na` so that the created column is
# *not* named `PropNA > screening$max_na` but instead uses the value of
# `screening$max_na` in the column name.
missing_data_per_trial %>%
count(PropNA > UQ(screening$max_na)) %>%
rename(`Num Trials` = n) %>%
knitr::kable()
missing_data_per_trial %>%
count(Group,PropNA > UQ(screening$max_na)) %>%
rename(`Num Trials` = n) %>%
knitr::kable()
```
Remove the bad trials.
```{r}
bad_trials <- missing_data_per_trial %>%
filter(PropNA >= screening$max_na)
looks_clean_trials <- anti_join(looks_good_version, bad_trials)
```
## Find unreliable blocks
Count the number of trials leftover in each block.
```{r}
# Narrow down to one row per trial
trial_counts <- looks_clean_trials %>%
select(BlockID:ResearchID) %>%
distinct() %>%
# Count rows in each group
count(BlockID, Study, ResearchID) %>%
arrange(n) %>%
rename(`Num Trials in Block` = n)
```
Identify blocks with fewer than `r screening$min_block` trials.
```{r}
blocks_to_drop <- trial_counts %>%
filter(`Num Trials in Block` < screening$min_block)
knitr::kable(blocks_to_drop)
```
Remove the blocks.
```{r}
looks_clean_blocks <- looks_clean_trials %>%
anti_join(blocks_to_drop)
```
## Enforce minimum number of trials per condition
Count the number of trials leftover in each condition.
```{r}
# Narrow down to one row per trial
condition_counts <- looks_clean_blocks %>%
left_join(df_trials %>% select(TrialID, Condition)) %>%
select(Study, ResearchID, TrialID, Condition) %>%
distinct() %>%
# Count rows in each group
count(Condition, Study, ResearchID) %>%
# Make sure 0 trials get counted
tidyr::complete(tidyr::nesting(Study, ResearchID), Condition,
fill = list(n = 0)) %>%
arrange(n) %>%
rename(`Num Trials in Condition` = n) %>%
print()
```
We want to ensure that there are at least
`r screening$min_condition` trials per condition.
```{r}
children_to_drop <- condition_counts %>%
filter(`Num Trials in Condition` < screening$min_condition)
if (nrow(children_to_drop) == 0) {
print("No children to remove")
} else {
knitr::kable(children_to_drop)
}
```
Remove the children.
```{r}
looks_clean_conditions <- looks_clean_blocks %>%
anti_join(children_to_drop)
```
## Remove unpaired children
Next, we need to exclude participants who no longer have a match.
```{r}
df_leftover <- looks_clean_conditions %>%
distinct(Study, ResearchID) %>%
left_join(df_child)
df_leftover %>%
count(Group) %>%
ungroup() %>%
rename(`Num Children` = n)
df_singletons <- df_leftover %>%
count(Matching_PairNumber) %>%
ungroup() %>%
rename(NumChildrenInPair = n) %>%
filter(NumChildrenInPair == 1) %>%
print()
df_looks_clean <- looks_clean_conditions %>%
left_join(df_child %>%
select(Group, Matching_PairNumber, Study, ResearchID)) %>%
anti_join(df_singletons)
```
Now, there will be the same number of children in each group x task.
```{r}
df_looks_clean %>%
distinct(Group, Study, ResearchID) %>%
count(Group) %>%
ungroup() %>%
rename(`Num Children` = n) %>%
knitr::kable()
# Make sure there are 2 children in every matching pair
df_looks_clean %>%
distinct(Matching_PairNumber, Group, Study, ResearchID) %>%
count(Matching_PairNumber) %>%
ungroup() %>%
rename(NumChildrenInPair = n) %>%
filter(NumChildrenInPair != 2)
```
## Data screening counts
Count the number of children and trials at each stage in data screening.
```{r}
cleaning_progression <- list(
`a. raw data` = df_looks,
`b. remove bad experiment version` = looks_good_version,
`c. drop bad trials` = looks_clean_trials,
`d. drop sparse blocks` = looks_clean_blocks,
`e. drop children w sparse conditions` = looks_clean_conditions,
`f. drop unpaired children` = df_looks_clean) %>%
bind_rows(.id = "Stage") %>%
select(-Group, -Matching_PairNumber) %>%
left_join(df_child)
cleaning_progression %>%
group_by(Stage) %>%
summarise(
`Num Pairs` = n_distinct(Matching_PairNumber),
`Num Children` = n_distinct(ChildID),
`Num Child-Study IDs` = n_distinct(ChildStudyID),
`Num Blocks` = n_distinct(BlockID),
`Num Trials` = n_distinct(TrialID)) %>%
knitr::kable()
cleaning_progression %>%
group_by(Stage, Group) %>%
summarise(
`Num Children` = n_distinct(ChildID),
`Num Child-Study IDs` = n_distinct(ChildStudyID),
`Num Blocks` = n_distinct(BlockID),
`Num Trials` = n_distinct(TrialID)) %>%
knitr::kable()
```
## Clean up
```{r}
df_looks_w_info <- df_looks_clean %>%
left_join(df_blocks) %>%
left_join(df_trials) %>%
left_join(df_child) %>%
select(ChildID, ChildStudyID, Group, Matching_PairNumber, Study, ResearchID,
Block_Basename, Block_Dialect, StimulusSet, Block_Version,
TrialNo, Condition:TargetImage, Time:GazeByAOI)
df_looks_w_info %>%
distinct(Block_Dialect, StimulusSet, Block_Version)
df_looks_w_info <- df_looks_w_info %>%
select(-Block_Dialect, -Block_Version, -StimulusSet)
```
Save clean data.
```{r}
df_looks_w_info %>%
mutate(
Group_Lab = Group %>%
factor(c("CochlearImplant", "NormalHearing"),
c("Cochlear implant", "Normal hearing"))) %>%
readr::write_csv("./data/screened.csv.gz")
```
Update participants data to only have children with eyetracking data.
```{r}
readr::read_csv(file.path(".", "data-raw", "test-scores.csv")) %>%
semi_join(df_looks_w_info) %>%
mutate(
Group_Lab = Group %>%
factor(c("CochlearImplant", "NormalHearing"),
c("Cochlear implant", "Normal hearing"))) %>%
readr::write_csv("./data/scores.csv")
```
***
```{r}
sessioninfo::session_info()
```