Preparing your data for use with eyetrackingR

Our Experiment: Each eyetrackingR vignette uses the eyetrackingR package to analyze real data from a simple 2-alternative forced choice (2AFC) word recognition task administered to 19- and 24-month-olds. On each trial, infants were shown a picture of an animate object (e.g., a horse) and an inanimate object (e.g., a spoon). After inspecting the images, they disappeared and they heard a label referring to one of them (e.g., "The horse is nearby!"). Finally, the objects re-appeared on the screen and they were prompted to look at the target (e.g., "Look at the horse!").

Overview of this vignette

This vignette will cover the basics of preparing your data for use with eyetrackingR.

Your Data

eyetrackingR is designed to deal with data in a (relatively) raw form, where each row specifies a sample. Each row should represent an equally spaced unit of time (e.g., if your eye-tracker's sample rate is 100hz, then each row corresponds to the eye-position every 10ms).

This is in contrast to the more parsed data that the software bundled with eye-trackers can sometimes output (e.g., already parsed into saccades or fixations). For eyetrackingR, the simplest data is the best.

This also maximizes compatibility: eyetrackingR will work with any eye-tracker's data (e.g., Eyelink, Tobii, etc.), since it requires the most basic format.

Note: eyetrackingR does not handle reading your data into R. Most software bundled with your eyetracker should be capable of exporting your data to a delimited format (.csv, tab-delimited .txt), etc. From there, you can use base functions like read.delim, or (recommended) check out the package readr.


eyetrackingR just needs to the following columns:

There are also some optional columns, which you might want to use depending on your analysis:

If your dataset has these columns, you're ready to begin using eyetrackingR.

Data Preparation

Load dataset and dependencies, set data options for eyetrackingR.

Before being used in eyetrackingR, data must be run through the make_eyetrackingr_data function.

This lets you provide the information about your dataset that was just described above. The function will perform some checks on your data to make sure it's in the correct format.

For this dataset, because each participant saw each item only once in this experiment, trial_column specifies a unique name for each trial (e.g., "FamiliarCow") and we don't specify an item_column.

set.seed(42)

library("Matrix")
library("lme4")
library("ggplot2")
library("eyetrackingR")

data("word_recognition")
data <- make_eyetrackingr_data(word_recognition, 
                               participant_column = "ParticipantName",
                               trial_column = "Trial",
                               time_column = "TimeFromTrialOnset",
                               trackloss_column = "TrackLoss",
                               aoi_columns = c('Animate','Inanimate'),
                               treat_non_aoi_looks_as_missing = TRUE
)

Dealing with Non-AOI Looks

You might be wondering about the treat_non_aoi_looks_as_missing argument above.

Almost all eyetracking analyses require calculating proportion looking--across a trial, within a time bin, etc. One important choice you as the researcher have to make is whether to include non-AOI looking in this calculation. There are two options:

The argument treat_non_aoi_looks_as_missing lets you decide which of these options eyetrackingR will do. If set to TRUE, when it comes time for eyetrackingR to calculate proportion looking to an AOI, this will be calculated as "time looking to that AOI divided by time looking to all other AOIs." In contrast, if this parameter is set to FALSE, proportion looking to an AOI will be calculated as "time looking to that AOI divided by total time looking (excluding actual trackloss)."

Cleaning Up Messy Data:

We all wish our data came right out of our eye-tracker ready for analysis, but this isn't always the case. Two of the more annoying problems you might encounter are:

Luckily, eyetrackingR has tools to address both of these problems

Adding an Area-of-Interest

Your eyetracking data doesn't have any columns corresponding to areas of interest. However, it does have columns give you the x,y gaze coordinates. You also have a csv file for each AOI, specifying its boundaries on each type of trial.

In that case, it's easy to add AOIs to your dataframe:

animate_aoi <- read.csv("./interest_areas_for_animate_aoi.csv")

#            Trial Left Top Right Bottom
# 1   FamiliarBird  500 100   900    500
# 2 FamiliarBottle  400 200   800    600
# 3    FamiliarCow  500 300   900    700
# 4    FamiliarDog  300 100   700    500
# 5  FamiliarHorse  500 200   900    600
# 6  FamiliarSpoon  350 300   750    700

data <- add_aoi(data = data, aoi_dataframe = animate_aoi, 
               x_col = "GazeX", y_col = "GazeY", 
               aoi_name = "Animate",
               x_min_col = "Left", x_max_col = "Right", y_min_col = "Top", y_max_col = "Bottom")

This can be done for each AOI: just load in a csv file and run the add_aoi function for each.

After using this function, you should probably check that the added AOI column actually indicates that the gaze was ever in the AOI. For example:

table(data$Animate)
table(is.na(data$Animate)) # if all TRUE, then something went wrong.

(Note that you should typically add your AOIs to your dataframe before running make_eyetrackingr_data, since that function will check your AOIs.)

Subsetting into the Time-Window of Interest Across Trials

eyetrackingR's subset_by_window has several methods for getting the data you're interested in. These are powerful because they can be used repeatedly/iteratively to home in on the relevant data. We show this below.

data$Message <- with(data, ifelse(TimeFromTrialOnset==0, "TrialStart", "."))
data$ResponseWindowStart <- 15500 

In this example, let's imagine that our Timestamp doesn't actually specify the start of the trial-- instead, it specifies the time since the eye-tracker was turned on!

Fortunately, our eye-tracker sends a message when each trial starts (this is not always the same as the very first sample for the trial-- recording often starts a few hundred milliseconds before the trial does). This message can be used to set the zero-point for each trial.

data <- subset_by_window(data, window_start_msg = "TrialStart", msg_col = "Message", rezero= TRUE)

Unfortunately, the eye-tracker didn't send a message for when the response-window starts. Instead, it added a column that tells you how long after the start of the trial the response-window started. Now that we have rezero'd our data so that 0 = trial-start, this column specifying the time after trial start can be used easily.

response_window <- subset_by_window(data, window_start_col = "ResponseWindowStart", rezero= FALSE, remove= TRUE)

Finally, our trials always ended after 21 seconds. So we'll simply remove data from after this.

response_window <- subset_by_window(response_window, window_end_time = 21000, rezero= FALSE, remove= TRUE)

In summary, we have subset the data to focus on our time window of interest.

Dealing with trackloss

Trackloss occurs when the eye-tracker loses track of the participant's eyes (e.g., when they turn away or blink) or when it captures their gaze location but with very low validity.

We need to decide which trials to remove (if any) due to very high trackloss. To do so here, we will:

# analyze amount of trackloss by subjects and trials
(trackloss <- trackloss_analysis(data = response_window))

response_window_clean <- clean_by_trackloss(data = response_window, trial_prop_thresh = .25)

How much data are we left with?

After data cleaning, it's important to assess how much data you are ultimately left with to (a) report along with your findings and, (b) identify any problematic participants who didn't contribute enough trials from which to reliably estimate their performance.

Assess mean trackloss for each participant

trackloss_clean <- trackloss_analysis(data = response_window_clean)

(trackloss_clean_subjects <- unique(trackloss_clean[, c('ParticipantName','TracklossForParticipant')]))

Summarize samples contributed per trial

# get mean samples contributed per trials, with SD
mean(1 - trackloss_clean_subjects$TracklossForParticipant)
sd(1- trackloss_clean_subjects$TracklossForParticipant)

Assess number of trials contributed by each participant

# look at the NumTrials column
(final_summary <- describe_data(response_window_clean, describe_column = 'Animate', group_columns = 'ParticipantName'))

Summarize number of trials contributed

mean(final_summary$NumTrials)
sd(final_summary$NumTrials)

Create additional columns needed for analysis

Now is the time to make sure that we have all the columns needed for our analyses, because this dataset is going to be shaped and subsetted as we analyze our data and it's easier to add these columns once then to do it for derivative datasets.

For the present experiment, one thing we want to do is create a "Target" condition column based on the name of each Trial.

In each trial, the participant was told to look at either an Animate or Inanimate objects. Here we create a column specifying which for each column.

response_window_clean$Target <- as.factor( ifelse(test = grepl('(Spoon|Bottle)', response_window_clean$Trial), 
                                       yes = 'Inanimate', 
                                       no  = 'Animate') )

Our dataset is now ready for analysis!



Try the eyetrackingR package in your browser

Any scripts or data that you put into this service are public.

eyetrackingR documentation built on Sept. 15, 2023, 5:08 p.m.