Using data UKDriverDeaths
Attempting to use Holt-Winters prediction function & ggplot()
.
Basically reproduce the data in ggplot
(1) with confidence intervals (2).
This is the data:
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)
This is solution (1) to creating it in ggplot()
:
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"))
I'm looking for confidence intervals (2) for holt-winters prediction.