---
title: "Aligning Data to a Specific Sample Message"
author: "Vincent Porretta"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Aligning Data to a Specific Sample Message}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---


```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.width=6, fig.height=4, warning=FALSE)
```

```{r, echo=FALSE, eval=TRUE, message=FALSE}
library(VWPre)
data(VWdat)
``` 

Sometimes, depending on the design of the experiment, you may decide not to define an interest period in Data Viewer prior to exporting the data.
If this is the case, there are some functions in VWPre which can help to align the data to your critical stimulus.
Specifically, these functions search for a specific Eyelink message which was read out during the recording sequence.
They will align the samples to this message, and, if necessary, also apply a variable adjustment forward or backward in time.
This depends on whether the message signals the onset of the stimulus itself, or rather, serves as a reference point for the onset of the stimulus.


## Before aligning

In order to perform the alignment, you must first load your sample report and complete the first two preprocessing steps using the functions `prep_data` and `relabel_na` provided in this package. 
These step will ensure that an Event column (unique index of each recording sequence - typically the combination of Subject and Trial) is created. 
A description of these can be found in the [Basic Preprocessing](VWPre_Basic_Preprocessing.html) vignette.

```{r, eval= TRUE, echo=FALSE, results='hide', message=FALSE}
library(VWPre)
# data(VWdat)
# dat0 <- prep_data(data = VWdat, Subject = "RECORDING_SESSION_LABEL", Item = "itemid")
# aligndat <- relabel_na(data = dat0, NoIA = 4)
# aligndat <- slice(aligndat, 1:10000)
# save(aligndat, file = "aligndat.rda", compress = "xz")
load("aligndat.rda")
```

## Aligning to a specific message

Using the function `check_msg_time` you can see that the TIMESTAMP values associated with the message are not the same for each event.
This indicates that alignment is required.
Note that a regular expression (regex) can be used here as the message string.

```{r, eval= TRUE, echo=TRUE, results='asis'}
check_msg_time(data = aligndat, Msg = "VowelOnset")
```

If you are not sure which messages are in your data set, you can use the function `check_all_msgs`. 
This will print all the messages (along with Event, Subject, Trial, and Timestamp).
By specifiying `ReturnData = TRUE`, you can assign the output to a dataframe for further sorting, etc.

```{r, eval= TRUE, echo=TRUE, results='asis'}
check_all_msgs(data = aligndat)
```

The function `align_msg` is used to perform the alignment. 
To do this you must specify the message text as a string to the parameter `Msg`.
Again, this string can contain a regular expression (regex) on which to locate the message.
The function finds the instance of the message for each event and sets that sample as the zero point.
Consequently, this creates a new column called `Align`, which represents the time sequence relative to the message.

```{r, eval= TRUE, echo=TRUE, results='asis'}
aligned1 <- align_msg(data = aligndat, Msg = "VowelOnset")
```

If we check the message time again, we now see that the message occurs at time 0 in the `Align` column.

```{r, eval= TRUE, echo=TRUE, results='asis'}
check_msg_time(data = aligned1, Msg = "VowelOnset")
```

To fully examine all events, you can include the parameter `ReturnData=TRUE` and assign the output to an object in your environment.

```{r, eval= FALSE, echo=TRUE, results='asis'}
MSGTime <- check_msg_time(data = aligned1, Msg = "VowelOnset", ReturnData = TRUE)
```

## Creating the Time series column

Once you have aligned the time sequence relative to the message, you need to create the `Time` column using the function `create_time_series`.
This function returns the time series column called `Time` which is required for subsequent processing, plotting, and modeling of the data.
In the example here, our message relates specifically to the time at which the vowel in the word was played (because we programmed Experiment Builder to output a message for that).
So, we do not need to specify an adjustment to the time series.
If, however, your critical stimulus did not occur exactly at the message, but rather, before or after, an adjustment (i.e., negative or positive value in milliseconds) can be applied to the time series, to shift the zero point.
This is done by specifying the `Adjust` parameter. 
If, on the other hand, this adjustment differed trial by trial, you can input a column name (present in your dataset) in `Adjust`, which will apply the recording event specific adjustment. 

A positive value (3 in the example below) provided to `Adjust` shifts the zero point to *after* the reference point (i.e., Message), effectively moving the zero point forward along the number line and causing the reference point to have a negative time value.

|  | Smp1 | Smp2 | Smp3 | Smp4 | Smp5 | Smp6 | Smp7 | Smp8 | Smp9 | Smp10 | Smp11 |
|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|
| Before | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 
|  |  |  |  |  | Old 0 |  |  | New 0 |  |  |  |
| After | -7 | -6 | -5 | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 |

A negative value (-3 in the example below) provided to `Adjust` shifts the zero point to *before* the reference point (i.e., Message), effectively moving the zero point backward along the number line and causing the reference point to have a postive time value.

|  | Smp1 | Smp2 | Smp3 | Smp4 | Smp5 | Smp6 | Smp7 | Smp8 | Smp9 | Smp10 | Smp11 |
|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|
| Before | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 
|  |  | New 0 |  |  | Old 0 |  |  |  |  |  |  |
| After | -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |


```{r, eval= TRUE, echo=TRUE, results='asis'}
aligned2 <- create_time_series(data = aligned1, Adjust = 0)
```

The function `check_time_series` can be used to verify that the time series was created.
The function outputs a list of the unique start values present in the data.
In this example, we do not expect all events to start with the same Time value, given that we performed our own alignment without a relative interest period defined in Data Viewer.
As with `check_msg_time`, the list of Events and Start Times can be returned as a data frame using the parameter `ReturnData`.

```{r, eval= TRUE, echo=TRUE, results='asis'}
check_time_series(data = aligned2)
```

Perhaps more meaningfully, we can check the message time again. 
We can see that our message is still the zero point in the Time series column.

```{r, eval= TRUE, echo=TRUE, results='asis'}
check_msg_time(data = aligned2, Msg = "VowelOnset")
```

## Proceed with preprocessing

At this point it is possible to proceed with preprocessing as usual. 
For details, please refer to the [Basic Preprocessing](VWPre_Basic_Preprocessing.html) vignette and continue by selecting a recording eye before binning the data.

