4

Using data UKDriverDeaths

Attempting to use Holt-Winters prediction function & ggplot().

Basically reproduce the data in ggplot.

data('UKDriverDeaths')    
past <- window(UKDriverDeaths, end = c(1982, 12))
hw <- HoltWinters(past)
pred <- predict(hw, n.ahead = 10)
plot(hw, pred, ylim = range(UKDriverDeaths))
lines(UKDriverDeaths)

enter image description here

I'd like to show what holt winters predicts starting in 1983 against the actual data. The 2 problems are:

1) ggplot doesn't understand ts data.

2) Using HoltWinters() uses ts data, not zoo (dates or xts). I need the prediction & the actual data at the cut point to show (usually + geom_line(aes()) does this)

If confidence intervals were possible that would be great.

Thanks so much, absolutely stuck

tonytonov
  • 25,060
  • 16
  • 82
  • 98
user3608523
  • 65
  • 1
  • 7

1 Answers1

3

I'm using xts to merge the data automatically.

library(xts)
ts_pred <- ts(c(hw$fitted[, 1], pred), start = 1970, frequency = 12)
df <- merge(as.xts(ts_pred), as.xts(UKDriverDeaths))
names(df) <- c("predicted", "actual")
ggplot(df, aes(x=as.POSIXct(index(df)))) + 
  geom_line(aes(y=predicted), col='red') + 
  geom_line(aes(y=actual), col='black') + 
  theme_bw() +
  geom_vline(xintercept=as.numeric(as.POSIXct("1982-12-01")), linetype="dashed") + 
  labs(title="Holt-Winters filtering\n", x="Time", y="Observed / Fitted") + 
  theme(plot.title = element_text(size=18, face="bold"))

enter image description here

tonytonov
  • 25,060
  • 16
  • 82
  • 98
  • If adding confidence intervals to this, All I would required is: +geom_ribbon(aes(ymin=as.POSIXct(lower), ymax=as.POSIXct(upper)) I add 2 columns (upper, lower) to df, then convert back to ts. However the error Error: ggplot2 doesn't know how to deal with data of class mtstsmatrix comes up. I did transform the new matrix to an XT series with CI's limits. Am I missing anything? Greatly appreciated. – user3608523 Jun 10 '14 at 14:08