r – Data window shifting in time (t)

Question:

I want to do recursive prediction. Every month (t) I need to shift the data window of the last month forward by one period (this period is monthly, that is, t+1).

dados<-read.table("C:/Biomedica/Doença/evolmensal.txt", header=T, dec=",")
dados.ts <- ts(dados, start=c(1998,03), freq=12)
    periodo = window(dados.ts, start=c(1999,01),end=c(2007,12))
periodo

    stat=c(2008,01) 
dado_de_fora=window(dados.ts,start=stat)  

recurs.pred1=ts(0,start=stat,end=c(2014,10),frequency=12) 

Here's the loop:

    for (t in 1:(length(dado_de_fora))) {
  reg.recur1=arima(window(periodo,end=c(2007,11+t)),order=c(1,0,1))
  recurs.pred1[t]=predict(reg.recur1,n.ahead=1)$pred

The problem I'm facing is that when t is equal to 2 the window continues to take only the period end=c(2007,12) that is, it doesn't go to end=c(2008,01) and so on.

When I run:

window(periodo,end=c(2007,11+3))

Should get as final data February 2008

Answer:

A possible solution without using window , using the indexers to get only a part of the time series (the [] , if it has another name, let me know and I'll fix it!)

prev <- se <- NULL
for (i in 0:12) {
dados.ts <- ts(AirPassengers[1:(24 + i)], start = start(AirPassengers), frequency = frequency(AirPassengers))
prev[i + 1] <- predict(arima(dados.ts, c(1,0,1)), 1)$pred
se[i + 1] <- predict(arima(dados.ts, c(1,0,1)), 1)$se
}
Scroll to Top