Skip to contents

Removes or imputes NA cases in time series lists. Imputation is done via interpolation against time via zoo::na.approx(), and if there are still leading or trailing NA cases after NA interpolation, then zoo::na.spline() is applied as well to fill these gaps. Interpolated values are forced to fall within the observed data range.

Usage

tsl_handle_NA(tsl = NULL, na_action = c("omit", "impute"), quiet = FALSE)

tsl_Inf_to_NA(tsl = NULL)

tsl_NaN_to_NA(tsl = NULL)

Arguments

tsl

(required, list) Time series list. Default: NULL

na_action

(required, character) NA handling action. Available options are:

  • "omit" (default): rows with NA cases are removed.

  • "impute" : NA cases are interpolated from neighbors as a function of time (see zoo::na.approx() and zoo::na.spline()).

quiet

(optional, logical) If TRUE, all messages are suppressed. Default: FALSE

Value

time series list

Examples

#tsl with NA cases
tsl <- tsl_simulate(
  na_fraction = 0.25
)

tsl_count_NA(tsl = tsl)
#> distantia::tsl_count_NA(): NA cases in 'tsl': 
#>   name NA_cases
#> 1    A      125
#> 2    B      125
#> Please impute, replace, or remove them with tsl_handle_NA().FALSE
#> $A
#> [1] 125
#> 
#> $B
#> [1] 125
#> 

if(interactive()){
  #issues warning
  tsl_plot(tsl = tsl)
}

#omit NA (default)
#--------------------------------------

#original row count
tsl_nrow(tsl = tsl)
#> $A
#> [1] 100
#> 
#> $B
#> [1] 100
#> 

#remove rows with NA
tsl_no_na <- tsl_handle_NA(
  tsl = tsl
)

#count rows again
#large data loss in this case!
tsl_nrow(tsl = tsl_no_na)
#> $A
#> [1] 29
#> 
#> $B
#> [1] 23
#> 

#count NA again
tsl_count_NA(tsl = tsl_no_na)
#> $A
#> [1] 0
#> 
#> $B
#> [1] 0
#> 

if(interactive()){
  tsl_plot(tsl = tsl_no_na)
}


#impute NA with zoo::na.approx
#--------------------------------------

#impute NA cases
tsl_no_na <- tsl_handle_NA(
  tsl = tsl,
  na_action = "impute"
)

#count rows again
#large data loss in this case!
tsl_nrow(tsl = tsl_no_na)
#> $A
#> [1] 100
#> 
#> $B
#> [1] 100
#> 

if(interactive()){
  tsl_plot(tsl = tsl_no_na)
}