Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
forecasting in R
# load some data
forecast_data <- structure(c("28", "42", "21", "23", "29", "15", " 8", " 4", " 2",
" 1", NA, NA, NA, NA, NA, "26", "39", "20", "22", "28", "14",
" 7", " 4", " 2", NA, NA, NA, NA, NA, "43", "65", "33", "36",
"45", "23", "12", NA, NA, NA, NA, NA, NA, NA, "58", "87", "44",
"48", "60", NA, NA, NA, NA, NA, NA, NA, NA, "37", "56", "28",
"31", NA, NA, NA, NA, NA, NA, NA, NA, NA, "28", "42", "21", NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, "19", "29", NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, "24"), .Dim = c(12L, 8L), .Dimnames = list(
c("Wk.1", "Wk.2", "Wk.3", "Wk.4", "Wk.5", "Wk.6", "Wk.7",
"Wk.8", "Wk.9", "Wk.10", "Wk.11", "Wk.12"), c("Book 1", "Book 2",
"Book 3", "Book 4", "Book 5", "Book 6", "Book 7", "Book 8"
)))
library(forecast)
forecast_data <- t(forecast_data)
colnames(forecast_data) <- forecast_data[1,1:8]
forecast_data <- forecast_data[-1, ]
forecast_data
### manually iterating through each book's time-series to obtain forecasts
myts <- ts(as.numeric(forecast_data[,8]), start=c(2016, 1), frequency=52)
fit <- ets(myts)
plot(forecast(fit,h=3))
# get week 13 sales forecasts
forecast(fit,h=1)
# get sum sales forecasts week 13-15
sum(forecast(fit,h=3)$mean)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment