1

I'm just trying to get the For loop to increment a rolling window with a 1 month increment and determine the HW parameters at each increment.

ss<-c(29,36,36,48,93,28,35,28,37,50,37,3,25,28,40,45,38,43,34,44,43,25,33,34)
ss2<-t(ss)
for (i in 1:12){
sseries<-ts(ss2[c(i:11+i)],frequency=12)
ssforecasts <- HoltWinters(sseries, beta=FALSE, gamma=FALSE)
ssforecasts
}

But I get :

Error in ts(cbind(xhat = final.fit$level[-len - 1], level = final.fit$level[-len - : 'ts' object must have one or more observations

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Angus
  • 355
  • 2
  • 12
  • 2
    Try having some `cat` commands in your for loop so that you have a better idea of what is happening and what is causing the error. For example, `for (i in 1:12){ sseries <- ts(ss2[c(i:11+i)],frequency=12) cat(paste0("Information for Series ",i,"\n")) cat(sseries) cat("\n") ssforecasts <- HoltWinters(sseries, beta=FALSE, gamma=FALSE) print(ssforecasts) cat("\n") }` – Kerry Jackson Sep 05 '18 at 21:09

2 Answers2

2

You are calling a slice properly, but R's order of evaluation is not evaluating as you seem to want it to. When you get to i=11 you get this:

> i:11+i
[1] 22

which is what is giving the error, try this instead:

ss<-c(29,36,36,48,93,28,35,28,37,50,37,3,25,28,40,45,38,43,34,44,43,25,33,34)
ss2<-t(ss)
for (i in 1:12){
  sseries<-ts(ss2[c(i:(11+i))],frequency=12)
  ssforecasts <- HoltWinters(sseries, beta=FALSE, gamma=FALSE)
  ssforecasts
}
Stedy
  • 7,359
  • 14
  • 57
  • 77
0

OK, I had to use the statement print(ssforecasts) to get the results.

Angus
  • 355
  • 2
  • 12