---
title: "Using E4tools to process EDA data"
author: "Evan Kleiman"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Using E4tools to process E4 data}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r}
library(E4tools)
```

## Step 0: Structure your files
Your files should be structured in a way such that you have one main folder that contains within in, a subfolder for each participant. The name of each participant folder should be their ID number. In each "participant" subfolder should be all zip files containing their data, using the naming convention that E4 Connect uses (starttime_serialnumber.zip). 

## Step 1: Extract and filter raw EDA
This step is where you extract and filter EDA data. It will output raw data, filtered data (using user-specified high and low pass filters + a butterworth filter), and filtered + feature-scaled ([0,1]) data. It will also provide summary data at the participant and session level.

Inputs: (1) List of participant numbers and (2) location where ZIP folders are stored. 

Outputs: (1) one RDS file per participant with all data, (2) summary file that gives participant-level meta-data.

#### Code
```{r eval=FALSE, echo=TRUE} 
E4_EDA_Process.part1.ExtractRawEDA(participant_list=c(1001),
                                   ziplocation="~/extdata/E4_demo_data/",
                                   rdslocation.EDA="~/extdata/output/raw_EDA/",
                                   summarylocation="~/extdata/output/summaries/",
                                   EDA_low_cut=0.001,LowPctCutoff=.75,
                                   EDA_high_cut=25,HighPctCutoff=.75)

```

#### Console output
```{r eval=FALSE, echo=FALSE} 
E4_EDA_Process.part1.ExtractRawEDA(participant_list=c(1001),
                                   ziplocation=paste(system.file(package="E4tools"),"/extdata/E4_demo_data/",sep=""),
                                   rdslocation.EDA=paste(tempdir(),"/extdata/output/raw_EDA/",sep=""),
                                   summarylocation=paste(tempdir(),"/extdata/output/summaries/",sep=""),
                                   EDA_low_cut=0.001,LowPctCutoff=.75,
                                   EDA_high_cut=25,HighPctCutoff=.75)

```

#### Sample of RDS output file
```{r, echo=FALSE,eval=FALSE}
options(scipen=999)
Table0<-readRDS("EDA_presses_COMBINED.RDS")
Table0a<-head(Table0)
knitr::kable(Table0a, format = "html")
```


## Step 2: Extract button presses
This steps is where  you extract button presses and remove presses that are within a certain number of minutes before the end of a session or that are too close to another button press. If the participant has not pressed the button at all, it will give you a warning and continue with the other participants.

Inputs: (1) List of participant numbers, (2) Location where ZIP folders are stored. 

Output (one file per study): RDS file with list of IDs and button press times.

#### Code

```{r eval=FALSE, echo=TRUE}
E4_EDA_Process.part2.ExtractButtonPresses(participant_list=c(1001),
                                          ziplocation="~/extdata/E4_demo_data/",
                                          rdslocation.buttonpress="~/extdata/output/presses/",
                                          summarylocation="~/extdata/output/summaries/",
                                          cutoff.ends=2,
                                          cutoff.overlap=20)
```

#### Console output
```{r eval=FALSE, echo=FALSE}
E4_EDA_Process.part2.ExtractButtonPresses(participant_list=c(1001),
                                          ziplocation=paste(system.file(package="E4tools"),"/extdata/E4_demo_data/",sep=""),
                                          rdslocation.buttonpress=paste(tempdir(),"/extdata/output/presses/",sep=""),
                                          summarylocation=paste(tempdir(),"/extdata/output/summaries/",sep=""),
                                          cutoff.ends=2,
                                          cutoff.overlap=20)
```

#### Sample of RDS output file
```{r, echo=FALSE,eval=FALSE}
Table1<-readRDS("1002_EDA.RDS")
Table1a<-head(Table1)
knitr::kable(Table1a, format = "html")
```


## Step 3: Match button presses to EDA
This function allows you to extract the data that are within X minutes before and/or after a button press. If there are no button pressess for a participant, it will issue a warning and continue with the next participant.
Inputs: (1) List of participant numbers, (2) location of individual EDA files from step 1, (3) location of button presses from step 2.
Outputs: (1) RDS file with EDA data before and/or after button presses (and control data), for each participant and combined.

#### Code
```{r, eval=FALSE, echo=TRUE}
E4_EDA_Process.part3.MatchPressesToEDA(participant_list=c(1001),
                                       rdslocation.buttonpress="~/extdata/output/presses/",
                                        rdslocation.MatchedEDA="~/extdata/output/matched_EDA/",
                                      rdslocation.EDA="~/extdata/output/raw_EDA/",
                                       min.before=20,min.after=20,control=T)
```
#### Console output
```{r, eval=FALSE, echo=FALSE}
E4_EDA_Process.part3.MatchPressesToEDA(participant_list=c(1001),
                                       rdslocation.buttonpress=paste(tempdir(),"/extdata/output/presses/",sep=""),
                                        rdslocation.MatchedEDA=paste(tempdir(),"/extdata/output/matched_EDA/",sep=""),
                                      rdslocation.EDA=paste(tempdir(),"/extdata/output/raw_EDA/",sep=""),
                                       min.before=20,min.after=20,control=T)
```


#### Sample of RDS output file
```{r, echo=FALSE,eval=FALSE}
Table2<-readRDS("EDA_presses_COMBINED.RDS")
Table2a<-head(Table2)
knitr::kable(Table2a, format = "html")
```


## Step 4: Put matched EDA in to bins
This is the final step where everything gets put into one file. 
```{r,eval=FALSE, echo=TRUE}
E4_EDA_Process.part4.BinMatchedEDA(participant_list=c(1001),
                                    rdslocation.MatchedEDA="~/extdata/output/matched_EDA/",
                                    rdslocation.BinnedMatchedEDA="~/extdata/output/binned_matched_EDA/",
                                   min.after = 20,min.before = 20)
```
#### Console output
```{r,eval=FALSE, echo=FALSE}
E4_EDA_Process.part4.BinMatchedEDA(participant_list=c(1001),
                                      rdslocation.MatchedEDA=paste(tempdir(),"/extdata/output/matched_EDA/",sep=""),
                                    rdslocation.BinnedMatchedEDA=paste(tempdir(),"/extdata/output/binned_matched_EDA/",sep=""),
                                   min.after = 20,min.before = 20)
```
#### Sample of RDS output file
Note: The values for "MinBeforeAfter" for "before" values will be negative, to use with things like growth curve modeling. If you positive integers, just multiple this column by -1.
```{r, echo=FALSE,eval=FALSE}
load(paste(system.file(package="E4tools"),"/data/EDA_Vignette4.rda",sep=""))
EDA_Vignette4a<-head(EDA_Vignette4)
knitr::kable(EDA_Vignette4a, format = "html")
```
