Skip to content

Instantly share code, notes, and snippets.

@thekensta
Last active August 29, 2015 14:25
Show Gist options
  • Save thekensta/dc731515bda2572f5718 to your computer and use it in GitHub Desktop.
Save thekensta/dc731515bda2572f5718 to your computer and use it in GitHub Desktop.
Auto Arima from data.frame embedding forecast in actuals
##
## Wrap forecast auto.arima(..) and forecast(..) into a data.frame
## Embeds the forecast into the data.frame
##
## Allow passing an EndDate so that the forecast can start mid-actuals
## (helps with visualization and exaplantion)
##
## Usage:
## Forecast.df <- AutoArimaForecast(Monthly.df, # DataFrame with
## H = 6, # Predict 6 months forward
## ValueCol = "Purchases", # name of value to predict
## DateCol = "Month", # datecolumn
## EndDate = ymd("2015-05-01"), # predict from start of month
## Freq = "month") # monhtly not daily
##
## AutoArimaForecastPlot(Forecast.df,
## "Month", # Date Column
## "Purchases", # Actuals Column
## "lightgreen") # colour for predicted range
##
AutoArimaForecast <- function(DF,
H,
ValueCol = "Bookings",
DateCol = "Date",
Freq = c("day", "month"),
EndDate = NULL,
...) {
if (Freq == "month") {
## Monthly Data, Season = 12 month per year
Interval <- months
.F <- 12
} else if (Freq == "day") {
## Daily Data, Season = 7 days per week
Interval <- days
.F <- 7
}
# If
if (is.null(EndDate)) {
TimeSpan <- nrow(DF)
df <- DF
EndDate <- max(DF[[DateCol]])
} else {
#TimeSpan <- interval(StartDate, EndDate) %/% Interval(1) + 1
df <- DF[DF[[DateCol]] <= EndDate,]
}
## Create the timeseries
## Fit the model and
## Forecast
Ts <- ts(df[[ValueCol]], frequency = .F)
Mdl <- auto.arima(Ts, ...)
Fcast <- forecast(Mdl, h = H)
## Paste Forecast back into the DataFrame
Forecast <- as.data.frame(Fcast)
names(Forecast) <- c("Forecast", "lower.80", "upper.80", "lower.95", "upper.95")
Forecast[[DateCol]] <- EndDate + Interval(1:H)
full_join(DF, Forecast, by = DateCol)
}
AutoArimaForecastPlot <- function(DF,
xcol,
ycol,
.color = "red",
.xlim = NULL) {
p <- ggplot(aes_string(xcol, ycol), data = DF) +
geom_line() +
geom_point(shape=1) +
geom_line(aes_string(y=Forecast), color = .color) +
geom_point(aes(y=Forecast), color = .color) +
geom_ribbon(aes(ymin=lower.95, ymax=upper.95), fill = .color, alpha=0.25) +
geom_ribbon(aes(ymin=lower.80, ymax=upper.80), fill = .color, alpha=0.25) +
theme_classic()
if (! is.null(.xlim)) {
p <- p + xlim(.xlim)
}
p
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment