interpolate_gaps: Interpolate missing data between gaps

Description Usage Arguments Details Examples

View source: R/interpolate_gaps.R

Description

interpolate_gaps returns a vector with the (originally) missing data points interpolated according to the specified method.

Usage

1
interpolate_gaps(pupil, type = "linear", rule = 2, method = "fmm")

Arguments

pupil

A numeric vector of pupil size measurements.

type

The type of interpolation to be used. Possible values are 'linear' (default) or 'spline'. Note that the spline interpolation does not yet function as intended.

Details

Returns a vector equal in length to pupil, with all missing data points interpolated according to the specified method.

Note that the default value for the rule argument in approx() is 2 rather than 1.

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
42
43
44
45
46
47
48
49
# 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, NA, NA, NA, 3.12),
  pupil_right = c(2.92, 2.95, NA, NA, NA, NA, NA, 2.95)
)
data

# Linearly interpolate values of the left eye:
mutate(data,
  pupil_left = interpolate_gaps(pupil_left, type = "linear")
)

# Example 2: Realistic data
blink

# First remove outliers:
blink <- mutate(blink,
  pupil_left = dilation_speed_outliers_to_na(pupil_left, timestamp,
    constant = 10),
  pupil_right = dilation_speed_outliers_to_na(pupil_right, timestamp,
    constant = 10)
)

# Then linearly interpolate gaps in pupil measurements of the left eye and
# perform a spline interpolation on the right eye:
blink <- mutate(blink,
    pupil_left = interpolate_gaps(pupil_left, type = "linear"),
    pupil_right = interpolate_gaps(pupil_right, type = "spline")
  )

# Restructure the data and plot the results to compare the pupil measurements
# before the interpolation and after the interpolation:
blink %>%
  pivot_longer(
    cols = starts_with("pupil"),
    names_to = "pupil",
    names_pattern = "(left|right)",
    values_to = "pupil_size"
 ) %>%
 ggplot(aes(x = timestamp, y = pupil_size)) +
   geom_point() +
   facet_wrap(~ pupil)

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