Skip to contents

This is mainly a wrapper for the Seasonally Adjusted Missing Value using Linear Interpolation function, na.interp(), from the forecast R package. The ts_impute_vec() function includes arguments for applying seasonality to numeric vector (non-ts) via the period argument.

Usage

ts_impute_vec(x, period = 1, lambda = NULL)

Arguments

x

A numeric vector.

period

A seasonal period to use during the transformation. If period = 1, linear interpolation is performed. If period > 1, a robust STL decomposition is first performed and a linear interpolation is applied to the seasonally adjusted data.

lambda

A box cox transformation parameter. If set to "auto", performs automated lambda selection.

Value

A numeric vector with the missing values imputed.

Details

Imputation using Linear Interpolation

Three circumstances cause strictly linear interpolation:

  1. Period is 1: With period = 1, a seasonality cannot be interpreted and therefore linear is used.

  2. Number of Non-Missing Values is less than 2-Periods: Insufficient values exist to detect seasonality.

  3. Number of Total Values is less than 3-Periods: Insufficient values exist to detect seasonality.

Seasonal Imputation using Linear Interpolation

For seasonal series with period > 1, a robust Seasonal Trend Loess (STL) decomposition is first computed. Then a linear interpolation is applied to the seasonally adjusted data, and the seasonal component is added back.

Box Cox Transformation

In many circumstances, a Box Cox transformation can help. Especially if the series is multiplicative meaning the variance grows exponentially. A Box Cox transformation can be automated by setting lambda = "auto" or can be specified by setting lambda = numeric value.

See also

Examples

library(dplyr)


# --- VECTOR ----

values <- c(1,2,3, 4*2, 5,6,7, NA, 9,10,11, 12*2)
values
#>  [1]  1  2  3  8  5  6  7 NA  9 10 11 24

# Linear interpolation
ts_impute_vec(values, period = 1, lambda = NULL)
#>  [1]  1  2  3  8  5  6  7  8  9 10 11 24

# Seasonal Interpolation: set period = 4
ts_impute_vec(values, period = 4, lambda = NULL)
#>  [1]  1  2  3  8  5  6  7  8  9 10 11 24

# Seasonal Interpolation with Box Cox Transformation (internal)
ts_impute_vec(values, period = 4, lambda = "auto")
#>  [1]  1.000000  2.000000  3.000000  8.000000  5.000000  6.000000  7.000000
#>  [8]  7.960572  9.000000 10.000000 11.000000 24.000000