na_approx | R Documentation |
NA
by interpolationactverse
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)
x |
A |
index |
An R object with the same length of |
fill_na_tips |
(optional) a |
week_start |
(optional) an integer number indicating the day on which
the week starts ( |
intp |
(optional) a |
print |
(optional) a |
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
argumentSome 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
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.
na_approx()
: Linear interpolationAs 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 meanThis method replaces NA
values with the overall mean of x
.
Visual example:
na_overall_median()
: Overall medianThis method replaces NA
values with the overall median of x
.
Visual example:
na_overall_mode()
: Overall modeThis 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 interpolationThis 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 meanThis 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 0
sThis method replaces NA
values with 0
s.
Visual example:
Tonon, A. C. et al. (2022). Handling missing data in rest-activity time series measured by actimetry. Chronobiology International, 39(7). \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1080/07420528.2022.2051714")}.
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 # 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
#> [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
#> [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 # 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 # 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
#> [14] 5.333333 # Expected
mean(x, na.rm = TRUE)
#> [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 # Expected
stats::median(x, na.rm = TRUE)
#> [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 # 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 # 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
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 # 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
#> [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 # Expected
na_plot(x, index, na_zero(x))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.