> For the complete documentation index, see [llms.txt](https://help.imotions.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.imotions.com/lab/analysis/facial-expression-analysis/r-notebook-fea-facial-expression-analysis-thresholding-and-aggregation.md).

# R Notebook (FEA): Facial expression analysis thresholding and aggregation

Run the FEA thresholding and aggregation R notebooks to binarize AFFDEX or Realeyes signals, find expression onsets and count respondents showing each one.

*Learn how to calculate whether a facial expression was present or absent with the FEA thresholding and aggregation R Notebooks developed for the supported facial coding algorithms from* [*Affectiva AFFDEX*](https://help.imotions.com/lab/analysis/facial-expression-analysis/affdex/affectiva-affdex-signals-overview.md) *and* [*Realeyes*](https://help.imotions.com/lab/sensor-setup/facial-expression-analysis/enabling-realeyes-in-imotions.md)*.*

## Background

The raw signal from the facial coding algorithm is a confidence score indicating the probability that a trained human coder would rate that action unit/emotion as present at that time.

Through data analysis with the R Notebook, iMotions can filter out with a lower score of the probability of the presence of a facial expression. By keeping only those periods of time where the probability is high, one can classify whether a facial expression was present (or absent).

In iMotions, this is done by applying a threshold used to binarize the raw signal.

Through this thresholding procedure, iMotions can indicate when in time a facial expression started (onset event) and ended (offset event).

iMotions can also classify whether a facial expression was present for a short or long period of time (compared against a user defined time threshold).

By aggregating the thresholded signals from many respondents, iMotions can calculate how many respondents showed a given facial expression at any point in time.

For data analysis, iMotions can calculate summary metrics allowing the user to export aggregated emotion metrics which is a way to reduce the massive amount of information from the raw data export (Sensor Data Export) into a summary table that can be used to compare emotion scores between stimuli, groups etc.

## How to run the processing

*Please note that we recommend* [*post processing all respondent videos*](https://help.imotions.com/lab/import-and-post-processing/facial-expression-analysis-importing-and-post-processing-videos.md) *before starting an analysis of the facial expression data*

1\. Create an Analysis by right-clicking on the study and selecting "add analysis" or by clicking on the oval button

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-09bc0a59a3a32624b0432c3a0827bac1d8e91141%2Fd65864ea68_image-1710239466143.webp?alt=media)

2\. Under Signal processing, click the "+" button to add a new Active Flow

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-82586c66228bdedb0dd549df8bf9b967e88c6803%2F35ea45e905_image-1710239523013.webp?alt=media)

3\. Under "Device", choose the facial coding algorithm you used

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-ff7158ae5ed904807b5246051e1cccdaa99edea6%2Facea75e281_image-1710239596714.webp?alt=media)

4\. Mandatory: Under "Algorithm", choose "FEA thresholding"

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-d89db15d610fd8ef1f491aa73330ffdbe197c061%2F7c4cbefa78_image-1710239648429.webp?alt=media)

5\. Decide on the threshold values you want to apply. The output will reflect the number of data points and percent of time above the threshold value for each category below.

You can either batch apply a threshold value for the emotion channels and facial expressions, or choose to set individual thresholds for each channel individually.

It is recommended that you explore and normalize your own value dataset but as a general rule of thumb, 50% confidence represents a moderately strong display of facial response. 75% or higher means there was high confidence in classifying the facial expression and 25% or lower means that only 1 in 4 manual coders has classified the facial expression as present.

You can also choose if you wish to classify every present facial expression into short or long.

### Default Threshold parameters for Affectiva

* Emotion: 50
* Valence: + 50 (Positive Valence) and - 50 (Negative Valence)
* Action Units: 50
* Behavioral signal thresholds (recommended by Affectiva)
  * Right (deg): Yaw greater than +10
  * Left (deg): Yaw less than -10
  * Up (deg): Pitch greater than 0
  * Down (deg): Pitch less than -20
  * Right tilt (deg): Roll less than -10
  * Left tilt (deg): Roll greater than +10
  * Forward (px): Interocular distance greater than 100 px
  * Backward (px): Interocular distance less than 100 px

6\. Optional: Under Algorithm, choose "FEA thresholding Aggregation"

This step is only required if you wish to view graphs for static stimuli or videos in the [Aggregate Replay](https://help.imotions.com/lab/analysis/signal-processing-r-notebooks/my-library/aggregate-replay.md) UI in iMotions. If you only wish to process data and export statistics, this step is not required.

*The parameters give you the ability to change the bin length and bin overlap that the aggregation is relied on. You can also choose to visualize the aggregated curves in numbers of respondents or in percentage.*

7\. Complete the analysis creation.

<figure><img src="https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-a4bdeb4447ad50133f9353023eb25fa64a8322af%2F1a682f055d_image-CXJ7J6MN.webp?alt=media" alt=""><figcaption><p>Signal Processing Flows with ‘FEA Thresholding’ (left) and ‘FEA Thresholding Aggregation’ (right)</p></figcaption></figure>

## Method

Facial expressions are classified as being probably present or absent based on a binarization of the raw data collected in the experiment. A threshold is applied, indicating when the confidence score of a given facial expression (i.e. the raw data) exceeds or surpasses the chosen threshold, hence classifying the facial expression as present or absent.

If raw data is missing, the classifier handles it the following way:

* If data right before and after the gap is classified as absent, also the time with data loss is classified as absent
* If data right before and after the gap is classified as present, also the time with data loss is classified as present
* If data is classified as present or absent before the gap, but as the opposite after the data gap, the time with data loss is classified as absent

<figure><img src="https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-0d8eecda2e619b13a8be15db9e7ec4e1709f88da%2F85075788dd_image-1710238995911.webp?alt=media" alt=""><figcaption><p>Illustration of how applying a threshold (here: 50) classifies a facial expression (here: brow furrow) into present (highlighted) and absent</p></figcaption></figure>

## Results

Once the processing is done, you have access to the results in the individual and (if selected) aggregate replay.

![replay\_enter.png](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-7f8860e646344c1b1a320b652a8910214ac6e00a%2F47c31bfa5d_replay_enter.webp?alt=media)

### Results in the Individual Replay

<figure><img src="https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-f946edf789d764abb677c2c91c485b5aedd7b069%2F0dcfc506ac_image-1710240502470.webp?alt=media" alt=""><figcaption><p>Data in the Individual Replay</p></figcaption></figure>

In addition to the raw data graphs for facial expression analysis, an extra set of graphs can be selected from the **Signals**: Under "FEA thresholding", you find the binarized signals indicating when a facial expression was classified as present (1) or absent (0).

FEA thresholding Metrics are given for every facial expression, indicating the percentage (%) of time out of the entire stimulus duration for which the facial expression is present as well as the amount of segments with the facial expression present (i.e. how often it was expressed on the stimulus).

<figure><img src="https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-16a1f82d1527d772aee2d91ebb7e7ad843019404%2F8afda73248_image-1710240844973.webp?alt=media" alt=""><figcaption><p>Events in the Individual Replay</p></figcaption></figure>

In the **Markers** menu, a list of all onset and offset events for all facial expressions can be retrieved.

Please note that these events can be used to place [automated Annotations](https://help.imotions.com/lab/analysis/signal-processing-r-notebooks/my-library/annotations.md).

### Results in the Aggregated Replay

<figure><img src="https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-aff42ec3c21aed6f359cd9f5fb992139c3eaddff%2F33f6c6ae7a_image-1710246634089.webp?alt=media" alt=""><figcaption><p>Graph in the Aggregated Replay indicating the percentage of respondents showing Joy at the same time</p></figcaption></figure>

If you chose to run the FEA thresholding Aggregation, you can choose to see graphs in the Aggregate replay indicating how many respondents showed a selected facial expression within a time window.

## Export options

In order to run statistical data analysis, you can export the results. iMotions offers you different export options.

You can find the Export button in the bottom menu when in the analysis level.

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-12a4b8643e10d7b7b85609981421d85b6a7f9324%2Fe0deefd42a_image-1710241774875.webp?alt=media)

### FEA Metrics Export

To export summary metrics, go to Export > R Processing > FEA Metrics Export

<figure><img src="https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-99e70d626fe1c117f2462bdcdd02899b3418f677%2F5aa544cc80_image-1710241898681.webp?alt=media" alt=""><figcaption><p>Access the FEA Metrics exports through Export > R Processing</p></figcaption></figure>

You can choose if you wish to export metrics for all available facial expressions, or only for certain facial expressions.

You can also choose to include Annotation markers in the facial expressions table.

**Expression metrics** provide a summary of how often and how long a facial expression was present for each respondent and each time interval (Stimulus, Annotation, Scene).

<figure><img src="https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-6212a939e98ea62d71a6f8f363ef849361285c76%2F91f305b0ef_image-1711021001078.png?alt=media" alt=""><figcaption><p>The facial expressions metrics export</p></figcaption></figure>

**Expression table** lists all instances of a facial expression being present along with the onset and offset time of this segment, its duration, information on whether the expression was classified as short or long, as well as a running number counting the instances.

<figure><img src="https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-ae2e4ffc2cd5f9e17c943bb18d68b568de610db0%2F05cd1763bb_FEA-Table.png?alt=media" alt=""><figcaption><p>The facial expression table export</p></figcaption></figure>

### Sensor Data

Under *Built in processing > Sensor Data*, you can choose to export time series data.

<figure><img src="https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-6c71e627f99df59e27c2f1c63198d34e4e60eb58%2F40b545074c_image-1710246325207.webp?alt=media" alt=""><figcaption><p>Access the Sensor Data export through Export > Built in Processing > Sensor Data > Export</p></figcaption></figure>

Here, you can choose to export the binarized signals.

<figure><img src="https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-4735aa5018b59960739d425d52a9efb87f423cd8%2Ff336513de5_image-1710246280834.webp?alt=media" alt=""><figcaption><p>UI to include the binarized facial expression signals in the Sensor Data export</p></figcaption></figure>

### Next steps

If you open the exports in Excel, Pivot tables are a helpful tool to compare the results from different conditions


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.imotions.com/lab/analysis/facial-expression-analysis/r-notebook-fea-facial-expression-analysis-thresholding-and-aggregation.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
