Skip to contents

Overall Forecast

If we have a data.frame with a time series forecast and actuals in time, use narrate_forecast() to create the narrative around overall forecast, anticipated changes in the next time period and current year volumes.

Let’s use prophet as an example. Notice that we require a data frame here, so if you are using ts objects, please make relevant conversions first.

fit_prophet <- function(data) {
  model <- prophet::prophet(data)
  future <- prophet::make_future_dataframe(model, periods = 12, freq = "month")
  forecast <- predict(model, future)
  return(forecast)
}

grouped_data <- sales %>%
  dplyr::mutate(ds = lubridate::floor_date(Date, unit = "month")) %>%
  dplyr::group_by(Region, ds) %>%
  dplyr::summarise(y = sum(Sales, na.rm = TRUE)) %>%
  tidyr::nest()

grouped_data$forecast <- lapply(grouped_data$data, fit_prophet)

actuals <- grouped_data %>%
  dplyr::select(-forecast) %>%
  tidyr::unnest(data)

df <- grouped_data %>%
  dplyr::select(-data) %>%
  tidyr::unnest(forecast) %>%
  dplyr::select(ds, yhat) %>%
  dplyr::left_join(actuals) %>%
  dplyr::rename(Actuals = y,
                Forecast = yhat)
df %>%
  head() %>%
  kable()
Region ds Forecast Actuals
ASPAC 2019-01-01 45357.61 63603.14
ASPAC 2019-02-01 82355.02 97232.62
ASPAC 2019-03-01 95845.54 100717.52
ASPAC 2019-04-01 107592.72 104206.56
ASPAC 2019-05-01 119140.35 130189.00
ASPAC 2019-06-01 33605.81 36273.58

Actuals in the most recent year, overall forecast for the next year (if available in forecast data frame) and projected YoY change for next vs last twelve months:

narrate_forecast(df, forecast = "Forecast", actuals = "Actuals")
#> $`Current Year Actuals`
#> Actuals for 2021 are equal to 13.5 M
#> 
#> $`12 Month Projection`
#> Overall forecast for the next 12 months is 13.8 M.
#> 
#> $`Overall increase the next 12 months`
#> Projected increase in the next 12 months is equal to 302.9 K (2.24%).
#> 
#> $`2022 YTD vs 2021 YTD`
#> From 2021 YTD to 2022 YTD, Forecast had an increase of 302.88 K (2.2 %, 13.55 M to 13.85 M).
#> 
#> $`Forecast change by Month`
#> Months with biggest changes of Forecast are May (141.3 K, 13.62 %, 1 M to 1.2 M), Mar (138 K, 13.94 %, 989.3 K to 1.1 M), Nov (-108.8 K, -6.36 %, 1.7 M to 1.6 M) and Sep (97.4 K, 8.68 %, 1.1 M to 1.2 M).