##Script used to calculate Trial-level Bias Scores. Your mileage may vary with this script, depending on how your data ##is compiled. Good luck! ##A few annotations: ##The variable "dir" is the directory where your result files are stored. This script will read CSV files. ##The variable "Y" is the raw reaction time. ##The variable "Cond" is the trial condition. Here, "0" is a neutral trial (no threat), "1" is a congruent trial ## where the threat is cued, and "2" is an incongruent trial where the threat is not cued. ##You may want to adjust the "for (j in 1:5) {" line of code depending on the proximity you're aiming for ## re: your trial-level bias. This is set up to look for neighboring trials with five trials above or below. ##The output will spit out the data files with a new column for the TL-BS score for each eligible trial. Check at least ## one participant by hand so that you can be sure there are no glaring differences in the way your data-set was analyzed ## by this script. ##If you have any questions, drop a line at tdaniel@westfield.ma.edu. dir <-"C:\\Users\\Alex\\Desktop\\Bardeen\\" listing <- list.files(dir) TotalSessions <- length(listing) sess = 1 for (i in 1:TotalSessions){ db <- paste(dir,listing[sess], sep="") data <- read.csv(db) colnames(data)[34]<-"Y" colnames(data)[46]<-"Cond" df<- data.frame(data) getclosest <- function(df, i) { if ((as.character(df$Cond[[i]]) == "0") | (as.character(df$Cond[[i]]) == "1")) { return (NA) } else { # df$Y[i] below <- NA above <- NA aj <- NA bj <- NA for (j in 1:5) { pos <- i - j if (pos < 1) break if (df$Cond[pos] == "1") { below <- df$Y[pos] bj <- j break } } for (j in 1:5) { pos <- i + j if (pos > nrow(df)) break if (df$Cond[pos] == "1") { above <- df$Y[pos] aj <- j break } } temp = NA tempb = NA if (is.na(above) && is.na(below)) { return(NA) } if (!is.na(below) && !is.na(above)) { if (aj < bj) { return(df$Y[i] - above) } else { return(df$Y[i] - below) } } if (!is.na(below)) { tempb <- (df$Y[i] - below) return (tempb) } if (!is.na(above)) { tempa <- (df$Y[i] - above) return(tempa) } return (NA) } } getclosest2 <- function(df, i) { if ((as.character(df$Cond[[i]]) == "0") | (as.character(df$Cond[[i]]) == "2")) { return (NA) } else { # df$Y[i] below <- NA above <- NA aj <- NA bj <- NA for (j in 1:5) { pos <- i - j if (pos < 1) break if (df$Cond[pos] == "2") { below <- df$Y[pos] bj <- j break } } for (j in 1:5) { pos <- i + j if (pos > nrow(df)) break if (df$Cond[pos] == "2") { above <- df$Y[pos] aj <- j break } } temp = NA tempb = NA if (is.na(above) && is.na(below)) { return(NA) } if (!is.na(below) && !is.na(above)) { if (aj < bj) { return(above - df$Y[i]) } else { return(below - df$Y[i]) } } if (!is.na(below)) { tempb <- (below - df$Y[i]) return (tempb) } if (!is.na(above)) { tempa <- (above - df$Y[i]) return(tempa) } return (NA) } } v <- c() # length=nrow(df)) for (i in 1:nrow(df)) { v[i] <- getclosest(df, i) } w <- c() # length=nrow(df)) for (i in 1:nrow(df)) { w[i] <- getclosest2(df, i) } TrialBias = rowSums(cbind(v, w), na.rm=TRUE) oka <- data.frame(df, TrialBias) db2 <- paste(dir,"t",listing[sess], sep="") write.table(oka, file=db2, row.names=F, col.names=T, append=T, sep=",") data = NULL df = NULL oka = NULL sess = sess + 1 }