# 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)
```


# Import
```{r}
mt_data <- mt_import_mousetrap(KH2017_raw)
```


# Preprocessing

## Spatial transformations
```{r}
# Remap trajectories
mt_data <- mt_remap_symmetric(mt_data)

# Align trajectories to common start position
mt_data <- mt_align_start(mt_data, start=c(0,0))
```

## Resampling
```{r}
# Time-normalize trajectories
mt_data <- mt_time_normalize(mt_data) 
```

## Filtering
```{r}
# Only keep trials with correct answers
mt_data <- mt_subset(mt_data, correct==1)
```

# Analysis

## Aggregate trajectories
```{r}
# Fig. 4
mt_plot_aggregate(mt_data, use="tn_trajectories",
  x="xpos", y="ypos", 
  color="Condition", subject_id="subject_nr")+
  scale_color_manual(values=c("darkorange","steelblue"))
```

## Calculate measures
```{r}
# Calculate velocity and acceleration
mt_data <- mt_derivatives(mt_data)

# Calculate trial-level indices
mt_data <- mt_measures(mt_data)

# Calculate sample entropy
mt_data <- mt_sample_entropy(mt_data, use="tn_trajectories")
```


## Curvature

### Aggregate analyses
```{r}
# Aggregate MAD values per participant and condition
agg_mad <- mt_aggregate_per_subject(mt_data, 
  use_variables="MAD", use2_variables="Condition",
  subject_id="subject_nr")

# Compare aggregated MAD values
t.test(MAD~Condition, data=agg_mad, paired=TRUE)

# Calculate descriptives
agg_mad %>% 
  group_by(Condition) %>% 
  summarise_at("MAD",.funs=c("mean","sd")) %>%
  as.data.frame()
```

### Trial level analyses
```{r, message=FALSE}
# Create data.frame that contains the
# trial variables and mouse-tracking indices
results <- merge(mt_data$data, mt_data$measures, by="mt_id")

# Load afex package
library(afex)

# Run linear mixed model with Condition as a fixed effect
# and a random intercept and slope per participant
mixed(MAD ~ (1+Condition|subject_nr)+Condition, data=results)
```


## Trajectory types

### Heatmap of individual trajectories
Fig. 5, top
```{r}
mt_heatmap(mt_data,
  xres=1000,
  colors=c("white","black"))
```

\pagebreak

### Smoothed heatmap
Fig. 5, middle
```{r}
mt_heatmap(mt_data,
  xres=1000,
  colors=c("white","black"),
  smooth_radius=20,
  n_shades=10,
  mean_image=0.2)
```

\pagebreak

### Difference of heatmaps between conditions
Fig. 5, bottom
```{r}
mt_diffmap(mt_data,
  xres=1000,
  condition=mt_data$data$Condition=="Typical",
  colors=c("steelblue","white","darkorange"),
  smooth_radius=20,
  n_shades=10)
```


\pagebreak

## Temporal analyses

### Average x positions
```{r,fig.width=8}
# Plot aggregate time-normalized x-positions (Fig. 6)
mt_plot_aggregate(mt_data, use="tn_trajectories",
  x="steps", y="xpos", color="Condition",
  subject_id="subject_nr", points=TRUE)+
  scale_color_manual(values=c("darkorange","steelblue"))

# Aggregate time-normalized trajectories per condition
# separately per participant
av_tn_trajectories <- mt_aggregate_per_subject(mt_data,
  use="tn_trajectories", use2_variables="Condition",
  subject_id="subject_nr")

# Paired t-tests on coordinates
xpos_t_tests <- 
  with(av_tn_trajectories,
       sapply(unique(steps),function(i){
         t.test(xpos[Condition=="Atypical" & steps==i],
                xpos[Condition=="Typical" & steps==i],
                paired = TRUE)$p.value})
       )

# Retrieve all significant t-tests
which(xpos_t_tests<.05)
```

\pagebreak

### Riverbed plot
Fig. 7
```{r}
mt_plot_riverbed(mt_data, use="tn_trajectories",
  y="xpos", facet_col="Condition")

```

