sparse_to_na: Sparsity filter

Description Usage Arguments Examples

View source: R/sparse_to_na.R

Description

sparse_to_na removes sparse samples in the data.

Usage

1
sparse_to_na(pupil, time, gap_criterion = 40, cluster_criterion = 50)

Arguments

pupil

A numeric vector of pupil size measurements.

time

A vector containing the timestamps associated with the pupil size measurements.

gap_criterion

A numeric value specifying the gap duration. Clusters that follow a gap of this size or larger will be removed.

cluster_criterion

A numeric value specifying the cluster duration. Clusters smaller than this this value will be removed.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Load the "dplyr", "tidyr", and "ggplot2" packages:
library(dplyr)
library(tidyr)
library(ggplot2)

# Example 1: Artificial data
# Create some artificial data:
data <- tibble(
  time = 1:8,
  pupil_left = c(3.11, 3.13, NA, NA, 3.24, NA, NA, 3.12),
  pupil_right = c(2.92, 2.95, NA, NA, 3.06, NA, NA, 2.95)
)
data

# Remove the cluster in the left eye:
mutate(data,
  pupil_left = sparse_to_na(pupil_left, time, gap_criterion = 2,
    cluster_criterion = 1)
)

# Example 2: Realistic data
sparse

# Remove a sparse cluster of the left eye:
sparse <- mutate(sparse,
  pupil_left_new = sparse_to_na(pupil_left, timestamp, gap_criterion = 10,
    cluster_criterion = 5)
  )

# Restructure the data and plot the results to compare the pupil measurements
# before and after padding the gaps:
sparse %>%
  rename(pupil_left_old = pupil_left) %>%
  pivot_longer(
    cols = c(pupil_left_old, pupil_left_new),
    names_to = "status",
    names_pattern = "(old|new)",
    values_to = "pupil_size"
 ) %>%
 ggplot(aes(x = timestamp, y = pupil_size, color = status)) +
   geom_point()

WillemSleegers/eyepatch documentation built on Aug. 2, 2021, 8:39 a.m.