Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Imputing missing values using ARIMA model

I am trying to impute missing values in a time series with an ARIMA model in R. I tried this code but no success.

x <- AirPassengers
x[90:100] <- NA
fit <- auto.arima(x)
fitted(fit)[90:100]  ## this is giving me NAs
plot(x)
lines(fitted(fit), col="red")

The fitted model is not imputing the missing values. Any idea on how this is done?

like image 521
Filly Avatar asked Sep 25 '26 20:09

Filly


2 Answers

fitted gives in-sample one-step forecasts. The "right" way to do what you want is via a Kalman smoother. A rough approximation good enough for most purposes is obtained using the average of the forward and backward forecasts for the missing section. Like this:

x <- AirPassengers
x[90:100] <- NA
fit <- auto.arima(x)
fit1 <- forecast(Arima(AirPassengers[1:89],model=fit),h=10)
fit2 <- forecast(Arima(rev(AirPassengers[101:144]), model=fit), h=10)

plot(x)
lines(ts(0.5*c(fit1$mean+rev(fit2$mean)), 
  start=time(AirPassengers)[90],freq=12), col="red")

enter image description here

like image 67
Rob Hyndman Avatar answered Sep 28 '26 10:09

Rob Hyndman


As said by Rob, using a Kalman Smoother is usually the "better" solution.

This can for example be done via the imputeTS package (disclaimer: I maintain the package). (https://cran.r-project.org/web/packages/imputeTS/index.html)

library("imputeTS")
x <- AirPassengers
x[90:100] <- NA
x <- na.kalman(x, model = "auto.arima")

Internally the imputeTS package performs KalmanSmoothing on the State Space Representation of the ARIMA model obtained by auto.arima.

Even if the theoretical background is not easy to understand, it usually gives very good results :)

like image 25
Steffen Moritz Avatar answered Sep 28 '26 10:09

Steffen Moritz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!