Search code examples
rstatisticstime-seriesforecasting

R Forecast multiple ARIMA models at once for pseudo out-of-sampling


I am trying to apply a pseudo out-of-sample method on forecasting GDP Growth data using auto.arima(). I have a .csv with data from 1996Q1 to 2016Q4 and I want to generate one-year-ahead forecasts starting from 2000Q1, which means my first forecast should be based on 1996Q1-1999Q1, my second forecast should be based on 1996Q2-1999Q2 etc and my last forecast (for 2016Q4) should be based on 2011Q4-2015Q4.

I figured out how to apply an ARIMA model for my subset and store a forecast one year ahead:

data = read.csv2(choose.files()) #loading file
data$Euroraum <- ts(data$Euroraum, start=c(1996, 1), end=c(2016, 4), frequency=4) #make it a time series
GDP_1999Q1 <- window(data$Euroraum, start=c(1996, 1), end=c(1999, 1)) #generate first subset for producing first forecast
fit_GDP2000Q1 = auto.arima(GDP_1999Q1, ic=c("aic")) 
Forecast_GDP2000Q1 = forecast(fit_GDP2000Q1,h=4)

But obviously I want to automate this and store all my forecasts as another time series. I'm sorry if this has an obvious solution or is already explained somewhere else, tried my best to google an appropriate solution.

My .csv looks like this:

GEOTIME Euroraum
1996Q1  1,1
1996Q2  1,4
1996Q3  2,1
1996Q4  1,7
1997Q1  1
1997Q2  3,1
1997Q3  2,6
1997Q4  3,5
1998Q1  4,2
1998Q2  2,4
1998Q3  2,8
1998Q4  2,1
1999Q1  2,2
1999Q2  2,6
1999Q3  2,9
1999Q4  4,1
2000Q1  4,9
2000Q2  4,3
2000Q3  3,4
2000Q4  2,7
2001Q1  3
2001Q2  2,3
2001Q3  1,8
2001Q4  1,4
2002Q1  0,1
2002Q2  1,1
2002Q3  1,5
2002Q4  0,9
2003Q1  0,9
2003Q2  0
2003Q3  0,4
2003Q4  1,1
2004Q1  2,2
2004Q2  2,7
2004Q3  2,2
2004Q4  2
2005Q1  0,9
2005Q2  2
2005Q3  1,8
2005Q4  1,7
2006Q1  3,6
2006Q2  2,6
2006Q3  3
2006Q4  3,5
2007Q1  3,4
2007Q2  3,1
2007Q3  3
2007Q4  2,3
2008Q1  1,7
2008Q2  1,6
2008Q3  0,6
2008Q4  -2,1
2009Q1  -5,5
2009Q2  -5,9
2009Q3  -4,3
2009Q4  -2
2010Q1  1,2
2010Q2  2,5
2010Q3  2,4
2010Q4  2,3
2011Q1  2,9
2011Q2  1,8
2011Q3  1,4
2011Q4  0
2012Q1  -0,2
2012Q2  -1,2
2012Q3  -1,2
2012Q4  -1,1
2013Q1  -1,8
2013Q2  -0,3
2013Q3  0,4
2013Q4  0,6
2014Q1  1,4
2014Q2  0,9
2014Q3  1,1
2014Q4  1,3
2015Q1  1,9
2015Q2  2,1
2015Q3  2
2015Q4  2,3
2016Q1  1,7
2016Q2  2,2
2016Q3  1,7
2016Q4  1,4

Thanks for any help in advance!


Solution

  • I found a way to list my desired one-year-ahead-forecasts directly using a for-loop:

    forecasts = numeric(length = 68)
    
    for (i in 1:68) {
        forecasts[i]<-forecast(auto.arima(ts(data$Euroraum[i:(12+i)], frequency = 4)), h=4)$mean[4]
      }
    forecasts