# Using information on both sides of a 'gap' in time series data for imputation

As with my previous question, I'm looking at ways to impute missing data in a hierarchical time series data.

With al my other procedures, including the experimentation of imputation packages (Amelia, HoltWinters from Forecast and MICE imputation) I've only been able to use the time series data prior to the missing gap.

     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2001 220 194 238 190 217 244 242 225 242 259 267 244
2002 212 246 250 236 261 286 265 269 226 267 234 246
2003 202 199 297 272 236 266 235 226 260 183 226 265
2004 211 215 219 213 240 236 273 266 262 244 241 235
2005 212 198 233 251 259 282 305 267 241 264 222 269
2006 182 220 250 287 279 281 286 332 300 272 221 233
2007  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
2008 193 215 235 242 246 315 326 280 279 239 236 258
2009 246 189 257 241 268 223 260 288 234 260 216 195


I'm trying to do simple imputation procedure that uses forecasting and backcasting estimates from the time series model. Forecasting using prior data to predict the future and backcasting using the later data to “predict” the past.

I would then like to combine the forecast and backcast value to use as imputation. After which I will look at the fit etc.

For example, I'm able to determine what SARIMA model exist for the first period 2001-end2006. But not the full period (because my basic functions I know from R does not support the NA values.)

This is only for the period 2001-end2006:

ARIMA(2,0,2)(1,0,1)[12] with non-zero mean

Call: auto.arima(x = ts.datt)

Coefficients:
ar1      ar2      ma1     ma2    sar1     sma1  intercept
1.3610  -0.8258  -1.2407  0.9191  0.8982  -0.7560   244.8374
s.e.  0.0884   0.0960   0.0878  0.1127  0.2190   0.3335     6.1894

sigma^2 estimated as 605.9:  log likelihood = -335.01
AIC = 686.02   AICc = 688.3   BIC = 704.23


Should I just model the first period, predict by forecast; model then the last period separately and then backcast? How will I do this backcasting (ie. 'predicting' the past)?

EDIT: What I'm asking: 1) How do I use the data from years 2008 & 2009 to BACKCAST? I already know how to use 2001-2006 to forecast.

2) How do I determine the SARIMA model for the whole period? (2001-2009) ie.

• @joran Nope. I'm purely looking for any code/package that will allow me to do backcasting (which is a made up word for 'predicting/forecasting the past'). – OSlOlSO Aug 6 '11 at 16:27

Try using na.StructTS in the zoo package. It has methods for zoo and ts series. e.g. using the built in USAccDeaths insert some NAs and then interpolate them:

library(zoo)
window(USAccDeaths, 1975, c(1975, 12)) <- NA
na.StructTS(USAccDeaths)


See ?na.StructTS for more.

• For some reason I do not have the na.StructTS in my zoo package. I tried ?na.StructTS and ??na.StructTS but both say no documentation found. When I Google it - I see that there should be a na.StructTS function (as well as other na.XX functions.) I only have na.trim na.aggregate na.approx and na.locf - any idea where na.StructTS went? – OSlOlSO Aug 6 '11 at 10:53
• Make sure you have the latest version of zoo (currently version 1.7-2). – G. Grothendieck Aug 6 '11 at 12:37
• @Thanks for highlighting that. For some reason when I go to package installer - R only recognises 1.6-4 as the latest zoo package. Will check it now. – OSlOlSO Aug 6 '11 at 14:38
• Make sure you have the latest version of R. Older versions of R won't necessarily pull in the latest version of zoo. – G. Grothendieck Aug 6 '11 at 15:26
• After I couldn't manually install 1.7-2, I updated R. Funny enough - updated R removes all previous packages I installed through the manager. I looked at na.StructTS - which is an imputation method, but not the 'backcasting' I'm looking for. Thanks though! – OSlOlSO Aug 6 '11 at 16:29

na.interp from the forecast package performs well (similar to na.StructTS as @g_grothendieck recommends, bit faster though) in this analysis of time series interpolation methods.

My first thought would be to put your 2008-2009 data into a vector, reverse the vector and fit/forecast on it using the same methods you did with your 2001-2006 data.

Not sure if you need stationarity or other requirements to hold for this to work, but I don't think so.

Interesting question, I am also thinking of adding a forecast/backcast combination missing value estimation function to the imputeTS R package.

What in this case would have to be done is, 1. Take Data from 2001-2006 and make a forecast for 2007 e.g. with forecast / auto.arima

1. Take 2008-2009 Data reverse it and make a forecast for 2007 (this is then called backcast, because it is done in the reverse direction)

2. The two results (from backcast and forecast) can be combined like wanted. The most simple solution would be the mean of forecast and backcast value. But you could also think of weighting them according to the amount of data that was used for model building.

For your example this could look like:

library(forecast)

#Create data for forecast and backcast
fcData <- x[1:72]
bcData <- c[84:108]

#reverse backcast data
rev(bcData)

#Do Forecast
resultForecasts <- forecast(auto.arima(fcData),h=12)$mean #Do Backcast and reverse the results resultBackcasts(rev(forecast(auto.arima(bcData),h=12)$mean)

#And now you would have to combine back and forecast


More problems arise, if there are multiple NAs all over the time series. Then you can not just split into before and after a certain NA gap. Because the parts before will also contain NAs and thus making a forecast is not possible. You would have to go iterative over the dataset in this case. Always making the fore/backcast for next NA.

But the other posters are also right, there are methods that might be better suited if you just want to replace/impute the missing values.

library(imputeTS)
na.kalman(x)


Would be an example of a method from the imputeTS package. (https://cran.r-project.org/web/packages/imputeTS/imputeTS.pdf for a detailed method description) Also the forecast and zoo package have some methods especially for time series imputation.