tags:

views:

45

answers:

1

I have a data.frame that contains historic data by day for several months. I would like to add a forecast to the end of this data.frame.

For example, I can run the following to create a simple example data.frame.

months <- c("2010-10-17","2010-10-18","2010-10-19","2010-10-20")
revenue <- c(10000,11000,10500,9500)

results <- data.frame(months,revenue)

And run this function to generate the forecast.

forecast(results$revenue)

What I am stuck on is how to add a date sequence for the rest of October and include the correct values from the forecast. I want to generate something that looks like this, adding 30 days of forecast to the historic data.

> print(results)
       months revenue
1  2010-10-17   10000
2  2010-10-18   11000
3  2010-10-19   10500
4  2010-10-20    9500
5  2010-10-21   10250
6  2010-10-22   10250
7  2010-10-23   10250
8  2010-10-24   10250
9  2010-10-25   10250
10 2010-10-26   10250
11 2010-10-27   10250
12 2010-10-28   10250
13 2010-10-29   10250
14 2010-10-30   10250
15 2010-10-31   10250

Any help is appreciated. Thanks.

+2  A: 

This is almost certainly not the ideal way to do this, but it'll get you there:

#Get the sequence from a date to another date
date.seq <- as.Date("2010-10-17"):as.Date("2010-10-31")

#Unfortunately, add.dates is numeric - but can easily be converted back to
#dates if you know the origin (which you can find at ?Date)
new.dates <- data.frame(months = as.Date(date.seq, origin = "1970-01-01"),
                        revenue = NA)

#Slap it on to your existing dates
results.fc <- rbind(results, new.dates)

That's split out into more steps than you need, but it's easier to read that way. Hope this helps.

Matt Parker
Matt, that got the dates. What about the forecast(results$revenue) part? should I do something like forecast <- forecast(results$revenue) then fc <- rbind(results.fc, forecast)?
analyticsPierce
I think I see what you mean. I can substitute forecast(results$revenue) for revenue=NA at the end of the data.frame function. However, I am getting an error that there are a different number of rows.
analyticsPierce
I got the rows to equal with forecast(results$revenue,44). When I run this though, it gets hung up on forecast() returning 5 variables;forecast, Lo 80, Hi 80, Lo 95 and Hi 95. I am not sure how to select just the forecast component.
analyticsPierce
Sorry, I am evidently not very good at actually reading questions in their entirety - I got hung up on the date expansion without actually reading the whole thing. What are you fitting the model with? If you're fitting it with an ARIMA or other time series model, I think you just need to pass the model fit and the number of periods to forecast, like: `forecast(object = your.fit, t = 14)`.
Matt Parker
If you're using a model that isn't specifically a time function, like `gam()`, I think you'd use the predict function: `predict(your.fit, newdata = new.dates)`. Or at least, that's what the author of the forecast package tells me to do in this question: http://stats.stackexchange.com/questions/173/time-series-for-count-data-with-counts-20
Matt Parker
Matt, you were right. forecast() returns a list and once I converted the right piece into a data.frame it worked fine.
analyticsPierce