Home > other >  Time Series Forecast With Different Historical Time
Time Series Forecast With Different Historical Time

Time:08-12

I have a time series data as such:

> dput(df)
 structure(list(Date = c("Q1-2000", "Q2-2000", "Q3-2000", "Q4-2000", 
 "Q1-2001", "Q2-2001", "Q3-2001", "Q4-2001", "Q1-2002", "Q2-2002", 
 "Q3-2002", "Q4-2002", "Q1-2003", "Q2-2003", "Q3-2003", "Q4-2003", 
 "Q1-2004", "Q2-2004", "Q3-2004", "Q4-2004"), x = c(14, 16, 26, 
 11, 18, 16, 14, 24, 24, 12, 20, 18, 10, 28, 16, 30, 10, 23, 20, 
 16)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
 -20L))

I wish to forecast using forecast function from the library(forecast) but with different historical time. I wish to forecast 4 Quarters ahead. For example, I want to forecast value of Q4-2002 using data from Q1-2000 to Q4-2001; forecast value for Q1-2003 using data from Q1-2000 to Q1-2002 and etc.

CodePudding user response:

forecast(df$x, h=4) will predict 4 periods ahead (including the intermediate steps)

   Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
21       18.29967 10.46771 26.13164 6.321715 30.27763
22       18.29967 10.46771 26.13164 6.321715 30.27763
23       18.29967 10.46771 26.13164 6.321715 30.27763
24       18.29967 10.46771 26.13164 6.321715 30.27763

The library will forecast n 1, then use this forecast to compute n 2 and so on. If you're only interested in the fourth step, just filter the other lines out.

This can be done like so:

sapply(5:length(df$x), function(i) forecast(df$x[1:i],h=4)[['fitted']][4])
[1] 14.90604 15.25603 16.42914 17.37555 18.11154 17.49781 17.72710 17.84675 17.15531 17.84625 17.79953 18.56279 18.06060 18.33274 18.41970 18.29978
  • Related