Skip to content

Instantly share code, notes, and snippets.

@refik
Created December 14, 2016 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save refik/84d98236448a6da2c57a1a92bc69eaff to your computer and use it in GitHub Desktop.
Save refik/84d98236448a6da2c57a1a92bc69eaff to your computer and use it in GitHub Desktop.
Holt winter forecasting for Audio production data
library(tidyverse)
library(forecast)
library(readxl)
tugce_file <- "/Users/refik/Desktop/her madde kodu için winters modeli.xlsx"
output <- "/Users/refik/Desktop/holt-winters.csv"
excel <- read_excel(tugce_file, col_names = FALSE)
products <- excel[5:nrow(excel), 1:30]
colnames(products) <- c(unlist(excel[4, 1:6]),
paste0("month ", sprintf("%02d", 1:24)))
predict <- products %>%
# Tidy sales values into time series
select(`Ürün kodu`, starts_with("month")) %>%
gather(month, sold, -`Ürün kodu`) %>%
spread(`Ürün kodu`, sold) %>%
select(-month) %>%
mutate_all(funs(as.numeric)) %>%
summarise_all(funs(list(ts(., start = c(2015, 1), frequency = 12)))) %>%
gather(`Ürün kodu`, series) %>%
right_join(select(products, -starts_with("month"))) %>%
# Make forecasting
mutate(winter = lapply(series, HoltWinters)) %>%
mutate(predict = lapply(winter, forecast, h = 12)) %>%
do({
bind_cols(products,
bind_rows(lapply(.$predict, function(prediction) {
as.data.frame(prediction) %>%
rownames_to_column("Month") %>%
select(Month, `Point Forecast`) %>%
add_row(Month = "Total", `Point Forecast` = sum(.$`Point Forecast`)) %>%
mutate(Month = factor(Month, Month)) %>%
spread(Month, `Point Forecast`)
}))
)
})
write_excel_csv(predict, output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment