0
$\begingroup$

I want to automate the forecasting procedure for a data set that I have. I have a three years of daily historic data and I want to use 2 years as test data and one year as train data. I want to have rolling forecast which means that I read historic data+observed data for train set, and forecast for every 2 days ahead. Then I again read the data including the historic set and the two observed data and this process goes on until the whole train set is predicted. I want to automate the process. My code is like the following:

i <- 883 
while (i < 912){
orders <- window(data$orders,end=i)
 y <- msts(orders,seasonal.periods=c(7,365.25))
 model <- tbats(y)
 print(forecast(model,h=2,l=c(50,80)))
 i <- i+2}

Instead of printing forecast for each 2 days, I prefer to have an empty dataframe and add the new forecasted values to that. However, I have a problem with understanding what is the data type that this line of code is generating:

forecast(model,h=2,l=c(50,80))

it generates three rows, the first row includes headers, second row shows point forecast, low 50% and high 50% prediction intervals and low 80% and high 80% prediction intervals. so it includes 5 columns. My only problem is that each time it wants to store these results in the data frame, it stores the first row which is the header row as well. I wonder how I can store only the last two rows in the data frame.

Thanks

$\endgroup$
4
  • $\begingroup$ Please add a reproducible example for people to work with. $\endgroup$ Mar 16, 2015 at 18:06
  • 3
    $\begingroup$ I'm voting to close this question as off-topic because it is about how to use R without a reproducible example. $\endgroup$ Mar 16, 2015 at 18:07
  • $\begingroup$ Unfortunately my data is confidential and I can not share it here. $\endgroup$
    – Fairy
    Mar 16, 2015 at 18:07
  • 1
    $\begingroup$ You could try to make up an example dataset. If you cannot provide a reproducible example, your question is off-topic & should be closed. $\endgroup$ Mar 16, 2015 at 18:08

1 Answer 1

1
$\begingroup$

I assume you refer to function forecast in the package of the same name.

You do not give a reproducible minimal example, so it is difficult to know what forecast can be returning. One esay way of finding out is to assign the result of the call as follows:

object <- forecast(model,h=2,l=c(50,80))

and then do

str(object)

You will see the structure of the returned object and can decide which elements you want to store. If for instance you wanted to keep the upper and lower bounds of the prediction's confidence interval in columns of the same name in dataframe mydataframe you would write something like:

mydataframe[i,"upper"] <- object$upper

mydataframe[i,"lower"] <- object$lower

inside your loop.

$\endgroup$
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.