Eyetracking R script variations

PennController for IBEX Forums Support Eyetracking R script variations

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #10654
    multanip
    Participant

    Hello
    I am working on R analysis for one of our pcibex projects. I have an initial script that gave me a graph (with your help through email) but I want a more detail.

    I am able to see the gaze on average of all participant for left and right image (I get two lines when plotting, one for left and one for right)

    But, I want to go further and also plot by item name starting at average THIRD SENTENCE onset gaze for all participant that have that item (three senteces per item-audio)
    -Average third onset is not marked yet in results or ETdata but would like to use R to create new column in ET data or ETdataplot that finds the average for each of the items and then sets the average to zero and plots the rest of the third sentence

    I need to change and add to script several things but I will start one thing:

    IDEA 1. ET data is by trial number—but I need to change so it show DisplayID or item
    name from results file
    a. Tried by adding to code and showed a column DisplayID but it showed
    the same DisplayID for all items (trial 15 and trial 17 are not the
    same item but its gives p10a for all of them for all the
    participants also)

    small part of the Rscript that outputs the ETdata and fileDF

    ETdata = data.frame()
    filesDF_bak <- subset(results, Parameter=="Filename"&displayID=="p10a") #tried to change the display ID to setnence but still showed p10a displayID for all
    filesDF <- filesDF_bak[6:nrow(filesDF_bak),] 
    apply(filesDF, 1, function(row) {
      data <- read.csv(paste(ETURL,as.character(row[['Value']]),sep=''))
      data$Participant <- row[['Participant']]
      data$displayID <- row[['displayID']] ##added this later and it showed the display id but only p10a for all
      datacheck <<- data
      ETdata <<- rbind(ETdata,data)
    })

    How I change code so that it shows the item name for each of the trial number? Trial number is very confusing, how can I plot by item name (average third sentence onset)
    in the ET DATA

    #10655
    multanip
    Participant

    IF this is too confusing, I can send through email.

    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) {
            break
          }
          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){
              break
            }
          }
        }
        close(con)
        return(read.csv(filepath, comment.char="#", header=FALSE, col.names=cols))
      }
      else{
        return(read.csv(filepath, comment.char="#", header=FALSE, col.names=seq(1:n.cols)))
      }
    }
    require("dplyr")
    require("ggplot2")
    require("tidyr")
    # 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_RStudio2.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=="p10a") #tried to change the display ID to setnence but still showed p10a displayID for all
    filesDF <- filesDF_bak[6:nrow(filesDF_bak),] 
    apply(filesDF, 1, function(row) {
      data <- read.csv(paste(ETURL,as.character(row[['Value']]),sep=''))
      data$Participant <- row[['Participant']]
      data$displayID <- row[['displayID']] ##added this later and it showed the display id but only p10a for all
      datacheck <<- data
      ETdata <<- rbind(ETdata,data)
    })
    • This reply was modified 10 months, 3 weeks ago by multanip.
    • This reply was modified 10 months, 3 weeks ago by multanip.
    • This reply was modified 10 months, 3 weeks ago by multanip.
    #10658
    multanip
    Participant
    # apply sections lines are run, get ETdata with trial number, data check alaso add lines after this in order to get ETdata and dat check without trial instead with item name
    
    # 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(
      Right=mean(X_Right),
      #Middle=mean(X_Middle),
      Left=mean(X_Left),
    )
    
    # 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)
    #(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)) +
      geom_line(stat="summary",fun="mean")
    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)
    #10661
    multanip
    Participant

    ADDITIONAL NOTE: pennElementName or order.number.of.item column is difficult to use in the same list, same item has different numbers. FOr example,

    In list A, item16c.wav (c16c): 1 participant has PennElementno. =59 and order no. =60, 2nd participant same item PennElementno. =60 and order no. =60, 3rd participant same item PennElementno. =47 and order no. =47.

    I need to see how particpants reacted to each item at average onset of the third sentence in each item. Using number is difficult.

    #10670
    Jeremy
    Keymaster

    Hello,

    Unfortunately these questions are rather specific to your project, and much more about transforming data using R than about using PCIbex, so I won’t be answering them here. I suggest you reach out to a peer with good R skills to assist you with your analyses, since once you have loaded your results file in a table, nothing crucially relies on PCIbex and someone familiar enough with R should be able to provide support

    Jeremy

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.