Data window moving in time (t)

4

I want to make a Recursive prediction. I need every month (t) to move the data window from the last month to the next in a period (this period, 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) 

Follow the looping:

    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 am facing is that when t equals 2 the window continues to take only the period end=c(2007,12) ie it does not walk to end=c(2008,01) and so on.

When I roll:

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

Should get as final data February 2008

    
asked by anonymous 25.12.2014 / 23:36

2 answers

3

A possible solution without using window , using indexers to get only part of the time series (the [] , if you have another name, let me know that I correct!)

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
}
    
26.12.2014 / 00:55
2
  

When I roll:

     

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

     

Should get as final data February 2008

Answering this point to add months:

To get what you want in this operation, you have to operate with dates. I'll give an example with the xts and lubridate packages. First we need to turn your numbers into a date format.

library(xts)
end <- c(2007, 11) # vetor de números
end <- paste(end, collapse="-") # transforma em texto
end <- as.yearmon(end) # transforma em data (formato yearmon)

Now you can add months:

end + 3/12
[1] "Fev 2008"

You can also go back to the number format using the year and month (lubridate) functions to extract the year and month numbers:

library(lubridate)
end <- end + 3/12 # adicionamos 3 meses ao vetor end
end <- c(year(end), month(end)) # transformamos o vetor end em número novamente
end
[1] 2008    2

So you could work with something like that. Now, in your specific case, it is easier to loop with an index of numbers than dates - as Rcoster put it - but it may be that another circumstance you actually need to deal with dates.

    
26.12.2014 / 03:57