-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path03-eyetracking-data.Rmd
384 lines (324 loc) · 10.3 KB
/
03-eyetracking-data.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
---
title: "Plot eyetracking data"
author: "Tristan Mahr"
date: "`r Sys.Date()`"
output:
github_document:
toc: true
toc_depth: 4
---
```{r setup, include = FALSE, message = FALSE, warning = FALSE, results = 'hide'}
library("knitr")
opts_chunk$set(
cache.path = "assets/cache/03-",
fig.path = "assets/figure/03-",
warning = FALSE,
collapse = TRUE,
comment = "#>",
message = FALSE,
fig.width = 8,
fig.asp = 0.618,
dpi = 300,
out.width = "80%")
wd <- rprojroot::find_rstudio_root_file()
opts_knit$set(root.dir = wd)
options(width = 100)
```
## Set up
```{r, results = "hide"}
library(dplyr)
library(littlelisteners)
library(ggplot2)
library(hrbrthemes)
# Work relative to RStudio project
wd <- rprojroot::find_rstudio_root_file()
df_child_vars <- readr::read_csv(file.path(wd, "data", "scores.csv"))
df_looks <- readr::read_csv(file.path(wd, "data", "screened.csv.gz"))
```
## Head counts and other stats
Boys/girls by native dialect.
```{r}
df_child_vars %>%
count(Dialect, Gender) %>%
ungroup() %>%
rename(`N Children` = n) %>%
knitr::kable()
```
Maternal education by native dialect.
```{r}
df_child_vars %>%
count(Dialect, Maternal_Education_Group) %>%
ungroup() %>%
rename(`N Children` = n) %>%
knitr::kable()
df_child_vars %>%
count(Dialect, Maternal_Education_Group) %>%
ungroup() %>%
tidyr::spread(Dialect, n) %>%
knitr::kable()
df_child_vars %>%
count(Dialect, Maternal_Education_Group, Maternal_Education) %>%
ungroup() %>%
tidyr::spread(Dialect, n) %>%
tidyr::replace_na(list(AAE = 0, MAE = 0)) %>%
knitr::kable()
```
Child level measures by dialect group.
```{r}
narm_mean <- function(...) mean(..., na.rm = TRUE)
narm_sd <- function(...) sd(..., na.rm = TRUE)
narm_n <- function(...) sum(!is.na(...))
df_child_vars %>%
group_by(Dialect) %>%
summarise(
`N Children` = n(),
sum(Female),
`Mean Age (months)` = narm_mean(EVT_Age),
`SD Age (months)` = narm_sd(EVT_Age),
`N EVT` = narm_n(EVT_Raw),
`Mean EVT Standard` = narm_mean(EVT_Standard),
`SD EVT Standard` = narm_sd(EVT_Standard),
`N PPVT` = narm_n(PPVT_Raw),
`Mean PPVT Standard` = narm_mean(PPVT_Standard),
`SD PPVT Standard` = narm_sd(PPVT_Standard)) %>%
knitr::kable(digits = 1)
df_child_vars %>%
group_by(Maternal_Education_Group) %>%
summarise(
`N Children` = n(),
`Mean Age (months)` = narm_mean(EVT_Age),
`SD Age (months)` = narm_sd(EVT_Age),
`N EVT` = narm_n(EVT_Raw),
`Mean EVT Standard` = narm_mean(EVT_Standard),
`SD EVT Standard` = narm_sd(EVT_Standard),
`N PPVT` = narm_n(PPVT_Raw),
`Mean PPVT Standard` = narm_mean(PPVT_Standard),
`SD PPVT Standard` = narm_sd(PPVT_Standard)) %>%
knitr::kable(digits = 1)
df_child_vars %>%
group_by(Dialect, Maternal_Education_Group) %>%
summarise(
`N Children` = n(),
`Mean Age (months)` = narm_mean(EVT_Age),
`SD Age (months)` = narm_sd(EVT_Age),
`N EVT` = narm_n(EVT_Raw),
`Mean EVT Standard` = narm_mean(EVT_Standard),
`SD EVT Standard` = narm_sd(EVT_Standard),
`N PPVT` = narm_n(PPVT_Raw),
`Mean PPVT Standard` = narm_mean(PPVT_Standard),
`SD PPVT Standard` = narm_sd(PPVT_Standard)) %>%
knitr::kable(digits = 1)
```
Home dialect predicts maternal ed. This is a recruitment artifact.
```{r}
df_child_vars %>%
count(Dialect, Maternal_Education_Group) %>%
mutate(`Mat. ed.` = Maternal_Education_Group %>%
factor(c("Low", "Mid", "High")) ) %>%
select(Dialect, `Mat. ed.`, n) %>%
arrange(Dialect, `Mat. ed.`) %>%
knitr::kable()
```
Maternal ed. in turn predicts vocabulary ntiles
```{r}
df_child_vars %>%
count(Maternal_Education_Group, ntile(EVT_Standard, 3)) %>%
rename(`Vocab. level` = `ntile(EVT_Standard, 3)`) %>%
mutate(`Mat. ed.` = Maternal_Education_Group %>%
factor(c("Low", "Mid", "High")) ) %>%
select(`Mat. ed.`, `Vocab. level`, n) %>%
arrange(`Mat. ed.`, `Vocab. level`) %>%
knitr::kable()
```
## Aggregate looking data
Define a response code for the `aggregate_looks()` function.
```{r}
resp_def <- create_response_def(
primary = "Target",
others = c("PhonologicalFoil", "SemanticFoil", "Unrelated"),
elsewhere = "tracked",
missing = NA
)
```
Assign the individual frames into 50-ms bins.
```{r}
df_bin_times <- df_looks %>%
left_join(df_child_vars) %>%
mutate(BlockDialect = ifelse(BlockDialect == "AAE", "AAE", "MAE"),
HearsNativeDialect = Dialect == BlockDialect) %>%
filter(between(Time, -520, 1970)) %>%
assign_bins(3, Time, Basename, TrialNo) %>%
group_by(.bin) %>%
mutate(BinTime = Time %>% median() %>% round(-1)) %>%
ungroup()
```
Aggregated looking data over Time across trials within Dialect x BlockDialect
within Child.
```{r}
df_looks <- df_bin_times %>%
aggregate_looks2(resp_def = resp_def, resp_var = GazeByImageAOI,
Maternal_Education_Group, Study, Dialect, BlockDialect,
HearsNativeDialect, ResearchID, BinTime) %>%
rename(Time = BinTime) %>%
mutate(
Looks_Images = Target + Others,
Prop_Target = Target / Looks_Images,
Prop_PhonologicalFoil = PhonologicalFoil / Looks_Images,
Prop_SemanticFoil = SemanticFoil / Looks_Images,
Prop_Unrelated = Unrelated / Looks_Images)
```
Add some grouping variables to the data.
```{r}
fct_add_counts <- function(f) {
counts <- forcats::fct_count(f)
counts[["new"]] <- sprintf("%s (%s)", counts[["f"]], counts[["n"]])
x <- setNames(counts[["new"]], counts[["f"]])
forcats::fct_relabel(f, function(level) x[level])
}
tertile_labels <- c("Lower third", "Middle third", "Upper third")
df_evt <- df_child_vars %>%
mutate(Vocab3tile = ntile(EVT_Standard, 3),
`Exp. vocab.` = Vocab3tile %>%
factor(1:3, tertile_labels) %>%
fct_add_counts(),
`Maternal edu.` = Maternal_Education_Group %>%
factor(c("Low", "Mid", "High")) %>%
fct_add_counts(),
`Native dialect` = Dialect %>% factor() %>% fct_add_counts()) %>%
select(Study, ResearchID, Vocab3tile,
`Exp. vocab.`, `Maternal edu.`, `Native dialect`,
EVT_Standard, Maternal_Education_Group)
df_looks <- df_looks %>%
mutate(
`Child hears` = ifelse(HearsNativeDialect, "Native dialect",
"Non-native dialect")) %>%
left_join(df_evt)
```
## Finally, plots of fixation patterns over time
Set up plotting constants and helpers.
```{r}
plot_text <- list(
x_time = "Time (ms) after target noun onset",
y_target = "Proportion of looks to named image",
y_image = "Proportion of looks to image",
caption_mean_se = "Mean ± SE"
)
legend_position <- theme(
legend.position = "bottom",
legend.text = element_text(size = 10),
legend.justification = "left")
colors <- viridis::scale_color_viridis(end = .7, discrete = TRUE)
hline_chance <- geom_hline(yintercept = .25, size = 1.25, color = "#cccccc")
vline_onset <- geom_vline(xintercept = 0, size = 1.25, color = "#cccccc")
```
```{r gca-plots}
ggplot(df_looks) +
aes(x = Time, y = Prop) +
hline_chance +
vline_onset +
stat_summary() +
theme_ipsum_rc(axis_title_size = 11) +
labs(x = plot_text$x_time,
y = plot_text$y_target,
caption = plot_text$caption_mean_se)
ggplot(df_looks) +
aes(x = Time, y = Prop, color = `Child hears`) +
hline_chance +
vline_onset +
stat_summary() +
colors +
theme_ipsum_rc(axis_title_size = 11) +
legend_position +
labs(x = plot_text$x_time,
y = plot_text$y_target)
df_looks %>%
filter(between(Time, 0, 2000)) %>%
ggplot() +
aes(x = Time, y = Prop, color = `Child hears`) +
hline_chance +
stat_summary() +
facet_wrap("`Native dialect`", labeller = label_both) +
colors +
theme_ipsum_rc(axis_title_size = 11) +
legend_position +
labs(x = plot_text$x_time,
y = plot_text$y_target)
df_looks %>%
filter(!is.na(`Exp. vocab.`)) %>%
filter(between(Time, 0, 2000)) %>%
ggplot() +
aes(x = Time, y = Prop, color = `Child hears`) +
hline_chance +
stat_summary() +
facet_wrap("`Exp. vocab.`") +
colors +
theme_ipsum_rc(axis_title_size = 11) +
legend_position +
labs(x = plot_text$x_time,
y = plot_text$y_target)
df_looks %>%
filter(!is.na(Maternal_Education_Group)) %>%
filter(between(Time, 0, 2000)) %>%
ggplot() +
aes(x = Time, y = Prop, color = `Child hears`) +
hline_chance +
stat_summary() +
facet_wrap("`Maternal edu.`") +
colors +
theme_ipsum_rc(axis_title_size = 11) +
legend_position +
labs(x = plot_text$x_time,
y = plot_text$y_target)
```
## Spaghetti
Double check the individual growth curves.
```{r everyone, fig.width = 8, fig.height = 10, fig.asp = NULL, out.width = "100%"}
df_looks %>%
filter(Dialect == "AAE") %>%
filter(between(Time, 0, 2000)) %>%
ggplot() +
aes(x = Time, y = Prop, color = `Child hears`) +
hline_chance +
geom_line(aes(group = interaction(ResearchID, `Child hears`))) +
facet_wrap("ResearchID") +
colors +
theme_ipsum_rc(axis_title_size = 11) +
theme(panel.spacing.x = grid::unit(1, "lines"),
panel.spacing.y = grid::unit(1, "lines"),
panel.grid.minor = element_blank()) +
legend_position +
scale_x_continuous(labels = function(x) x / 1000) +
labs(x = "Time (s)",
y = plot_text$y_target)
df_looks %>%
filter(Dialect == "MAE") %>%
filter(between(Time, 0, 2000)) %>%
ggplot() +
aes(x = Time, y = Prop, color = `Child hears`) +
hline_chance +
geom_line(aes(group = interaction(ResearchID, `Child hears`))) +
facet_wrap("ResearchID") +
colors +
theme_ipsum_rc(axis_title_size = 11) +
theme(panel.spacing.x = grid::unit(1, "lines"),
panel.spacing.y = grid::unit(1, "lines"),
panel.grid.minor = element_blank()) +
legend_position +
scale_x_continuous(labels = function(x) x / 1000) +
labs(x = "Time (s)",
y = plot_text$y_target)
```
## Save data-set for modeling
```{r}
df_looks %>%
select(-.response_def, -Study, -PhonologicalFoil,
-SemanticFoil, -Unrelated, -Elsewhere, -Looks_Images,
-(Prop_PhonologicalFoil:Prop_Unrelated), -Vocab3tile) %>%
select(ResearchID, Dialect, BlockDialect,
HearsNativeDialect, Time, everything()) %>%
readr::write_csv(file.path("data", "modeling.csv"))
```
***
```{r}
sessioninfo::session_info()
```