knitr::opts_chunk$set(
    message = FALSE,
    warning = FALSE,
    fig.width = 8, 
    fig.height = 4.5,
    fig.align = 'center',
    out.width='95%', 
    dpi = 100
)

# devtools::load_all() # Travis CI fails on load_all()

Clustering is an important part of time series analysis that allows us to organize time series into groups by combining "tsfeatures" (summary matricies) with unsupervised techniques such as K-Means Clustering. In this short tutorial, we will cover the tk_tsfeatures() functions that computes a time series feature matrix of summarized information on one or more time series.

Libraries

To get started, load the following libraries.

library(dplyr)
library(purrr)
library(timetk)

Data

This tutorial will use the walmart_sales_weekly dataset:

walmart_sales_weekly

TS Features

Using the tk_tsfeatures() function, we can quickly get the "tsfeatures" for each of the time series. A few important points:

# Custom Function
my_mean <- function(x, na.rm=TRUE) {
  mean(x, na.rm = na.rm)
}

tsfeature_tbl <- walmart_sales_weekly %>%
    group_by(id) %>%
    tk_tsfeatures(
      .date_var = Date,
      .value    = Weekly_Sales,
      .period   = 52,
      .features = c("frequency", "stl_features", "entropy", "acf_features", "my_mean"),
      .scale    = TRUE,
      .prefix   = "ts_"
    ) %>%
    ungroup()

tsfeature_tbl

Clustering with K-Means

We can quickly add cluster assignments with the kmeans() function and some tidyverse data wrangling.

set.seed(123)


cluster_tbl <- tibble(
    cluster = tsfeature_tbl %>% 
        select(-id) %>%
        as.matrix() %>%
        kmeans(centers = 3, nstart = 100) %>%
        pluck("cluster")
) %>%
    bind_cols(
        tsfeature_tbl
    )

cluster_tbl

Visualize the Cluster Assignments

Finally, we can visualize the cluster assignments by joining the cluster_tbl with the original walmart_sales_weekly and then plotting with plot_time_series().

cluster_tbl %>%
    select(cluster, id) %>%
    right_join(walmart_sales_weekly, by = "id") %>%
    group_by(id) %>%
    plot_time_series(
      Date, Weekly_Sales, 
      .color_var   = cluster, 
      .facet_ncol  = 2, 
      .interactive = FALSE
    )

Learning More

My Talk on High-Performance Time Series Forecasting

Time series is changing. Businesses now need 10,000+ time series forecasts every day. This is what I call a High-Performance Time Series Forecasting System (HPTSF) - Accurate, Robust, and Scalable Forecasting.

High-Performance Forecasting Systems will save companies MILLIONS of dollars. Imagine what will happen to your career if you can provide your organization a "High-Performance Time Series Forecasting System" (HPTSF System).

I teach how to build a HPTFS System in my High-Performance Time Series Forecasting Course. If interested in learning Scalable High-Performance Forecasting Strategies then take my course. You will learn:

Unlock the High-Performance Time Series Forecasting Course



business-science/timetk documentation built on Feb. 1, 2024, 10:39 a.m.