title: Forecasting model predict one day ahead - sliding window
body: <p>I'm struggeling with a problem. I'm using SparkR for time series forecasting, but this scenario can also transferred to normal R environment. Instead of using ARIMA model I want to use regression models such as Random Forest Regression etc. to forecast the load of one day ahead. I also read about the sliding window approach to evaluate the performance of different regressors with respect to different parameters combinations. So to get a better understanding this is an example of the structure of my dataset:</p>
  
  <pre><code>Timestamp              UsageCPU     UsageMemory   Indicator  Delay
  2014-01-03 21:50:00    3123            1231          1        123
  2014-01-03 22:00:00    5123            2355          1        322
  2014-01-03 22:10:00    3121            1233          2        321
  2014-01-03 22:20:00    2111            1234          2        211
  2014-01-03 22:30:00    1000            2222          2         0 
  2014-01-03 22:40:00    4754            1599          1         0
  </code></pre>
  
  <p>To use any kind of regressor the next step is to extract feature and transform them into a readable format, because these regressions can not read timestamps:</p>
  
  <pre><code>Year   Month  Day  Hour    Minute    UsageCPU   UsageMemory  Indicator Delay
  2014   1      3    21       50        3123        1231          1      123
  2014   1      3    22       00        5123        2355          1      322
  2014   1      3    22       10        3121        1233          2      321
  2114   1      3    22       20        2111        1234          2      211
  </code></pre>
  
  <p>The next step is to create training and test set for the model.</p>
  
  <pre><code>trainTest &lt;-randomSplit(SparkDF,c(0.7,0.3), seed=42)
  train &lt;- trainTest[[1]]
  test &lt;- trainTest[[2]]
  </code></pre>
  
  <p>Then it is possible to create the model + prediction (the setting of the randomForest is firstly not relevant):</p>
  
  <pre><code>model &lt;- spark.randomForest(train, UsageCPU ~ ., type = "regression", maxDepth = 5, maxBins = 16)
  predictions &lt;- predict(model, test)
  </code></pre>
  
  <p>So I know all these steps and by plotting the predicted data with actual data it looks quite good. But this regression model is not dynamic, which means I can not predict one day ahead. Because the features such as UsageCPU, UsageMemory etc. does not exist, I want to predict from historical values to the next day. As mentioned in the beginning the sliding window approach can work here, but I'm not sure how to apply it (on the whole dataset, only on the training or test set). </p>
  
  <p>This implementation was from <a href="https://stats.stackexchange.com/questions/3051/mean-of-a-sliding-window-in-r/3072#3072">shabbychef's</a> and <a href="https://stats.stackexchange.com/questions/3051/mean-of-a-sliding-window-in-r/3054">mbq</a>:</p>
  
  <pre><code> slideMean&lt;-function(x,windowsize=3,slide=2){
   idx1&lt;-seq(1,length(x),by=slide);
   idx1+windowsize-&gt;idx2;
   idx2[idx2&gt;(length(x)+1)]&lt;-length(x)+1;
   c(0,cumsum(x))-&gt;cx;
   return((cx[idx2]-cx[idx1])/windowsize);
  }
  </code></pre>
  
  <p>The last question deals about the window size. I want to predict the next day in hours (00,01,02,03...), but the time stamps have an interval of 10min, so in my calculation the size of a window should be 144 (10*60*24 / 10).</p>
  
  <p>Would be so nice if someone can help me. Thanks!</p>