Uses either linear, spline or stineman interpolation to replace missing values.

na_interpolation(x, option = "linear", maxgap = Inf, ...)

Arguments

x

Numeric Vector (vector) or Time Series (ts) object in which missing values shall be replaced

option

Algorithm to be used. Accepts the following input:

  • "linear" - for linear interpolation using approx (default choice)

  • "spline" - for spline interpolation using spline

  • "stine" - for Stineman interpolation using stinterp

maxgap

Maximum number of successive NAs to still perform imputation on. Default setting is to replace all NAs without restrictions. With this option set, consecutive NAs runs, that are longer than 'maxgap' will be left NA. This option mostly makes sense if you want to treat long runs of NA afterwards separately.

...

Additional parameters to be passed through to approx or spline interpolation functions

Value

Vector (vector) or Time Series (ts) object (dependent on given input at parameter x)

Details

Missing values get replaced by values of a approx, spline or stinterp interpolation.

References

Johannesson, Tomas, et al. (2015). "Package stinepack".

See also

Author

Steffen Moritz

Examples

# Prerequisite: Create Time series with missing values x <- ts(c(2, 3, 4, 5, 6, NA, 7, 8)) # Example 1: Perform linear interpolation na_interpolation(x)
#> Time Series: #> Start = 1 #> End = 8 #> Frequency = 1 #> [1] 2.0 3.0 4.0 5.0 6.0 6.5 7.0 8.0
# Example 2: Perform spline interpolation na_interpolation(x, option = "spline")
#> Time Series: #> Start = 1 #> End = 8 #> Frequency = 1 #> [1] 2.00000 3.00000 4.00000 5.00000 6.00000 6.53923 7.00000 8.00000
# Example 3: Perform stine interpolation na_interpolation(x, option = "stine")
#> Time Series: #> Start = 1 #> End = 8 #> Frequency = 1 #> [1] 2.0 3.0 4.0 5.0 6.0 6.5 7.0 8.0
# Example 4: Same as example 1, just written with pipe operator x %>% na_interpolation()
#> Time Series: #> Start = 1 #> End = 8 #> Frequency = 1 #> [1] 2.0 3.0 4.0 5.0 6.0 6.5 7.0 8.0
# Example 5: Same as example 2, just written with pipe operator x %>% na_interpolation(option = "spline")
#> Time Series: #> Start = 1 #> End = 8 #> Frequency = 1 #> [1] 2.00000 3.00000 4.00000 5.00000 6.00000 6.53923 7.00000 8.00000