# Ctrl+Shift+Enter to run entire document ################################################################################################### #IMPORT LIBRARIES, IMPORT DATA ############# import libraries library(plyr) #data manipulator package library(ggplot2) #GGplot package for visualizing data library(reshape2) library(grid) library(gridExtra) library(afex) #easy ANOVA package library(emmeans) data <- read.csv("raw_data.csv") ################################################################################################### ############# format data #convert columns to factors data$sbj_num <- factor(data$sbj_num) #Participant data$block <- factor(data$block) #Experiment block (80 trials per block) data$compatibility <- factor(data$compatibility) #IV1 data$delay <- factor(data$delay) #IV2 data$stimulus <- factor(data$stimulus) #Cue color data$corr_resp <- factor(data$corr_resp) #Sequence data$group <- factor(data$group) #IV3 (1 = fixed order of sequences, 2 = pseudorandom order) data$trialnum <- as.numeric(as.character(data$trialnum)) #Trial number data$blocksection[data$trialnum<21] <- "1" data$blocksection[data$trialnum<41&data$trialnum>20] <- "2" data$blocksection[data$trialnum<61&data$trialnum>40] <- "3" data$blocksection[data$trialnum<81&data$trialnum>60] <- "4" data$trialnum <- factor(data$trialnum) #convert columns with dependent variables to numbers data$rt1 <- as.numeric(as.character(data$rt1)) #DV 1 - Initiation time (response time from cue to first keypress) data$rt2 <- as.numeric(as.character(data$rt2)) #response time from first to second keypress data$rt3 <- as.numeric(as.character(data$rt3)) #response time from second to third keypress data$rt <- as.numeric(as.character(data$rt)) #sum all inter-response times #convert rts from sec to ms units data$rt1 <- data$rt1 * 1000 data$rt2 <- data$rt2 * 1000 data$rt3 <- data$rt3 * 1000 data$rt <- data$rt * 1000 #convert accuracy to binary, numeric values data$acc <- revalue(data$accuracy, c(" True"=1, " False"=0)) data$acc <- as.numeric(as.character(data$acc)) #Here get rid of practice trials data <- subset(data, compatibility!=" practice") data <- subset(data, block!=" practice") #create block numbers data$blocknum <- rep(1:4, times=40, each=80) data$blocknum <- factor(data$blocknum) #Create variables for cue and sequence transitions (include trial 1, it will be deleted later) data$cue <- "switch" #default value data$cue[which(data$stimulus[2:nrow(data)]==data$stimulus[1:nrow(data)-1])+1] <- "repetition" data$seq <- "switch" #default value data$seq[which(data$corr_resp[2:nrow(data)]==data$corr_resp[1:nrow(data)-1])+1] <- "repetition" data$cue <- factor(data$cue) data$seq <- factor(data$seq) data$transition <- interaction(data$cue,data$seq) #IV4 data$transition <- revalue(data$transition, c("repetition.repetition"="both_rep", "switch.switch"="both_switch", "switch.repetition"="cue_switch")) #Create variable for trials following an error (including trial 1, deleted later) data$acc_previous <- 1 #default value data$acc_previous[which(data$acc[1:nrow(data)-1]==0)+1] <- 0 data$acc_compare <- interaction(data$acc, data$acc_previous) #Here exclude trial 1 (needed above, should be omitted below for rt cleaning) data <- subset(data,data$trialnum!=1) ################################################################################################### ############# clean data ### Exclusion of trials: # practice (done), first trial (done), RTs more than 3sds away from the mean (below) #divide the data by the two groups data1 <- subset(data,data$group=="1") data2 <- subset(data,data$group=="2") #keep track of trials retained pre-to-post cleaning num_alltrials <- nrow(data1) # h1rt1 <- hist(data1$rt1) # h1rt2 <- hist(data1$rt2) # h1rt3 <- hist(data1$rt3) # h2rt1 <- hist(data2$rt1) # h2rt2 <- hist(data2$rt2) # h2rt3 <- hist(data2$rt3) #Exclude too long RTs across all subjects maxrt <- function(x){mean(x) + (3*sd(x))} maxrts<-apply(data1[c("rt1","rt2","rt3")],2,maxrt) data1 <- subset(data1, abs(data1$rt1)