Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

zoo - R: Holt-Winters with daily data (forecast package)

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))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...