Solved – Time series with missing data period

rtime series

I have time series measured every 10 seconds with missing periods in between and a repeating pattern.How do I handle this data to fit a model and forecast the future using R

Best Answer

I would recommend a two step approach:

1. Imputation / Estimation (replacing missing values with resonable values). This is a interpolation task.

2. Forecasting (predicting future values). This is a extrapolation task.

Most forecasting methods require time series without NAs, that's why the imputation step is required.

Assuming you have a univariate time series (just one attribute observed over time), an R approach could include the packages

Both packages provide multiple algorithms, you would have to choose the best one for your specific dataset.

I will provide you an example for algortihms, which I think perform good in most of the cases:

library(imputeTS)
library(forecast)

# tsAirgap is a example time series with missing data included in imputeTS package
x <- tsAirgap

#Replace missing data using na.kalman method from imputeTS
x <- na.kalman(x)

#Perform a forecast using ets method from forecast
# The h parameter specifies how far in the future to forecast
result <- forecast(ets(x),h=10)$mean

#This is the result
result

This is the solution for univariate time series. Beware, AMELIA, which was mentioned in another answer does not work for univariate data.

If you have multivariate time series (two or more variables observed over time) the solution would look different. Here imputeTS would not work and now the AMELIA or mice or VIM packages would have to be used.