Search code examples
rforecasting

one-step ahead, out of sample forecast from only one value received at a time, in R


I have 100 values of training data of a time-series and I use auto.arima to find model order and coefficients from the same.

I receive streaming values from a sensor, one at a time. On receiving one value, I need to forecast/predict the next value (one-step ahead/single value only), from the model-object obtained from auto.arima. I update model coefficients upon certain events, but right now there is no need to mention them. The on-step ahead predictions are made till the sensor is working.

These are my sample training and test data: https://drive.google.com/open?id=0B3UpwQBKryLleXdtMkQyOXVDcW8

This is my code. There are certain constraints on the model, which are set accordingly.

data<-read.csv('stackoverflow_data.csv',header=TRUE, sep=",");
data1<-data[[1]];  # first 100 points of data - training data
mdl<-auto.arima(data1,max.p=3, max.q=3,max.d=1, stepwise=FALSE, approximation = FALSE,allowdrift=TRUE, allowmean=TRUE);
summary(mdl);

Series: data1 
ARIMA(1,0,1) with non-zero mean 

Coefficients:
         ar1     ma1  intercept
      0.7456  0.2775   767.7463
s.e.  0.0804  0.1197     0.1072

sigma^2 estimated as 0.04944:  log likelihood=9.34
AIC=-10.69   AICc=-10.27   BIC=-0.27

Training set error measures:
                       ME      RMSE       MAE           MPE       MAPE
Training set -0.004354719 0.2189945 0.1706344 -0.0005753987 0.02222701
                  MASE        ACF1
Training set 0.9063639 -0.01022176

For in-sample data, one-step ahead predictions were produced manually in (red plot) as well as using fitted(mdl) in (green plot). Below shown is the combined plot of them with the original training data(black plot).

This is the code for manual one-step ahead forecast.

res_1 = 0;
res_2 = 0;
constant_1 = mdl$coef [["intercept"]] * (1 - mdl$coef [["ar1"]]);
fc = 0;
for (i in 1:length(data1)){
  fc[i] <-constant_1 +(mdl$coef [["ar1"]]*(data1[i] )) + (mdl$coef [["ma1"]]*(res_1)); # one-step ahead forecast for in-sample data
res_2[i] = data1[i] - fc[i];
res_1 = data1[i] - fc[i];
}

one step ahead forecast : in-sample data plot

These are my questions:

(1) By observing the plot (I have shared image link above, as I can't post images due to reputation score), it seems that the fitted(mdl) forecasts are off by one time unit. How this can be corrected?

(2) The test data in the shared link is future data for which one-step ahead forecast is to be done. This data come sequentially one value at a time. How we can forecast single next value from single value received at that point of time, till the time algorithm keeps getting values?


Solution

    1. You multiply the AR(1) coefficient by the current observation rather than the previous observation.

    2. Re-fit the ARIMA model to the test data.

    Easily done in R:

    library(forecast)
    
    data <- read.csv('stackoverflow_data.csv', header=TRUE, sep=",")
    data1 <- ts(data[[1]][1:100])  # first 100 points of data - training data
    mdl <- auto.arima(data1, max.p=3, max.q=3, max.d=1, 
             stepwise=FALSE, approximation=FALSE, allowdrift=TRUE, allowmean=TRUE)
    
    # One-step forecasts on training data
    plot(data1)
    lines(fitted(mdl), col='red')
    

    enter image description here

    # One-step forecasts on test data
    data2 <- ts(data[[2]], start=length(data1)+1)
    data12 <- ts(c(data1,data2))
    mdl2 <- Arima(data12, model=mdl)
    plot(data2)
    lines(window(fitted(mdl2), start=101), col='red')
    

    enter image description here