Learn R Programming

imputeTS (version 3.2)

na_kalman: Missing Value Imputation by Kalman Smoothing and State Space Models

Description

Uses Kalman Smoothing on structural time series models (or on the state space representation of an arima model) for imputation.

Usage

na_kalman(x, model = "StructTS", smooth = TRUE, nit = -1, maxgap = Inf, ...)

Value

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

Arguments

x

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

model

Model to be used. With this parameter the State Space Model (on which KalmanSmooth is performed) can be chosen. Accepts the following input:

  • "auto.arima" - For using the state space representation of arima model (using auto.arima) (default choice)

  • "StructTS" - For using a structural model fitted by maximum likelihood (using StructTS)

For both auto.arima and StructTS additional parameters for model building can be given with the ... parameter

Additionally it is also possible to use a user created state space model (See code Example 5). This state space model could for example be obtained from another R package for structural time series modeling. Furthermore providing the state space representation of a arima model from arima is also possible. But it is important to note, that user created state space models must meet the requirements specified under KalmanLike. This means the user supplied state space model has to be in form of a list with at least components T, Z, h , V, a, P, Pn. (more details under KalmanLike)

smooth

if TRUE - KalmanSmooth is used for estimation, if FALSE - KalmanRun is used. Since KalmanRun is often considered extrapolation KalmanSmooth is usually the better choice for imputation.

nit

Parameter from Kalman Filtering (see KalmanLike). Usually no need to change from default.

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 the functions that build the State Space Models (StructTS or auto.arima).

Author

Steffen Moritz

Details

The KalmanSmoother used in this function is KalmanSmooth. It operates either on a Basic Structural Model obtained by StructTS or the state space representation of a ARMA model obtained by auto.arima.

For an detailed explanation of Kalman Filtering and Space Space Models the following literature is a good starting point:

  • G. Welch, G. Bishop, An Introduction to the Kalman Filter. SIGGRAPH 2001 Course 8, 1995

  • Harvey, Andrew C. Forecasting, structural time series models and the Kalman filter. Cambridge university press, 1990

  • Grewal, Mohinder S. Kalman filtering. Springer Berlin Heidelberg, 2011

References

Hyndman RJ and Khandakar Y (2008). "Automatic time series forecasting: the forecast package for R". Journal of Statistical Software, 26(3).

See Also

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

Examples

Run this code
# Example 1: Perform imputation with KalmanSmoother and state space representation of arima model
na_kalman(tsAirgap)

# Example 2: Perform imputation with KalmanRun and state space representation of arima model
na_kalman(tsAirgap, smooth = FALSE)

# Example 3: Perform imputation with KalmanSmooth and StructTS model
na_kalman(tsAirgap, model = "StructTS", smooth = TRUE)

# Example 4: Perform imputation with KalmanSmooth and StructTS model with additional parameters
na_kalman(tsAirgap, model = "StructTS", smooth = TRUE, type = "trend")

# Example 5:  Perform imputation with KalmanSmooth and user created model
usermodel <- arima(tsAirgap, order = c(1, 0, 1))$model
na_kalman(tsAirgap, model = usermodel)

# Example 6: Same as example 1, just written with pipe operator
tsAirgap %>% na_kalman()

Run the code above in your browser using DataLab