forked from CrumpLab/LabJournalWebsite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataWrangleFlankerAV.Rmd
More file actions
222 lines (153 loc) · 7.35 KB
/
DataWrangleFlankerAV.Rmd
File metadata and controls
222 lines (153 loc) · 7.35 KB
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
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message = FALSE)
```
## Flanker task
In a flanker task, participants identify a central stimulus (as quickly and accurately) as possible, while ignoring distracting stimuli presented on the left or right of the central stimulus (the flankers).
The data for this assignment come from a flanker task where participants responded to many flanker stimuli over several trials.
## Load the data and libraries you will use
```{r}
library(data.table)
library(dplyr)
library(ggplot2)
library(bit64)
# get the file names
file_names <- list.files(path="FlankerData")
# create headers for each column
the_headers <- c("stimulus","congruency","proportion",
"block","condition","dualtask","unknown",
"stimulus_onset","response_time","response","subject")
# Load data
# create empty dataframe
all_data<-data.frame()
# loop to add each file to the dataframe
for(i in file_names){
one_subject <- fread(paste("FlankerData/",i, sep=""))
names(one_subject) <- the_headers
one_subject$subject <- rep(i,dim(one_subject)[1])
one_subject <- cbind(one_subject, trial= 1:dim(one_subject)[1])
all_data <- rbind(all_data,one_subject)
}
```
## Pre-processing
Create an accuracy column that codes whether the response was correct or incorrect on each trial
```{r}
center_let<-unlist(lapply(strsplit(all_data$stimulus,""),
FUN=function(x)unlist(x)[2]))
all_data<-cbind(all_data,center_let)
all_data<-all_data %>%
mutate(response=tolower(response),
center_let=tolower(center_let),
accuracy=response==center_let)
```
### Get Reaction time on each trial
Add a column that calculates the reaction time on each trial.
```{r}
# Somehow the stimulus and the response time are the same number?
all_data <- all_data %>%
mutate(RT = as.integer(response_time - stimulus_onset))
```
## Checks
Check how many trials each subject completed in the congruent and incongruent conditions, the mean accuracy for each subject in each congruency condition, and the mean RT for each subject in each congruency condition.
```{r}
Sub_Trials <- all_data %>%
mutate(subject = as.factor(subject),
congruency = as.factor(congruency)) %>%
group_by(subject,congruency) %>%
summarise(num_trials = length(RT),
mean_RT = mean(RT),
mean_accuracy = mean(accuracy))
knitr::kable(Sub_Trials)
```
### Exclusion
Exclude all RTs that are longer than 2000 ms
```{r}
# Reaction Time =0 somehow the stimulus and response are the same number
Slow_RT <- all_data %>%
filter(RT < 2000)
```
## Analysis
### Reaction Time analysis
1. Get the individual subject mean reaction times for **correct** congruent and incongruent trials.
```{r}
Subject_Trials_meanRTs <- Slow_RT %>%
filter(accuracy == TRUE) %>%
group_by(subject,congruency) %>%
summarise(mean_RT = mean(RT))
```
2. Get the overall mean RTs and SEMs (standard errors of the mean) for the congruent and incongruent condition. Make a table and graph.
```{r}
congruency_means <- Subject_Trials_meanRTs %>%
group_by(congruency) %>%
summarise(meanRT = mean(mean_RT),
SEM = sd(mean_RT)/sqrt(length(mean_RT)))
knitr::kable(congruency_means)
ggplot(congruency_means, aes(x=congruency,y=meanRT,
fill=congruency))+
geom_bar(stat="identity")+
theme_classic(base_size=12)+
ylab("Mean Reaction Time ")+
geom_errorbar(aes(ymin=meanRT-SEM,
ymax=meanRT+SEM),
position=position_dodge(width=0.9),
width=.2,
color="black")+
coord_cartesian(ylim=c(100,1000))
```
3. Compute the flanker effect for each subject, taking the difference between their mean incongruent and congruent RT. Then plot the mean flanker effect, along with the SEM of the mean flanker effect
**tip:** Not all problems have an easy solution in dplyr, this is one them. You may have an easier time using logical indexing of the dataframe to solve this part.
```{r}
library(ggplot2)
flanker_means <- subject_meanRTs[subject_meanRTs$congruency=="I",]$mean_RT -
subject_meanRTs[subject_meanRTs$congruency=="C",]$mean_RT
flanker_df <- data.frame(dv = "flanker effect",
flanker_mean = mean(flanker_means),
SEM = sd(flanker_means)/sqrt(length(flanker_means)))
ggplot(flanker_df, aes(x=dv, y=flanker_mean))+
geom_bar(stat="identity")+
theme_classic(base_size=12)+
ylab("Mean Flanker Effect")+
geom_errorbar(aes(ymin=flanker_mean-SEM,
ymax=flanker_mean+SEM),
position=position_dodge(width=0.9),
width=.2,
color="black")
```
### Exploratory analysis
Multiple questions may often be asked of data, especially questions that may not have been of original interest to the researchers.
In flanker experiments, like this one, it is well known that the flanker effect is modulated by the nature of the previous trial. Specifically, the flanker effect on trial n (the current trial), is larger when the previous trial (trial n-1) involved a congruent item, compared to an incongruent item.
Transform the data to conduct a sequence analysis. The dataframe should already include a factor (column) for the congruency level of trial n. Make another column that codes for the congruency level of trial n-1 (the previous trial). This creates a 2x2 design with trial n congruency x trial n-1 congruency.
First get teh subject means for each condition, then create a table and plot for teh overall means and SEMs in each condition. This should include:
1. trial n congruent : trial n-1 congruent
2. trial n incongruent : trial n-1 congruent
3. trial n congruent : trial n-1 incongruent
4. trial n incongruent : trial n-1 incongruent
**tip:** be careful, note that the first trial in each experiment can not be included, because it had no preceding trial
```{r}
library(dplyr)
previous_congruency <- c(0,all_data$congruency[1:(length(all_data$congruency)-1)])
sequence_subjects <- all_data %>%
mutate(n1_congruency = previous_congruency) %>%
filter(trial > 1,
RT < 2000,
accuracy == TRUE) %>%
group_by(subject,congruency,n1_congruency) %>%
summarise(meanRT = mean(RT))
sequence_means <- sequence_subjects %>%
group_by(congruency,n1_congruency) %>%
summarise(mean_RT = mean(meanRT),
SEM = sd(meanRT)/sqrt(length(meanRT)))
knitr::kable(sequence_means)
ggplot(sequence_means, aes(x=n1_congruency, y=mean_RT,
group=congruency,
fill=congruency))+
geom_bar(stat="identity", position="dodge")+
theme_classic(base_size=12)+
ylab("Mean RT (ms")+
geom_errorbar(aes(ymin=mean_RT-SEM,
ymax=mean_RT+SEM),
position=position_dodge(width=0.9),
width=.2,
color="black")+
coord_cartesian(ylim=c(600,900))
```
[link to Presentation](https://13vasquezan.github.io/RClassWeb/Presentation-Final#1)