Custom Feature Lags

Nickalus Redell

2019-11-22

Purpose

The purpose of this vignette is to demonstrate how custom predictor or feature lags can be created for forecast model inputs in forecastML with the forecastML::create_lagged_df() function. The rationale behind creating custom feature lags is to improve model accuracy by removing noisy or redundant features in high dimensional training data. Keeping only those feature lags that show high autocorrelation or cross-correlation with the modeled outcome–e.g., 3 and 12 months for monthly data–is a good place to start.

Load packages and data

library(forecastML)
library(DT)

data("data_seatbelts", package = "forecastML")
data <- data_seatbelts

data <- data[, c("DriversKilled", "kms", "PetrolPrice", "law")]
DT::datatable(head(data, 5))


Create a date column

ts_date_interval <- "1 month"

dates <- seq(as.Date("1969-01-01"), as.Date("1984-12-01"), by = ts_date_interval)

Custom feature lags

horizons <- c(1, 6, 12)  # forecasting 1, 1:6, and 1:12 months into the future.

# A list of length 3, one slot for each modeled forecast horizon.
lookback_control <- vector("list", length(horizons))
lookback_control <- lapply(lookback_control, function(x) {
  list(
    c(3, 12),  # column 1: DriversKilled
    1:3,       # column 2: kms
    1:12,      # column 3: PetrolPrice
    12         # column 4: law
    )
  })

data_train <- forecastML::create_lagged_df(data, type = "train", outcome_col = 1,
                                           horizons = horizons, 
                                           lookback_control = lookback_control,
                                           dates = dates,
                                           frequency = ts_date_interval)


plot(data_train)


Removing features

horizons <- c(1, 6, 12)  # forecasting 1, 1:6, and 1:12 months into the future.

# A list of length 3, one slot for each modeled forecast horizon.
lookback_control <- vector("list", length(horizons))
lookback_control <- lapply(lookback_control, function(x) {
  
  lapply(1:4, function(x) {1:12})  # 12 feature lags for each of our 4 modeled features.
  })

# Find the column index of the feature that we're removing.
remove_col <- which(grepl("PetrolPrice", names(data)))

# Remove the feature from the 12-month-out lagged data.frame.
lookback_control[[which(horizons == 12)]][remove_col] <- list(NULL)

data_train <- forecastML::create_lagged_df(data, type = "train", outcome_col = 1, 
                                           lookback_control = lookback_control,
                                           horizons = horizons, dates = dates,
                                           frequency = ts_date_interval)


plot(data_train)[[remove_col]]  # we're selecting 1 of our 4 feature-level plots.


DT::datatable(head(data_train$horizon_12))