1

I am trying to do a holt winters forecast for a dataset which is of this pattern:

>Insample

Region   Week     Sales
   x    01/1/2013   200
   x    08/1/2013   250
   x    15/1/2013   185
   x    22/1/2013   375
   y    01/1/2013   155
   y    08/1/2013   160
   y    15/1/2013   225
   y    22/1/2013   200
   z    01/1/2013   345
   z    08/1/2013   285
   z    15/1/2013   300 
   z    22/1/2013   325

I have been following the little book of R and Rob Hyndman's Otexts. But to my understanding, we can consider only one dataset at a time. But considering the number of regions I have here in this dataset, I might have to store data for each region separately in the workspace and read from there everytime. That doesn't seem efficient. Is there any way to handle this?

I had the same issue with arima as well but someone suggested using this and it worked:

arima_fits <- group_by(Insample, Region) %>% do(fit=auto.arima(.$Sales))

But this doesn't seem to help with HW.

hw_fits <- group_by(Insample, Region) %>% do(fit=hw(.$Sales))

Error:

Error in ets(x, "AAA", alpha = alpha, beta = beta, gamma = gamma, damped = damped, : Nonseasonal data

Since I got this error, I tried doing it per region (univariate series - sales of 1 region) and it worked fine.

fit1 <- hw(Region1, seasonal="additive")

Any suggestions?

currarpickt
  • 2,290
  • 4
  • 24
  • 39
Shraddha
  • 155
  • 3
  • 16
  • Why does this not work? Please show what you have tried, ideally with a reproducible example. – Andrie Aug 04 '14 at 11:43
  • Hi Andrie.. I updated the question. Does this help? – Shraddha Aug 04 '14 at 11:57
  • 1
    @Shraddha. You can check this ``. Based on the solution, you may try: `Insample %>% group_by(Region) %>% mutate(year= as.numeric(str_extract(Week, perl('(?<=\\/)\\d+$'))), tsR= ts(Sales, start=year, frequency=52)) %>% do(data.frame(fc = forecast(ets(.$tsR))))` – akrun Aug 04 '14 at 12:37
  • Thanks @akrun I am not very sure what `(year= as.numeric(str_extract(Week, perl('(?<=\\/)\\d+$')))` this bit of code does. But I tried without it - `hw_fits <- Insample %>% group_by(Region) %>% mutate(tsR= ts(Sales, frequency=7)) %>% do(data.frame(fc = forecast(ets(.$tsR))))` and it works! But again, I tried the same with `hw` and it didnt work! I am confused why `hw` doesn't work and `ets` does.. – Shraddha Aug 04 '14 at 13:02
  • Nevermind, as long as `ets` worked, it's good as it chooses the best model! If you want to put that up as an answer instead of a comment, I can vote it up.. Thank you :) – Shraddha Aug 04 '14 at 13:32
  • @Shraddha, Both of them works. if `res` is the former and `res1` latter, `identical(res,res1)#[1] TRUE`. I used `frequency=52` meaning the number of weeks in a year (you had 4 observations per month). Also, your code didn't mention `start`. I was trying to extract the `year` from `Week` column using `str_extract` – akrun Aug 04 '14 at 14:35
  • Oh okay! Now I get the `str_extract` one! But, I thought `freq` is the frequency of the data, so if my data has observations per week, should I not be mentioning freq=7? I could be wrong, it would be great if you could clarify. Thanks again! – Shraddha Aug 04 '14 at 14:52
  • @Shraddha. I think for daily data, you could use `freq=7` For weekly data, it may be a little difficult as you have to take leap years into consideration. Check this link – akrun Aug 04 '14 at 15:03
  • Thank you... the discussion with you helped a lot! – Shraddha Aug 04 '14 at 15:06

2 Answers2

2
 Insample <- read.table(text="Region Week Sales
  x 01/1/2013 200
  x 08/1/2013 250
  x 15/1/2013 185
  x 22/1/2013 375
  y 01/1/2013 155
  y 08/1/2013 160
  y 15/1/2013 225
  y 22/1/2013 200
  z 01/1/2013 345
  z 08/1/2013 285
  z 15/1/2013 300
  z 22/1/2013 325",sep="",header=T,stringsAsFactors=F)

  library(stringr)
  library(dplyr)
  library(forecast)

  Insample %>% 
  group_by(Region) %>%
  mutate(year= as.numeric(str_extract(Week, perl('(?<=\\/)\\d+$'))), #extract year from Week column
       tsR= ts(Sales, start=year, frequency=52)) %>% 
       do(data.frame(fc = forecast(ets(.$tsR))))
akrun
  • 874,273
  • 37
  • 540
  • 662
0
hltwtr<-function(x)   
{

tsfull<- ts(x$Sales, start=c(2013,2), freq=12)

  fr<-hw(tsfull, seasonal="additive", bootstrap=TRUE, simulate=TRUE,h=1, alpha = 0.7)

  fr<-data.frame(fr)

  return(fr)
}

i6<-split(Insample,list(Insample$Region))

dspt6<-i6[sapply(i6, function(x) dim(x)[1]) >= 2]

forecast<-sapply(dspt6,function(x) hltwtr(x))

forecast<-data.frame(forecast)

forecast <- t(forecast)

forecast <- as.data.frame(forecast)

forecast <- forecast[c(1)]

forecast <- do.call(rbind,forecast)

forecast <- t(forecast)

write.csv(forecast,'D:/forecast.csv')'
FelixSFD
  • 6,052
  • 10
  • 43
  • 117