2

I am doing a recursive one-step-ahead daily forecast with different time series models for 2010. For example:

set.seed(1096)
Datum=seq(as.Date("2008/1/1"), as.Date("2010/12/31"), "days")
r=rnorm(1096)
y=xts(r,order.by=as.Date(Datum))
List.y=vector(mode = "list", length = 365L)

for (i in 1:365) {
window.y <- window(y[,1], end = as.Date("2009-12-30") + i) 
fit.y <- arima(window.y, order=c(5,0,0))
List.y[[i]] <- forecast(fit.y , h = 1)
}

the list looks like this:

List.y
[[1]]
Point Forecast     Lo 80    Hi 80     Lo 95    Hi 95
732  -0.0506346 -1.333437 1.232168 -2.012511 1.911242
[[2]]
Point Forecast     Lo 80    Hi 80     Lo 95   Hi 95
733   0.03905936 -1.242889 1.321008 -1.921511 1.99963

....

[[365]]
 Point Forecast   Lo 80    Hi 80     Lo 95    Hi 95
 1096  0.09242849 -1.1794 1.364257 -1.852665 2.037522

And now I want to extract only the forecast value for each period [1]-[365], so I can work with the forecast data. However, I am not sure how to do this. I tried

sa=sapply(List.y[1:365], `[`, 4)

but then I only get this:

$mean
Time Series:
Start = 732 
End = 732 
Frequency = 1 
[1] -0.0506346

$mean
Time Series:
Start = 733 
End = 733 
Frequency = 1 
[1] 0.03905936

...

$mean
Time Series:
Start = 1096 
End = 1096 
Frequency = 1 
[1] 0.09242849

but I want all 365 [1] values in a numeric vector or something, so I can work with the data.

Chris Pram
  • 21
  • 1
  • Thanks, that's helpful. You'll want to set the seed [?set.seed](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Random.html) before you run `rnorm` so that everyone will get exactly the same values you got. – gung - Reinstate Monica Jan 16 '16 at 00:14
  • i edited set.seed. thanks, i hope somebody can help me – Chris Pram Jan 16 '16 at 00:25
  • You'll need to rerun the code, though. The `rnorm` call will give different output now, so all the results will be different. Don't fret, someone on [SO] should be able to help. – gung - Reinstate Monica Jan 16 '16 at 00:27

1 Answers1

0

Just use this: sa2=as.numeric(sa). sa2 will be a numeric vector of forecasted means.

Richard Hardy
  • 375
  • 6
  • 20