imputeTS (version 3.0)

na_locf: Missing Value Imputation by Last Observation Carried Forward

Description

Replaces each missing value with the most recent present value prior to it (Last Observation Carried Forward- LOCF). Optionally this can also be done starting from the back of the series (Next Observation Carried Backward - NOCB).

Usage

na_locf(x, option = "locf", na_remaining = "rev", 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:

  • "locf" - for Last Observation Carried Forward

  • "nocb" - for Next Observation Carried Backward

na_remaining

Method to be used for remaining NAs.

  • "keep" - to return the series with NAs

  • "rm" - to remove remaining NAs

  • "mean" - to replace remaining NAs by overall mean

  • "rev" - to perform nocb / locf from the reverse direction

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.

Value

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

Details

Replaces each missing value with the most recent present value prior to it (Last Observation Carried Forward- LOCF). This can also be done from the reverse direction -starting from the back (Next Observation Carried Backward - NOCB). Both options have the issue, that NAs at the beginning (or for nocb at the end) of the time series cannot be imputed (since there is no last value to be carried forward present yet). In this case there are remaining NAs in the imputed time series. Since this only concerns very few values at the beginning of the series, na_remaining offers some quick solutions to get a series without NAs back.

See Also

na_interpolation, na_kalman, na_ma, na_mean, na_random, na_replace, na_seadec, na_seasplit

Examples

Run this code
# NOT RUN {
# Prerequisite: Create Time series with missing values
x <- ts(c(NA, 3, 4, 5, 6, NA, 7, 8))

# Example 1: Perform LOCF
na_locf(x)

# Example 2: Perform NOCF
na_locf(x, option = "nocb")

# Example 3: Perform LOCF and remove remaining NAs
na_locf(x, na_remaining = "rm")

# Example 4: Same as example 1, just written with pipe operator
x %>% na_locf()
# }

Run the code above in your browser using DataCamp Workspace