In chapter 8 of John Foreman’s book, Data Smart, he turns to forecasting demand for a fictional replica sword manufacturing business. The author focuses an Exponential smoothing method which takes Trend and Seasonality into account (ETS), known as the Holt-Winters method.
The code to generate the forecast in R is very, very concise and the author provides it in Chapter 10. We try to add value here by provide code for useful supporting and validation information, such as for the generation of the Auto-Correlation Function.
A fantastic online resource for time-series forecasting is the e-text “Forecasting: Principles and Practice” by Prof. Rob Hyndman, the author of the R forecast package, and George Athanasopoulos.
# make use of the forecast package library(forecast)
# read in the historical sword sales data sourceData <- read.csv("./SwordForecasting.csv") swordData <- ts(sourceData[,2], frequency = 12) # converts data to time series type # plot the historical sword sales data plot(swordData)
# generate a 12-month, Holt-Winters forecast (additive linear trend, multiplicative seasonality forecast) hwForecast <- forecast(ets(swordData, model="ZAM", damped = FALSE), h=12) par(col.sub = "Grey") # sets the sub-title color to grey plot(hwForecast, xlab = "Yearly Intervals", ylab = "Demand", main = "Sword Demand Forecast For Next 12 Months ", sub = "based on Holt-Winters method, 80% & 95% prediction intervals shown", xlim=c(1,5))
# Calculate the auto-correlations and plot the correlogram autoCF <- acf(hwForecast$residuals, lag.max = 12)
The ACF performance of the forecast residuals is acceptable. The R code required to generate the forecast and the ACF data, using the R ‘forecast’ is very concise. Prof. Hyndman’s ebook, referenced above, is a vital asset for understanding forecasting terms and learning how to forecast with R.