-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-ET_pupil_latency_lmm.Rmd
225 lines (200 loc) · 9.41 KB
/
05-ET_pupil_latency_lmm.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
---
title: "02-pupil_size_peak_lmm"
output: html_document
date: "2023-08-22"
---
```{r setup, include=FALSE}
# install all needed packages
pacman::p_load('dplyr', 'ggdist', 'ggeffects', 'ggpubr', 'lme4', 'emmeans', 'rstatix', 'car', 'rsq', 'sjPlot')
library(lme4) # for the analysis
# library(tidyverse) # needed for data manipulation.
library(emmeans)
library(lmerTest)
```
## Setting the parameters
Setting the parameters. This is where the user can change the parameters if needed
```{r Housekeeping and parameters setting}
rm(list = ls())
bids_root <- getwd()
dir()
```
# Experiment 1
## Loading the data
```{r Loading and transform the data}
# Generate the file name:
full_path = file.path(bids_root, 'bids', 'derivatives', 'pupil_latency_no_rej', 'prp', 'pupil_peak_latencies.csv')
data_exp1 <- read.csv(full_path)
# Remove the targets:
data_exp1 = data_exp1 %>% filter(task != "target")
# make latency aud of 0 a 0.001 (zeros cause errors in GLMM with gamma family)
data_exp1$latency_aud[data_exp1$latency_aud == 0] = 0.001
# Set the factors types:
# data task relevance and SOA lock numeric
data_exp1 = data_exp1 %>% mutate(is_task_relevant = ifelse(task == 'non-target',1,0))
data_exp1 = data_exp1 %>% mutate(is_onset = ifelse(SOA_lock == 'onset',1,0))
# make SOA and duration oridnal factors
data_exp1 = data_exp1 %>% mutate(f_SOA = factor(SOA, ordered = TRUE, levels = c("0", "0.116", "0.232", "0.466")))
data_exp1 = data_exp1 %>% mutate(f_duration = factor(duration, ordered = TRUE, levels = c("0.5", "1", "1.5")))
# center variables
data_exp1 = data_exp1 %>% mutate(c_task_relevant = is_task_relevant - mean(is_task_relevant))
data_exp1 = data_exp1 %>% mutate(c_is_onset = is_onset - mean(is_onset))
data_exp1 = data_exp1 %>% mutate(c_SOA = SOA - mean(SOA))
data_exp1 = data_exp1 %>% mutate(c_duration = duration - mean(duration))
# Create the directory to store the results:
save_dir <- file.path(bids_root, 'bids', "derivatives", "pupil_latency", "prp")
# Create the directory if it doesn't exist
if (!file.exists(save_dir)) {
dir.create(save_dir, recursive = TRUE)
}
```
## Onset model
The full model is applied to all the data
```{r Onset model:}
# Extract the onset data points only:
onset_data_exp1 = data_exp1 %>% filter(SOA_lock == "onset")
onset_model_exp1 <- glmer(formula = latency_aud ~ 1 + c_task_relevant * f_SOA + (1 | sub_id),
family=Gamma(link="log"), data = onset_data_exp1)
summary(onset_model_exp1)
anova_results <- Anova(onset_model_exp1)
write.csv(anova_results, file = file.path(save_dir, "Experiment1-onset_mdl_anova.csv"), row.names = TRUE)
anova_results
plot(fitted(onset_model_exp1),residuals(onset_model_exp1))
qqnorm(residuals(onset_model_exp1))
```
## Offset model
The full model is applied to all the data
```{r Offset model:}
# Extract the onset data points only:
offset_data_exp1 = data_exp1 %>% filter(SOA_lock == "offset")
offset_model_exp1 <- glmer(formula = latency_aud ~ 1 + f_duration * c_task_relevant * f_SOA + (1 | sub_id),
family=Gamma(link="log"), data = offset_data_exp1)
summary(offset_model_exp1)
anova_results <- Anova(offset_model_exp1)
write.csv(anova_results, file = file.path(save_dir, "Experiment1-offset_mdl_anova.csv"), row.names = TRUE)
anova_results
plot(fitted(offset_model_exp1),residuals(offset_model_exp1))
qqnorm(residuals(offset_model_exp1))
```
## Offset model separately for each T1 durations:
Modelling pupil peak latency of offset trials separately for each T1 duration:
```{r Offset model per duration:}
# ========================================================
# Short trials:
offset_data_short_exp1 = offset_data_exp1 %>% filter(duration == 0.5)
# Modelling:
offset_model_short_exp1 <- glmer(formula = latency_aud ~ 1 + c_task_relevant * f_SOA + (1 | sub_id),
family=Gamma(link="log"), data = offset_data_short_exp1)
summary(offset_model_short_exp1)
anova_results <- Anova(offset_model_short_exp1)
write.csv(anova_results, file = file.path(save_dir, "Experiment1-offset_short_mdl_anova.csv"), row.names = TRUE)
anova_results
# ========================================================
# Intermediate trials:
offset_data_int_exp1 = offset_data_exp1 %>% filter(duration == 1.0)
# Modelling:
offset_model_int_exp1 <- glmer(formula = latency_aud ~ 1 + c_task_relevant * f_SOA + (1 | sub_id),
family=Gamma(link="log"), data = offset_data_int_exp1)
summary(offset_model_int_exp1)
anova_results <- Anova(offset_model_int_exp1)
write.csv(anova_results, file = file.path(save_dir, "Experiment1-offset_int_mdl_anova.csv"), row.names = TRUE)
anova_results
# ========================================================
# Long trials:
offset_data_long_exp1 = offset_data_exp1 %>% filter(duration == 1.5)
# Modelling:
offset_model_long_exp1 <- glmer(formula = latency_aud ~ 1 + c_task_relevant * f_SOA + (1 | sub_id),
family=Gamma(link="log"), data = offset_data_long_exp1)
summary(offset_model_long_exp1)
anova_results <- Anova(offset_model_long_exp1)
write.csv(anova_results, file = file.path(save_dir, "Experiment1-offset_long_mdl_anova.csv"), row.names = TRUE)
anova_results
```
# Experiment 2
## Loading the data
```{r Loading and transform the data}
# Generate the file name:
full_path = file.path(bids_root, 'bids', 'derivatives', 'pupil_latency', 'introspection', 'pupil_peak_latencies.csv')
data_exp2 <- read.csv(full_path)
# Remove the targets:
data_exp2 = data_exp2 %>% filter(task != "target")
# make latency aud of 0 a 0.001 (zeros cause errors in GLMM with gamma family)
data_exp2$latency_aud[data_exp2$latency_aud == 0] = 0.001
# Set the factors types:
# data task relevance and SOA lock numeric
data_exp2 = data_exp2 %>% mutate(is_task_relevant = ifelse(task == 'non-target',1,0))
data_exp2 = data_exp2 %>% mutate(is_onset = ifelse(SOA_lock == 'onset',1,0))
# make SOA and duration oridnal factors
data_exp2 = data_exp2 %>% mutate(f_SOA = factor(SOA, ordered = TRUE, levels = c("0", "0.116", "0.466")))
data_exp2 = data_exp2 %>% mutate(f_duration = factor(duration, ordered = TRUE, levels = c("0.5", "1", "1.5")))
# center variables
data_exp2 = data_exp2 %>% mutate(c_task_relevant = is_task_relevant - mean(is_task_relevant))
data_exp2 = data_exp2 %>% mutate(c_is_onset = is_onset - mean(is_onset))
data_exp2 = data_exp2 %>% mutate(c_SOA = SOA - mean(SOA))
data_exp2 = data_exp2 %>% mutate(c_duration = duration - mean(duration))
# Create the save dir
save_dir <- file.path(bids_root, 'bids', "derivatives", "pupil_latency", "introspection")
# Create the directory if it doesn't exist
if (!file.exists(save_dir)) {
dir.create(save_dir, recursive = TRUE)
}
```
## Onset model
The full model is applied to all the data
```{r Onset model:}
# Extract the onset data points only:
onset_data_exp2 = data_exp2 %>% filter(SOA_lock == "onset")
onset_model_exp2 <- glmer(formula = latency_aud ~ 1 + c_task_relevant * f_SOA + (1 | sub_id),
family=Gamma(link="log"), data = onset_data_exp2)
summary(onset_model_exp2)
anova_results <- Anova(onset_model_exp2)
write.csv(anova_results, file = file.path(save_dir, "Experiment2-onset_mdl_anova.csv"), row.names = TRUE)
anova_results
plot(fitted(onset_model_exp2),residuals(onset_model_exp2))
qqnorm(residuals(onset_model_exp2))
```
## Offset model
The full model is applied to all the data
```{r Offset model:}
# Extract the onset data points only:
offset_data_exp2 = data_exp2 %>% filter(SOA_lock == "offset")
offset_model_exp2 <- glmer(formula = latency_aud ~ 1 + f_duration * c_task_relevant * f_SOA + (1 | sub_id),
family=Gamma(link="log"), data = offset_data_exp2)
summary(offset_model_exp2)
anova_results <- Anova(offset_model_exp2)
write.csv(anova_results, file = file.path(save_dir, "Experiment2-offset_mdl_anovas.csv"), row.names = TRUE)
anova_results
```
## Offset model separately for each T1 durations:
Modelling pupil peak latency of offset trials separately for each T1 duration:
```{r Offset model per duration:}
# ========================================================
# Short trials:
offset_data_short_exp2 = offset_data_exp2 %>% filter(duration == 0.5)
# Modelling:
offset_model_short_exp2 <- glmer(formula = latency_aud ~ 1 + c_task_relevant * f_SOA + (1 | sub_id),
family=Gamma(link="log"), data = offset_data_short_exp2)
summary(offset_model_short_exp2)
anova_results <- Anova(offset_model_short_exp2)
write.csv(anova_results, file = file.path(save_dir, "Experiment2-offset_short_mdl_anova.csv"), row.names = TRUE)
anova_results
# ========================================================
# Intermediate trials:
offset_data_int_exp2 = offset_data_exp2 %>% filter(duration == 1.0)
# Modelling:
offset_model_int_exp2 <- glmer(formula = latency_aud ~ 1 + c_task_relevant * f_SOA + (1 | sub_id),
family=Gamma(link="log"), data = offset_data_int_exp2)
summary(offset_model_int_exp2)
anova_results <- Anova(offset_model_int_exp2)
write.csv(anova_results, file = file.path(save_dir, "Experiment2-offset_long_mdl_anova.csv"), row.names = TRUE)
anova_results
# ========================================================
# Long trials:
offset_data_long_exp2 = offset_data_exp2 %>% filter(duration == 1.5)
# Modelling:
offset_model_long_exp2 <- glmer(formula = latency_aud ~ 1 + c_task_relevant * f_SOA + (1 | sub_id),
family=Gamma(link="log"), data = offset_data_long_exp2)
summary(offset_model_long_exp2)
anova_results <- Anova(offset_model_long_exp2)
write.csv(anova_results, file = file.path(save_dir, "Experiment2-offset_long_mdl_anova.csv"), row.names = TRUE)
anova_results
```