---
# System prepended metadata

title: CogScie eye-tracking lab Fall 2022

---

---
title: "CogScie eye-tracking lab Fall 2022"
subtitle: "Data cleaning and sanity checks"
author: "Fabio Trecca"
date: "8/12/2022"
output: html_document
---

```{r setup, include=FALSE}
require(knitr)
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```

# Load in the Visual Search experiment data
```{r}
Fixations <- read_csv("Data/Fixations.csv")
Saccades <- read_csv("Data/Saccades.csv")
```

# Sanity checks

The first thing we want to do is to run some sanity checks on the sample data (x and y coordinates) to make sure everything is in order. Notice anything wrong?

```{r}
## Check distribution of raw samples. Is everything alright?
hist(Fixations$PositionX)
hist(Fixations$PositionY)

hist(Saccades$StartX)
hist(Saccades$StartY)
hist(Saccades$EndX)
hist(Saccades$EndY)
```

If needed, remove artifacts and other "illegal" data points. The screen resolution at COBE Lab was 1050 x 1680. Any points outside this range should be excluded. Start by figuring out how frequent these points are. Eg. are some participants more susceptible to outliers than others?

```{r}
range(Fixations$PositionX)
range(Fixations$PositionY)
```

Then filter out these outlier points:

```{r}
Fixations <- Fixations %>% 
  filter(PositionX >= 0 & PositionX <= 1680)

Fixations <- Fixations %>% 
  filter(PositionY >= 0 & PositionY <= 1050)

Saccades <- Saccades %>% 
  filter(StartX >= 0 & StartX <= 1680)

Saccades <- Saccades %>% 
  filter(EndX >= 0 & EndX <= 1680)

Saccades <- Saccades %>% 
  filter(StartY >= 0 & StartY <= 1050)

Saccades <- Saccades %>% 
  filter(EndY >= 0 & EndY <= 1050)

```


## Inspect distribution of no. of fixations and fixation duration by participant/trial both VISUALLY and NUMERICALLY

```{r}
Fixations %>% 
  group_by(ParticipantID) %>% 
  summarise(sum(FixationNo))

Jakob <- Fixations %>% 
  filter(ParticipantID == "Jakob2")

Fixations %>% 
  group_by(ParticipantID) %>% 
  summarise(sum(Duration))

  Fixations <- Fixations %>% 
  filter(ParticipantID != "Jakob2")

Fixations <- Fixations %>% 
  filter(ParticipantID != "maria1")

Fixations <- Fixations %>% 
  filter(ParticipantID != "sword2")

Fixations <- Fixations %>% 
  filter(ParticipantID != "Freddy2")


```

What do you notice? Make sure that nothing looks funky in the data!

## Inspect distribution of no. of saccades and saccade amplitude by participant/trial both VISUALLY and NUMERICALLY

```{r}
Saccades %>% 
  group_by(ParticipantID) %>% 
  summarise(sum(SaccadeNo))

Saccades %>% 
  group_by(ParticipantID) %>% 
  summarise(Amplitude)
  
  Saccades <- Saccades %>% 
  filter(ParticipantID != "Jakob2")

Saccades <- Saccades %>% 
  filter(ParticipantID != "maria1")

Saccades <- Saccades %>% 
  filter(ParticipantID != "sword2")

Saccades <- Saccades %>% 
  filter(ParticipantID != "Freddy2")
```

What do you notice? Make sure that nothing looks funky in the data!

## Inspect distribution of pupil size by participant/trial both VISUALLY and NUMERICALLY

```{r}
Fixations %>% 
  group_by(ParticipantID) %>% 
  summarise(PupilSize)

Fixations <- Fixations %>% 
  mutate(ParticipantID = as.factor(ParticipantID))

Fixations %>% 
  ggplot(aes(x = ParticipantID, y = PupilSize, fill = ParticipantID)) + geom_violin()
  

```

What do you notice? Make sure that nothing looks funky in the data!

## Check distribution of Time variable. Is there anything weird?

```{r}

```

## Let's also do some outlier checks:

```{r}
## Check for outliers in fixation duration using a boxplot

```

```{r}
## Check for outliers in saccade length using a boxplot

```
