Skip to content

Instantly share code, notes, and snippets.

@christophsax
Last active December 3, 2017 15:07
Show Gist options
  • Select an option

  • Save christophsax/dcac9d25f22bf5b1010d3976cf621a1f to your computer and use it in GitHub Desktop.

Select an option

Save christophsax/dcac9d25f22bf5b1010d3976cf621a1f to your computer and use it in GitHub Desktop.
Forecasting monthly time series with common trends
library(tsbox)  # remotes::install_github("christophsax/tsbox")
library(forecast)
library(seasonal)

(Short) Time Series with common pattern

Monthly time series, with 4 to 5 years of data. Here, Monthly Deaths from Lung Diseases in the UK

ts_plot(mdeaths, fdeaths)

Extracting a few 'core trends'

Principal Component Decomposition, is easy, fast and powerful. Note that the sign is undefined, so I had to switch it for the graph.

ts_prcomp <- ts_(prcomp, predict, scale = TRUE)
core_trend <- ts_prcomp(ts_c(mdeaths, fdeaths))[,1]
ts_plot(ts_scale(ts_c(-core_trend, mdeaths, fdeaths)))

Forecasting 'core trends'

This may be a more extended excercise. We may use some additional information here. E.g., overall sales projections, economic projections, etc. Or we could also specify some scenarios here, e.g., strong growth, weak growth etc.

Here, I am doing an exponential smoothing forecast, which is simply based on the history of the series

fm <- forecast(core_trend, h = 12)
plot(fm)

core_trend_fct <- ts_rbind(core_trend, fm$mean)

Using 'core trends' forecasts to forecast individual series:

Note that the confidence intervalls are not valid anymore, because fm$mean is already an estimate.

One such 'core trend' could also be the number of trading days

m <- forecast(auto.arima(fdeaths, xreg = core_trend), xreg = fm$mean)
fdeaths_fct <- ts_rbind(fdeaths, m$mean)
plot(m)

Seasonal adjustment

Using standard X-11 methodology

This already performs a standard trading day adjustment, but no significant effects have been found. Also performs automatic outlier detection.

plot(seas(fdeaths_fct, x11 = ""))

library(tsbox) # remotes::install_github("christophsax/tsbox")
library(forecast)
library(seasonal)
#' ### (Short) Time Series with common pattern
#' Monthly time series, with 4 to 5 years of data. Here, Monthly Deaths from
#' Lung Diseases in the UK
ts_plot(mdeaths, fdeaths)
#' ### Extracting a few 'core trends'
#'
#' Principal Component Decomposition, is easy, fast and powerful. Note that the
#' sign is undefined, so I had to switch it for the graph.
ts_prcomp <- ts_(prcomp, predict, scale = TRUE)
core_trend <- ts_prcomp(ts_c(mdeaths, fdeaths))[,1]
ts_plot(ts_scale(ts_c(-core_trend, mdeaths, fdeaths)))
#' ### Forecasting 'core trends'
#'
#' This may be a more extended excercise. We may use some additional information
#' here. E.g., overall sales projections, economic projections, etc. Or we could
#' also specify some scenarios here, e.g., strong growth, weak growth etc.
#'
#' Here, I am doing an exponential smoothing forecast, which is simply based on
#' the history of the series
fm <- forecast(core_trend, h = 12)
plot(fm)
core_trend_fct <- ts_rbind(core_trend, fm$mean)
#' ### Using 'core trends' forecasts to forecast individual series:
#'
#' Note that the confidence intervalls are not valid anymore, because `fm$mean`
#' is already an estimate.
#'
#' One such 'core trend' could also be the number of trading days
m <- forecast(auto.arima(fdeaths, xreg = core_trend), xreg = fm$mean)
fdeaths_fct <- ts_rbind(fdeaths, m$mean)
plot(m)
#' ### Seasonal adjustment
#'
#' Using standard X-11 methodology
#'
#' This already performs a standard trading day adjustment, but no significant
#' effects have been found. Also performs automatic outlier detection.
plot(seas(fdeaths_fct, x11 = ""))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment