[Experimental]

actverse provides several functions to interpolate your data in an easy manner. These functions can be found with the prefix na_. See the Methods section to learn more about each one.

na_plot() creates a plot showing the shape of the interpolation. It helps to visualize and find the best interpolation technique for your data.

na_approx(x, index, fill_na_tips = TRUE)

na_locf(x, fill_na_tips = TRUE)

na_overall_mean(x)

na_overall_median(x)

na_overall_mode(x)

na_spline(x, index)

na_weekly_mean(x, index, fill_na_tips = TRUE, week_start = 1)

na_zero(x)

na_plot(x, index, intp = NULL, print = TRUE)

Arguments

x

A numeric object.

index

An R object with the same length of x representing the index of a time series.

fill_na_tips

(optional) a logical value indicating if the function must fill remaining NA values with the closest non-missing data point. Learn more about it in the Details section (default: TRUE).

week_start

(optional) an integer number indicating the day on which the week starts (1 for Monday and 7 for Sunday) (default: 1).

intp

(optional) a numeric object with the same length of x and with the output of the NA interpolation of x (default: NULL).

print

(optional) a logical value indicating if the function must print the plot (default: TRUE).

Value

  • For na_*: a numeric object with the same length of x.

  • For na_plot(): a ggplot object with a point and line chart showing the original data versus the interpolated data.

Details

Interpolation in actigraphy

There are few articles that deals with interpolation in actigraphy. Tonon et al. (2022) recommends not using interpolation (i.e., maintain NA values) whenever is possible. The same authors also recommends using the weekly mean method of interpolation when the parameters cannot be computed in the presence of NA values.

fill_na_tips argument

Some interpolation methods can result in outputs with remaining NA values. That's the case, for example, with the linear interpolation method (na_approx()).

  • Example:

x <- c(NA, 1, 5, 10, NA, 5, 10, 1, NA, 10, 1, 5, NA, NA)
index <- seq(as.Date("2020-01-01"), as.Date("2020-01-14"), by = "day")

na_approx(x, index, fill_na_tips = FALSE)
#>  [1]   NA  1.0  5.0 10.0  7.5  5.0 10.0  1.0  5.5 10.0  1.0  5.0   NA   NA


By using fill_na_gaps == TRUE (default), the function will fill those gaps with the closest non-missing data point.

  • Example:

na_approx(x, index, fill_na_tips = TRUE)
#>  [1]  1.0  1.0  5.0 10.0  7.5  5.0 10.0  1.0  5.5 10.0  1.0  5.0  5.0  5.0

Methods

na_approx(): Linear interpolation

As the name suggests, this method creates a "bridge" between the gaps found in x. Learn more about it in zoo::na.approx() and stats::approx().

  • Visual example:

na_locf(): Last observation carried forward


This method replaces NA values with the preceding observation of the NA block.

  • Visual example:

na_overall_mean(): Overall mean

This method replaces NA values with the overall mean of x.

  • Visual example:

na_overall_median(): Overall median

This method replaces NA values with the overall median of x.

  • Visual example:

na_overall_mode(): Overall mode

This method replaces NA values with the most frequent value (mode) of x.

If no mode can be found, the function will return x without any interpolation. na_overall_mode() will show a warning message to inform the user if that happen.

  • Visual example:

na_spline(): Cubic spline interpolation

This method uses low-degree polynomials in each of the intervals, and chooses the polynomial pieces such that they fit smoothly together. It can produce extreme values when dealing with large gaps.

Learn more about the spline method in the spline interpolation Wikipedia page. See also: stats::spline() and zoo::na.spline().

  • Visual example:

na_weekly_mean(): Weekly mean

This method replaces NA values with the weekly mean of x. For datasets with only one week in the index the result will be the same as na_overall_mean().

  • Visual example:

na_zero(): Replace NA with 0s

This method replaces NA values with 0s.

  • Visual example:

References

Tonon, A. C. et al. (2022). Handling missing data in rest-activity time series measured by actimetry. Chronobiology International, 39(7). doi:10.1080/07420528.2022.2051714 .

Examples

x <- c(NA, 1, 5, 10, NA, 5, 10, 1, NA, 10, 1, 5, NA, NA)
index <- seq(as.Date("2020-01-01"), as.Date("2020-01-14"), by = "day")

x
#>  [1] NA  1  5 10 NA  5 10  1 NA 10  1  5 NA NA
#> [1] NA  1  5 10 NA  5 10  1 NA 10  1  5 NA NA # Expected
na_plot(x, index)


## 'na_approx()': Linear interpolation

na_approx(x, index, fill_na_tips = FALSE)
#>  [1]   NA  1.0  5.0 10.0  7.5  5.0 10.0  1.0  5.5 10.0  1.0  5.0   NA   NA
#> [1]   NA  1.0  5.0 10.0  7.5  5.0 10.0  1.0  5.5 10.0
#> [11] 1.0  5.0  NA   NA # Expected
na_plot(x, index, na_approx(x, index, fill_na_tips = FALSE))


na_approx(x, index, fill_na_tips = TRUE)
#>  [1]  1.0  1.0  5.0 10.0  7.5  5.0 10.0  1.0  5.5 10.0  1.0  5.0  5.0  5.0
#> [1]   1.0  1.0  5.0 10.0  7.5  5.0 10.0  1.0  5.5 10.0
#> [11] 1.0  5.0  5.0  5.0 # Expected
na_plot(x, index, na_approx(x, index, fill_na_tips = TRUE))


## 'na_locf()': Last observation carried forward

na_locf(x, fill_na_tips = FALSE)
#>  [1] NA  1  5 10 10  5 10  1  1 10  1  5  5  5
#> [1] NA  1  5 10 10  5 10  1  1 10  1  5  5  5 # Expected
na_plot(x, index, na_locf(x, fill_na_tips = FALSE))


na_locf(x, fill_na_tips = TRUE)
#>  [1]  1  1  5 10 10  5 10  1  1 10  1  5  5  5
#> [1]  1  1  5 10 10  5 10  1  1 10  1  5  5  5 # Expected
na_plot(x, index, na_locf(x, fill_na_tips = TRUE))


## 'na_overall_mean()': Overall mean

na_overall_mean(x)
#>  [1]  5.333333  1.000000  5.000000 10.000000  5.333333  5.000000 10.000000
#>  [8]  1.000000  5.333333 10.000000  1.000000  5.000000  5.333333  5.333333
#> [1]  5.333333  1.000000  5.000000 10.000000  5.333333  5.000000 10.000000
#> [8] 1.000000 5.333333 10.000000  1.000000  5.000000  5.333333
#> [14] 5.333333 # Expected
mean(x, na.rm = TRUE)
#> [1] 5.333333
#> [1] 5.333333 # Expected
na_plot(x, index, na_overall_mean(x))


## 'na_overall_median()': Overall median

na_overall_median(x)
#>  [1]  5  1  5 10  5  5 10  1  5 10  1  5  5  5
#> [1]  5  1  5 10  5  5 10  1  5 10  1  5  5  5 # Expected
stats::median(x, na.rm = TRUE)
#> [1] 5
#> [1] 5 # Expected
na_plot(x, index, na_overall_median(x))


## 'na_overall_mode()': Overall mode

na_overall_mode(x)
#> ! No mode was found. x was not interpolated.
#>  [1] NA  1  5 10 NA  5 10  1 NA 10  1  5 NA NA
#> ! No mode was found. x was not interpolated.
#> [1] NA  1  5 10 NA  5 10  1 NA 10  1  5 NA NA # Expected

x2 <- append(x, 1)
index2 <- append(index, as.Date("2020-01-15"))

na_overall_mode(x2)
#>  [1]  1  1  5 10  1  5 10  1  1 10  1  5  1  1  1
#> [1]  1  1  5 10  1  5 10  1  1 10  1  5  1  1  1 # Expected
na_plot(x2, index2, na_overall_mode(x2))


## 'na_spline()': Cubic spline interpolation

na_spline(x, index)
#>  [1]   4.567728   1.000000   5.000000  10.000000   6.589146   5.000000
#>  [7]  10.000000   1.000000   5.037198  10.000000   1.000000   5.000000
#> [13]  42.905390 131.216171
#> [1]  4.567728   1.000000   5.000000  10.000000   6.589146   5.000000
#> [7]  10.000000  1.000000   5.037198  10.000000   1.000000   5.000000
#> [13] 42.905390 131.216171
na_plot(x, index, na_spline(x, index))


## 'na_weekly_mean()': Weekly mean

na_weekly_mean(x, index, fill_na_tips = FALSE)
#>  [1]  5.333333  1.000000  5.000000 10.000000  5.333333  5.000000 10.000000
#>  [8]  1.000000  5.333333 10.000000  1.000000  5.000000        NA        NA
#> [1]  5.333333  1.000000  5.000000 10.000000  5.333333  5.000000 10.000000
#> [8] 1.000000 5.333333 10.000000  1.000000  5.000000  NA  NA # Expected
na_plot(x, index, na_weekly_mean(x, index, fill_na_tips = FALSE))


na_weekly_mean(x, index, fill_na_tips = TRUE)
#>  [1]  5.333333  1.000000  5.000000 10.000000  5.333333  5.000000 10.000000
#>  [8]  1.000000  5.333333 10.000000  1.000000  5.000000  5.000000  5.000000
#> [1]  5.333333  1.000000  5.000000 10.000000  5.333333  5.000000 10.000000
#> [8] 1.000000 5.333333 10.000000  1.000000  5.000000  5.000000
#> [14] 5.000000 # Expected
na_plot(x, index, na_weekly_mean(x, index, fill_na_tips = TRUE))


## 'na_zero()': Replace 'NA' with '0's

na_zero(x)
#>  [1]  0  1  5 10  0  5 10  1  0 10  1  5  0  0
#> [1]  0  1  5 10  0  5 10  1  0 10  1  5  0  0 # Expected
na_plot(x, index, na_zero(x))