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)
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
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"))