# Reference
Kieslich, P. J., & Henninger, F. (2017). Mousetrap: An integrated, open-source mouse-tracking package. _Behavior Research Methods, 49_(5), 1652-1667.
https://doi.org/10.3758/s13428-017-0900-z

# General preparations

## Load libraries
```{r,message=FALSE}
library(mousetrap)
library(ggplot2)
library(dplyr)
```

## Custom ggplot2 theme
```{r}
theme_set(theme_classic()+ 
  theme(
    axis.line = element_line(colour = "black"),
    axis.ticks = element_line(colour = "black"),
    axis.text = element_text(colour = "black"),
    panel.border = element_rect(colour = "black", fill=NA)
  ))

options(width=90)
```



# Data preparation

## Read in merged raw data
```{r}
# Read in raw data from csv.gz file
# (commented out as the raw data are now included in the mousetrap R package)
# raw_data <- read.csv("merged_data/raw_data.csv.gz",stringsAsFactor=FALSE)

# Instead, get raw data directly from data stored in mousetrap package
raw_data <- KH2017_raw
```

## Filter trials
```{r}
# Look at percent of correct responses per Condition
with(raw_data,table(Condition, correct)/c(table(Condition)))

# Exlude incorrect trials
raw_data <- subset(raw_data, correct==1)

# Check that each subject has at least one typical and atypical trial left
table(with(raw_data,table(subject_nr,Condition))>0)
```


## Import mouse-tracking data
```{r}
mt_data <- mt_import_mousetrap(raw_data)
```


## Trajectory preprocessing
```{r}
mt_data <- mt_remap_symmetric(mt_data)
mt_data <- mt_align_start(mt_data)
mt_data <- mt_measures(mt_data)
mt_data <- mt_time_normalize(mt_data)
```


# Analyses

## Logging resolution
```{r}
mt_check_resolution(mt_data, desired = 10)
```

## Plot average time-normalized tajectories
```{r}
mt_plot_aggregate(mt_data, use = "tn_trajectories", points=TRUE,
  x = "xpos", y = "ypos", color = "Condition", subject_id = "subject_nr")+
  theme(legend.position=c(.2,.2))+
  xlab("x coordinate (px)") + ylab("y coordinate (px)")+
  scale_color_grey(start = .55, end = .20)

# ggsave("figures/Figure5.png",width=8.5, height=10,unit="cm",dpi=1200)
# ggsave("figures/Figure5.pdf",width=8.5, height=10,unit="cm")
# ggsave("figures/Figure5.eps",width=8.5, height=10,unit="cm")
```

## Comparison of measures based on time-normalized vs. raw trajectories
```{r}
mt_data <- mt_measures(mt_data,use="tn_trajectories",save_as="tn_measures")
diag(cor(mt_data$measures[,-1],mt_data$tn_measures[,-1]))
```

## Comparison of MAD aggregated per participant
```{r}
agg_mad <- mt_aggregate_per_subject(mt_data, subject_id = "subject_nr",
  use_variables = "MAD", use2_variables = "Condition")

agg_mad %>%
  group_by(Condition) %>%
  select(MAD,Condition) %>%
  summarize_all(.funs = c("mean","sd"))

t.test(MAD~Condition, data=agg_mad, paired=TRUE)

# Cohen's dz
mad_diff <- with(agg_mad,MAD[Condition=="Atypical"]-MAD[Condition=="Typical"])
mean(mad_diff)/sd(mad_diff)

```

## Bimdality analyses
```{r}
# Standardize MAD per participant
mt_data <- mt_standardize(mt_data,
  use_variables = "MAD", within = "subject_nr",prefix = "z_")

# Calculate bimodality coefficient
mt_check_bimodality(mt_data,use_variables = "z_MAD",
  grouping_variables = "Condition", methods = "BC")

# Merge trial level data (needed for distribution qplot with facets)
results <- merge(mt_data$data,mt_data$measures,by="mt_id")

# Distribution
qplot(x=z_MAD,data=results,bins=50)+facet_grid(Condition~.)

```

## Repeat analyses without large MAD values
```{r}
# Exclude trials with abs(z_MAD)>1.5
table(abs(mt_data$measures$z_MAD)<=1.5)/nrow(mt_data$measures)
mt_data_sub <- mt_subset(mt_data,abs(z_MAD)<=1.5,check="measures")

# Check that each subject has at least on typical and atypical trial left
table(with(mt_data_sub$data,table(subject_nr,Condition))>0)

# Comparison of MAD aggregated per participant
agg_mad_sub <- mt_aggregate_per_subject(mt_data_sub, subject_id = "subject_nr",
  use_variables = "MAD", use2_variables = "Condition")

agg_mad_sub %>%
  group_by(Condition) %>%
  select(MAD,Condition) %>%
  summarize_all(.funs = c("mean","sd"))

t.test(MAD~Condition, data=agg_mad_sub, paired=TRUE)
```

