# Want to make a function which allows for recursive window forecasting

I have been looking for a function that can make recursive window out-of-sample forecasts, but seems there is none. So I'm thinking about about making a function that can be used for recursive window forecasting in an ARIMA model. However I know little about programming, so I'm seeking for help.

What I want to do is use the function forecast.Arima (forecast package) to predict future values in a expanding window. Suppose 20 years is the initial window, and I expand the window by 1 year on each iteration until it is of size 30 years. More specifically, use 20 years data to predict one value, use 21 years data to predict the next value, etc.

## 1 Answer

The code below probably fits your need, but I use the forecast package. I simulate a 30-year time series to illustrate.

 library(forecast) set.seed(1234) y <- ts(sort(rnorm(30)), start = 1978, frequency = 1) # annual data fcasts <- vector(mode = "list", length = 10L) for (i in 1:10) { # start rolling forecast # start from 1997, every time one more year included win.y <- window(y, end = 1996 + i) fit <- auto.arima(win.y) fcasts[[i]] <- forecast(fit, h = 1) }
{"ft_lang_label":"__label__en","ft_lang_prob":0.93392307,"math_prob":0.76354176,"size":647,"snap":"2019-35-2019-39","text_gpt3_token_len":144,"char_repetition_ratio":0.12752722,"word_repetition_ratio":0.0,"special_character_ratio":0.2194745,"punctuation_ratio":0.099236645,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9919434,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-08-19T06:25:44Z\",\"WARC-Record-ID\":\"<urn:uuid:f680e359-a2f4-422d-9abd-ef19272b3e40>\",\"Content-Length\":\"132750\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:81e98d58-14ca-42f9-be83-8855830986d5>\",\"WARC-Concurrent-To\":\"<urn:uuid:f49633a5-1d9d-488d-b8f7-301a70c9a20c>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://stats.stackexchange.com/questions/91675/want-to-make-a-function-which-allows-for-recursive-window-forecasting\",\"WARC-Payload-Digest\":\"sha1:PYI4J3QDS25TX5JTSRT4CJEZIOVS4YSB\",\"WARC-Block-Digest\":\"sha1:CDIDZ2PNMPFSGZAWJCXMVVSNX553H3GO\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-35/CC-MAIN-2019-35_segments_1566027314667.60_warc_CC-MAIN-20190819052133-20190819074133-00264.warc.gz\"}"}