3

In the following example, I am trying to use Holt-Winters smoothing on daily data, but I run into a couple of issues:

# generate some dummy daily data
mData = cbind(seq.Date(from = as.Date('2011-12-01'), 
         to = as.Date('2013-11-30'), by = 'day'), rnorm(731))

# convert to a zoo object
zooData = as.zoo(mData[, 2, drop = FALSE], 
                 order.by = as.Date(mData[, 1, drop = FALSE], format = '%Y-%m-%d'),
                 frequency = 7)

# attempt Holt-Winters smoothing
hw(x = zooData, h = 10, seasonal = 'additive', damped = FALSE, 
   initial = 'optimal', exponential = FALSE, fan = FALSE)

# no missing values in the data
sum(is.na(zooData))

This leads to the following error:

Error in ets(x, "AAA", alpha = alpha, beta = beta, gamma = gamma, damped = damped, : You've got to be joking. I need more data! In addition: Warning message: In ets(x, "AAA", alpha = alpha, beta = beta, gamma = gamma, damped = damped, : Missing values encountered. Using longest contiguous portion of time series

Emphasis mine.

Couple of questions: 1. Where are the missing values coming from? 2. I am assuming that the "need more data" arises from attempting to estimate 365 seasonal parameters?

Update 1:

Based on Gabor's suggestion, I have recreated a fractional index for the data where whole numbers are weeks.

I have a couple of questions.
1. Is this is an appropriate way of handling daily data when the periodicity is assumed to be weekly?
2. Is there is a more elegant way of handling the dates when working with daily data?

library(zoo)
library(forecast)

# generate some dummy daily data
mData = cbind(seq.Date(from = as.Date('2011-12-01'), 
                       to = as.Date('2013-11-30'), by = 'day'), rnorm(731))

# conver to a zoo object with weekly frequency
zooDataWeekly = as.zoo(mData[, 2, drop = FALSE], 
                 order.by = seq(from = 0, by = 1/7, length.out = 731))


# attempt Holt-Winters smoothing
hwData = hw(x = zooDataWeekly, h = 10, seasonal = 'additive', damped = FALSE, 
   initial = 'optimal', exponential = FALSE, fan = FALSE)
plot(zooDataWeekly, col = 'red')
lines(fitted(hwData))
tchakravarty
  • 10,736
  • 12
  • 72
  • 116
  • You have specified a frequency of 7 so its filling in 6 NAs between each pair of points to satisfy what was specified. – G. Grothendieck Mar 01 '14 at 14:11
  • @G.Grothendieck Realized that as soon as I posted. :) I guess my question really is can I get the `hw()` function to extract a weekly seasonal with daily data? What attributes would the `ts` need to have to be able to do this? – tchakravarty Mar 01 '14 at 15:50
  • Time should be measured in weeks rather than days, e.g. times of successive observations could be 0, 1/7, 2/7, ... – G. Grothendieck Mar 01 '14 at 15:54
  • @G.Grothendieck Have updated. Will be great if you could take a look. – tchakravarty Mar 01 '14 at 17:08
  • Yes, that is ok. Could alternately use `zooreg(mData[, 2], 0, freq = 7)` or `ts(mData[, 2], start = 0, freq = 7)`. – G. Grothendieck Mar 02 '14 at 00:12

1 Answers1

3

hw requires a ts object not a zoo object. Use

zooDataWeekly <- ts(mData[,2], frequency=7)

Unless there is a good reason for specifying the model exactly, it is usually better to let R select the best model for you:

fit <- ets(zooDataWeekly)
fc <- forecast(fit)
plot(fc)
Rob Hyndman
  • 30,301
  • 7
  • 73
  • 85
  • Rob, thanks for your answer. I tend to use zoo since it is a little easier to specify and gets coerced to ts as required. Between your answer and Gabor's, I have one unanswered query, and that is how to properly handle dates in ts objects when also declaring frequency. I tend to expect that most functions will retain dates from the zoo object passed to them. – tchakravarty Mar 03 '14 at 03:04
  • Unfortunately ts is not good at handling dates except when the period is specified in years (rather than weeks as here). – Rob Hyndman Mar 03 '14 at 03:46
  • 1
    `hw` converts its first argument to `ts` so any time series class with an `as.ts` method will be ok provided you ensure that it converts to the appropriate ts object. zoo and zooreg do have as.ts methods. You can try `as.ts(z)` where z is a zoo or zooreg object just to check. – G. Grothendieck Mar 03 '14 at 20:25