Skip to content

Instantly share code, notes, and snippets.

@pybokeh
Created April 1, 2016 22:27
Show Gist options
  • Select an option

  • Save pybokeh/e4c4ed2d8d000f6645e5f86c22226c56 to your computer and use it in GitHub Desktop.

Select an option

Save pybokeh/e4c4ed2d8d000f6645e5f86c22226c56 to your computer and use it in GitHub Desktop.
read_input_example
library(readxl)
library(zoo)
library(dplyr)
library(forecast)
df <- read_excel('path_to_xlsx', sheet='Sheet1')
by_ro <- df %>%
group_by(RO_YR_MTH) %>%
summarize(count = n())
by_ro$RO_YR_MTH <- as.Date(as.yearmon(by_ro$RO_YR_MTH))
newdf <- data.frame(RO_YR_MTH = seq(min(by_ro$RO_YR_MTH), max(by_ro$RO_YR_MTH), by = "month"))
df_final <- merge(newdf, by_ro, by = "RO_YR_MTH", all.x = TRUE)
df_final[is.na(df_final)] <- 0
ro_mth.min <- as.Date(as.yearmon(min(df_final$RO_YR_MTH)))
yr_start <- as.numeric(format(ro_mth.min,'%Y'))
mth_start <- as.numeric(format(ro_mth.min,'%m'))
dft = ts(df_final$count,frequency=12,start=c(yr_start,mth_start))
hwm = HoltWinters(dft)
num_months <- as.numeric(readline("How months you want to forecast into the future? "))
hwm = HoltWinters(dft)
hwf = forecast.HoltWinters(hwm, h=num_months)
summary(hwf)
plot.forecast(hwf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment