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.

This function supports a parallelization setup via future::plan(), and progress bars provided by the package progressr.

Usage

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

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:

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

  • "omit": rows with NA cases are removed.

Value

time series list

Examples


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

tsl_count_NA(tsl = tsl)
#> $A
#> [1] 101
#> 
#> $B
#> [1] 105
#> 

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

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

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

#remove rows with NA
tsl_no_na <- tsl_handle_NA(
  tsl = tsl,
  na_action = "omit"
)

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

#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] 81
#> 
#> $B
#> [1] 84
#> 

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