An open API service indexing awesome lists of open source software.

https://github.com/bnicenboim/eeguana

A package for manipulating EEG data in R.
https://github.com/bnicenboim/eeguana

dplyr eeg eeg-analysis eeg-data eeg-signals r tidyverse

Last synced: 11 months ago
JSON representation

A package for manipulating EEG data in R.

Awesome Lists containing this project

README

          

---
bibliography: papers.bib
output: github_document
---
[![R build status](https://github.com/bnicenboim/eeguana/workflows/R-CMD-check/badge.svg)](https://github.com/bnicenboim/eeguana/actions)
[![Codecov test coverage](https://codecov.io/gh/bnicenboim/eeguana/branch/master/graph/badge.svg)](https://codecov.io/gh/bnicenboim/eeguana?branch=master)
[![DOI](https://zenodo.org/badge/153299577.svg)](https://zenodo.org/badge/latestdoi/153299577) [![lifecycle](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
[![Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public.](https://www.repostatus.org/badges/latest/wip.svg)](https://www.repostatus.org/#wip)

```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# eeguana

## Overview

A package for flexible manipulation of EEG data. `eeguana` provides a `data.table` powered framework through `tidytable` for manipulating EEG data with *dplyr*-like functions (e.g., `eeg_mutate`, `eeg_filter`, `eeg_summarize`) extended to a new class `eeg_lst`, other EEG-specialized functions, and `ggplot` wrapper functions. The new class is inspired by tidyverse principles but it's not really "tidy" (due to space considerations), it's a list of (i) a wide *data table* (`signal_tbl`) that contains the signal amplitudes at every sample point of the EEG, (ii) an events *data table* with information about markers (or triggers), blinks and other exported information, and (iii) a long table with experimental information, such as participant number (`.recording`), conditions, etc.

*eeguana* can do only basic pre-processing for now, more complete packages exist for Matlab ([FieldTrip](http://www.fieldtriptoolbox.org/) and [EEGLAB](https://sccn.ucsd.edu/eeglab/index.php)) and python ([MNE](https://martinos.org/mne/stable/index.html)).

See [Reference](https://bnicenboim.github.io/eeguana/reference/index.html) for more information about the functions of *eeguana*.

**NOTE: Changes in dependencies that broke the package are now fixed!**

## Installation

There is still **no** released version of *eeguana*. The package is in the early stages of development, and it **will** be subject to a lot of changes. To install the latest version from github use:

``` r
devtools::install_github("bnicenboim/eeguana")
```

## Example

Here, I exemplify the use of *eeguana* with (pre-processed) EEG data from BrainVision 2.0. The data belong to a simple experiment where a participant was presented 100 faces and 100 assorted images in random order. The task of the experiment was to mentally count the number of faces.

First we download the data:

```{r downloading, results='hide', eval = any(!file.exists("faces.vhdr","faces.vmrk","faces.dat"))}
# Run the following or just download the files from brain_vision folder in https://osf.io/tbwvz/
library(httr)
GET("https://osf.io/q6b7x//?action=download",
write_disk("./faces.vhdr", overwrite = TRUE),
progress()
)
GET("https://osf.io/ft5ge//?action=download",
write_disk("./faces.vmrk", overwrite = TRUE),
progress()
)
GET("https://osf.io/85dgj//?action=download",
write_disk("./faces.dat", overwrite = TRUE),
progress()
)
```

BrainVision 2.0 exports three files: `faces.vhdr`, `faces.vmrk`, and
`faces.dat`. The file `faces.vhdr` contains the metadata and links to the other
two files, `faces.vmrk` contains the triggers and other events in the samples,
and `faces.dat` contains the signals at every sample for every channel recorded.

```{r libs, message = FALSE}
library(eeguana)
```

We first need to read the data:

```{r}
faces <- read_vhdr("faces.vhdr")
```

The function `read_vhdr()` creates a list with data frames for the signal, events,
segments information, and incorporates in its attributes generic EEG information.

```{r}
faces
```

Some intervals were marked as "bad" by BrainVision, and so we'll remove them
from the data. We'll also segment and baseline the data. In this experiment, the
trigger "s70" was used for faces and "s71" for no faces. We'll segment the data
using these two triggers.

```{r}
faces_segs <- faces |>
eeg_segment(.description %in% c("s70", "s71"),
.lim = c(-.2, .25)
) |>
eeg_events_to_NA(.type == "Bad Interval") |>
eeg_baseline()
```

We can also edit the segmentation information and add more descriptive labels. Once the `eeg_lst` is segmented, the segments table includes the relevant columns from the events table (but without the leading dots).

*eeguana* has wrappers for many `dplyr` commands for the EEG data. These commands always return an entire `eeg_lst` object so that they can be piped using `magrittr`'s pipe, `|>`.

```{r}
## To only see the segments table:
segments_tbl(faces_segs)
## We modify the entire object:
faces_segs_some <- faces_segs |>
eeg_mutate(
condition =
ifelse(description == "s70", "faces", "non-faces")
) |>
eeg_select(-type)

faces_segs_some
```

With some "regular" `ggplot` skills, we can create customized plots. `ggplot()` applied to an `eeg_lst` object will downsample the signals (when needed), and convert them to a long-format data frame that is feed into `ggplot`. This object can then be customized.

```{r plot, fig.dim = c(10,15), out.width = "100%", results = "hide"}
library(ggplot2)
faces_segs_some |>
eeg_select(O1, O2, P7, P8) |>
ggplot(aes(x = .time, y = .value)) +
geom_line(alpha = .1, aes(group = .id, color = condition)) +
stat_summary(
fun = "mean", geom = "line", alpha = 1, size = 1.5,
aes(color = condition)
) +
facet_wrap(~.key) +
geom_vline(xintercept = 0, linetype = "dashed") +
geom_vline(xintercept = .17, linetype = "dotted") +
theme(legend.position = "bottom")
```

Another possibility is to create a topographic plot of the two conditions, by
first making segments that include only the interval .1-.2 *s* after the onset
of the stimuli, creating a table with interpolated amplitudes and using the ggplot wrapper `plot_topo`.

```{r, echo = FALSE, results="hide", message = FALSE}
#fixes bug?
dev.off()
```

```{r topo, fig.dim = c(10,5), out.width = "100%", results = "hide"}
faces_segs_some |>
eeg_filter(between(as_time(.sample, .unit = "milliseconds"), 100, 200)) |>
eeg_group_by(condition) |>
eeg_summarize(across_ch(mean, na.rm = TRUE)) |>
plot_topo() +
annotate_head() +
geom_contour() +
annotate_electrodes(colour = "black") +
facet_grid(~condition)
```

## Articles and dissertations using `eeguana`

---
nocite: '@*'
---

## Other R packages for EEG/ERP data:

- [permuco4brain](https://jaromilfrossard.github.io/permuco4brain/index.html) provides functions to compute permutation test in brain imagery data. It is specially designed for M-EEG/ERP data. This a [vignette](https://jaromilfrossard.github.io/permuco4brain/articles/permuco4brain-with-eeguana.html) explains how to use it together with `eeguana`.
- [eegUtils](https://github.com/craddm/eegUtils) some helper utilities for plotting and processing EEG data in active development by Matt Craddock.
- [erpR](https://cran.r-project.org/package=erpR) analysis of event-related potentials (ERPs) by Giorgio Arcara, Anna Petrova. It hasn't been updated since 2014.
- [mne-r](https://mne.tools/mne-r/index.html) provides fast acccess to MNE-Python from within R.