Eyetracking R Script onset for third sentence average for each item

PennController for IBEX Forums Support Eyetracking R Script onset for third sentence average for each item

Viewing 1 post (of 1 total)
  • Author
  • #10650

    R script below I need to map the duration of the third setnece in each item but average across participants who have that item. I see the ET data is by trial number but to translate into item number is too difficult.
    1. How to change trial number to item name in the results file? (maybe this will make it easier a get average by item name across participants with that item)
    2. I have an excel with average 3rd sentence onset for each item but I need to incroprate that into the results to find that average number, make new column and set to zero in order to just analyze start to finish the third sentence. I have search everywhere no luck.

    read.pcibex <- function(filepath, auto.colnames=TRUE, fun.col=function(col,cols){cols[cols==col]<-paste(col,"Ibex",sep=".");return(cols)}) {
      n.cols <- max(count.fields(filepath,sep=",",quote=NULL),na.rm=TRUE)
      if (auto.colnames){
        cols <- c()
        con <- file(filepath, "r")
        while ( TRUE ) {
          line <- readLines(con, n = 1, warn=FALSE)
          if ( length(line) == 0) {
          m <- regmatches(line,regexec("^# (\\d+)\\. (.+)\\.$",line))[[1]]
          if (length(m) == 3) {
            index <- as.numeric(m[2])
            value <- m[3]
            if (is.function(fun.col)){
              cols <- fun.col(value,cols)
            cols[index] <- value
            if (index == n.cols){
        return(read.csv(filepath, comment.char="#", header=FALSE, col.names=cols))
        return(read.csv(filepath, comment.char="#", header=FALSE, col.names=seq(1:n.cols)))
    # The URL where the data is stored; note the ?experiment= at the end
    ETURL = "https://psyli-lab.research-zas.de/eye-tracking_full_ger/eye-tracking_full_ger_results/php_full_ger.php?experiment="
    # Time-window to bin the looks
    BIN_DURATION = 100
    # We'll use Reception time to identify individual sessions
    results <- read.pcibex("~/Results online study/full_ger_pcibex_june/results.csv")
    names(results)[1] <- 'Participant'
    write.csv(results, file = 'results_clean_RStudio.csv',
              row.names = FALSE)
    # Read ET data file for each session and append output to ETdata (first 5 rows are corrupt)
    ETdata = data.frame()
    filesDF_bak <- subset(results, Parameter=="Filename"&displayID=="c02c")
    filesDF <- filesDF_bak[6:nrow(filesDF_bak),] #add lines after this in order to get ETdata and dat check without trial instead with item name
    apply(filesDF, 1, function(row) {
      data <- read.csv(paste(ETURL,as.character(row[['Value']]),sep=''))
      data$Participant <- row[['Participant']]
      datacheck <<- data
      ETdata <<- rbind(ETdata,data)
    # Bin the data to 100ms since its in every 10s of millisecond
    ETdata$bin <- BIN_DURATION*floor(ETdata$times/BIN_DURATION)
    ETdata <- ETdata %>% group_by(Participant,trial,bin) %>% mutate(
    # Some transformations before plotting
    #  - only keep first row for each bin per participant+trial
    ETdata_toplot <- ETdata %>% group_by(Participant,trial,bin) %>% filter(row_number()==1)
    #  - from wide to long (see http://www.cookbook-r.com/Manipulating_data/Converting_data_between_wide_and_long_format/)
    ETdata_toplot <- gather(ETdata_toplot, focus, gaze, Right:Left) #top_female:bottom_male)
    #Save the excel files below and make changes to these files to get the average onset for third sentence
    write.csv(ETdata, file = 'ETdata_fullGer_RStudio.csv',
              row.names = FALSE)
    write.csv(ETdata_toplot, file = 'ETdata_topplot_fullGer_RStudio.csv',
              row.names = FALSE)
    # Plot the results
    ger_plot = ggplot(ETdata_toplot, aes(x=bin,y=gaze,color=focus)) +
    ger_plot + xlim(7242.4875,18000)+ 
      geom_vline(xintercept=c(7242.4875,12806.8875,15975.90), linetype="dashed") +
      annotate("text", x=c(13000), y=.14, label=c("X=12806.8875  Aver.3rd sentence onset"), angle=90) + annotate("text", x=c(7355), y=.14, label="X=7242.4875  Aver.2nd sentence onset", angle=90) + annotate("text", x=c(16170), y=.14, label=c("X=15975.90  Aver.3rd sentence completed"), angle=90)
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.