1
$\begingroup$

I've been working through the HW work in the online book A little book of R for time series analysis, and have started testing with some "live" customer data. I have a dataset that looks like:

CustomerName | Sales
123456         $5,000
123456         $3,455
123456         $7,540
123456         $2,300
987654         $5,600
987654         $6,700
987654         $1,300
987654         $690

Where I have Sales values by customer for the previous 60 months. There are ~200 customers for which I'm looking to generate a forecast. I'm able to generate a forecast for a single customer at a time, but I am having trouble finding guidance on how to run the forecast for the whole group of customers and output the results.

Ideally, the output would be the regular forecast output but with CustomerID included like so:

CustomerID | Month | Point.Forecast | Lo.80 |Hi.80 | Lo.95 | Hi.95
$\endgroup$
1
$\begingroup$

Since the output of Holt.Winters forecast isn't a table, you need to create two separate write.table() statements and append them. The first being a matrix of col.names and the second being your forecast(s).

Try something like this:

colnames <- c("CustomerID", "Month", "Point Forecast", "Lo.80","Hi.80","Lo.95","Hi.95")
titles   <- matrix(colnames, nrow=1, ncol=7)
write.table(titles,    "\pathname.csv", sep=",", row.names=FALSE, col.names=FALSE)
write.table(forecasts, "\pathname.csv", sep=",", append=TRUE,     col.names=FALSE)
$\endgroup$
  • $\begingroup$ I think I'm missing a step. So my initial input is a two-column file with customerID,sales. Once I get that file in I do this upd.ts <- ts(data=upd.df$X_c1, start=c(2012, 8), end=c(2014, 5), frequency=12) which ends up stripping my customerID out. What I need to do is pass my whole file (100s of customerIDs) into my time series and run the forecast (at least I think that's how it'll work). Unfortunately, I'm stuck on how to get that working, which I believe precedes your suggestion above. $\endgroup$ – salvyp Jun 16 '14 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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