user3476463 user3476463 - 1 month ago 13
R Question

For Loop with Append issue

I'm having trouble using append in a for loop. The goal of the for loop below is to take the ModelCast$mean and append 3 more forecast$mean on to it. So in the end length(append_cast) = 40. Instead I'm getting a length(append_cast) = 20. I think the problem is that append is only being run once.

The earlier code outside the for loop trains and forecasts a model. The rest of the code in the for loop is to fit that model to 3 new chunks of data.

This is inspired by Rob Hyndman's blog post:
http://robjhyndman.com/hyndsight/rolling-forecasts/

specifically the part on Multi-step forecasts without re-estimation

Any tips are greatly appreciated.

Code:

library("forecast")
library("tseries")
library("sqldf")
library("manipulate")
library("caret")
library("qdapTools")
library("RODBC")
library("dplyr")
library("yts")


##Partitioning Time Series
EndLearn1<-length(tsData-3*10)
data_tsLearn1 <-tsData[1:EndLearn1]

##Fit Model
fit_Model <- auto.arima(data_tsLearn1)

##Forecast Model 1st time
ModelCast<-forecast(fit_Model, h=10)

for(i in 1:3)
{
startLearn2<-1+i*10
EndLearn2<-EndLearn1+i*10

y <- tsData[startLearn2:EndLearn2]
fit_new <- Arima(y, model=fit_Model )

append_cast <- append(ModelCast$mean,forecast(fit_new, h=10)$mean, after=i*10) ## Only seems to append one forecast instead of 3
}


Update: The change to the code below works
append_cast <- ts()


for(i in 1:3)
{
startLearn2<-1+i*10
EndLearn2<-EndLearn1+i*10-1

y <- tsData[startLearn2:EndLearn2]
fit_new <- Arima(y, model=fit_Model )

append_cast <- append(append_cast, forecast(fit_new, h=10)$mean, after=i*10)
append_final<-ts(append(ModelCast$mean,append_cast[2:length(append_cast)])) ## First value in append_cast is na because ts() starts that way
}

Data:
dput(tsData[1:300])
c(7.6, 0.6, 2.2, 1.8, NA, 6.6, 12.8, 0.2, 5.6, 2, NA, 0.4, NA,
1.6, 0.8, 2, 0.4, NA, NA, 2, 4.8, NA, 0.2, NA, NA, NA, NA, 0.2,
0.2, 0.2, 0.2, 0.2, NA, 0.4, 0.8, 0.6, 20, 27.4, 0.4, 29.2, 30.4,
0.2, 42.2, NA, 0.8, 0.2, 2, 32.2, 2.4, 7, 2.2, 30.8, 26.6, 15.2,
12, 10.2, 27, 15.8, 22.2, 20, 23.8, 1, 18.2, 6, 23, 16.2, 17,
1.8, 17.8, 8.8, 0.2, 7.8, 2.6, 0.2, 17.8, 2.4, 15.4, 3.2, 8,
12.4, 3.2, NA, 3.2, 2.2, 5.6, NA, 0.4, 2.6, 1.8, 2.4, NA, 2.2,
NA, 1.6, NA, NA, NA, 1.4, 1.8, 0.2, NA, 1.8, NA, 1.4, 1.6, 5,
0.2, NA, NA, 1.4, 0.4, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2.8,
3, 0.2, 11.4, 13.2, 15, 13.2, 5.8, 6.8, 24.6, 17, 21.6, 5, 11.4,
23, 9.2, 7.2, 12, 31.6, 43, 1.2, 38.2, 38.4, 15.2, 43.6, 29.6,
20, 3.8, 23, 3.2, 15.4, 14.6, 17.4, 27.6, 24, 27.8, 35.4, 2.4,
12.4, 36, NA, 0.2, 15.6, 0.4, 20.8, 3.4, 22.8, 23.8, 25.6, 34,
NA, 0.6, 5.6, 1.8, NA, NA, 28.6, NA, NA, 40.4, NA, 16.2, 13,
4, NA, NA, 1.6, 1.2, 6, NA, 1.6, 1.2, NA, 1.8, NA, NA, 0.4, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.4, NA, NA, 0.8, 3.4,
3.2, 3.2, 3.8, 4.4, 2.8, 5.2, 11, 10.8, 0.2, 1.8, 2.4, 4.4, 2,
1.2, 1.8, 4.6, 6, 5, 8.6, 10.6, 10.4, 10, 7.2, 7.6, 0.2, 17,
3, 2.6, 1.4, 2.8, 0.2, NA, 7.4, 17.4, 7.4, 0.6, NA, 5.2, 1.6,
NA, 7.2, 2.6, 7.2, 7.6, 15, 21.8, NA, 2, NA, 10.4, 4, 2.4, 4.8,
1.4, NA, NA, 0.6, NA, 1.2, 0.2, NA, 1.4, 3, 0.4, 1, NA, 1.6,
38.8, NA, NA, NA, 0.2, NA, NA, NA, NA, NA, NA, 0.2, 1.8, 2.8,
4.2, 1.6, 7.4, 2, 6, 8)

Answer

the append function expects you to pass it the thing you want to append to as well as what you want to append to it. You're only passing the latter. I sussed this out by (don't tell anyone) reading the documentation for append.

The other things you'll need are to initialize the vector append_Cast so that it will be there to append to on the first loop. And you will need to wrap your values you want to append into a c() wrapper so they will be a vector. Something like this:

 append_cast <- {}

  for(i in 1:3)
  {  
    i<-1
    startLearn2<-1+i*10
    EndLearn2<-EndLearn1+i*10

    y <- tsData[startLearn2:EndLearn2] 
    fit_new <- Arima(y, model=fit_Model )

    append_cast <- append(append_cast, c(ModelCast$mean,forecast(fit_new, h=10)$mean, after=i*10)) ## Only seems to append one forecast instead of 3
  }
Comments