dtw_dist_gridsearch: Perform a grid search of parameters and 'dtwclust' distance...

View source: R/dtw-dist-gridsearch.R

dtw_dist_gridsearchR Documentation

Perform a grid search of parameters and dtwclust distance functions

Description

dtw_dist_gridsearch() performs a gridsearch using a list of parameter grids and a list of distance functions from the dtwclust package.

Usage

dtw_dist_gridsearch(
  query_tbl,
  ref_series,
  dtw_funs,
  dtw_grids,
  num_best = "all"
)

Arguments

query_tbl

Data.frame or tibble containing columns of numeric vectors for each query time series that are to be compared to the reference time series.

ref_series

Numeric vector; the reference time series which is the series that all query series will compared to.

dtw_funs

Named list of dtwclust distance functions. Names need to match those in dtw_grids

dtw_grids

Object created by create_dtw_grids() or named nested list of parameter name-value pairs that correspond to the distance functions. Names need to match those in dtw_funs.

num_best

Integer or "all"; if an integer, then that number of query series with the lowest distance values for each parameter configuration will be returned; if "all", then all results will be returned. Default is "all".

Details

The distance algorithms currently supported are:

  • dynamic time warping (dtw_basic)

  • dynamic time warping with an additional L2 Norm (dtw2)

  • dynamic time warping with lower bound (dtw_lb)

  • Triangular Global Alignment Kernel (gak)

  • Slope Based Distance (sbd)

Value

A tibble with columns for the names of the query series, names of the distance functions, parameter values, and calculated distances.

See Also

create_dtw_grids() dtwclust::dtw_basic(), dtwclust::dtw2(), dtwclust::dtw_lb(), dtwclust::gak(), dtwclust::sbd()

Examples


suppressPackageStartupMessages(library(dtwclust))

head(ohio_covid)[,1:6]

ref_series <- ohio_covid[["cases"]]
query_tbl <- dplyr::select(ohio_covid, -cases, -date)


params_ls_lg <- list(dtw_basic = list(window.size = 5:10,
                                      norm = c("L1", "L2"),
                                      step.pattern = list(symmetric1, symmetric2)),
                     dtw2 = list(step.pattern = list(symmetric1, symmetric2),
                                 window.size = 5:10),
                     dtw_lb = list(window.size = 5:10,
                                   norm = c("L1", "L2"),
                                   dtw.func = "dtw_basic",
                                   step.pattern = list(symmetric2)),
                     sbd = list(znorm = TRUE, return.shifted = FALSE),
                     gak = list(normalize = TRUE, window.size = 5:10))

dtw_grids_lg <- create_dtw_grids(params_ls_lg)

dtw_funs_lg <- list(dtw_basic = dtw_basic,
                    dtw2 = dtw2,
                    dtw_lb = dtw_lb,
                    sbd = sbd,
                    gak = gak)

search_res_lg <- dtw_dist_gridsearch(query_tbl = query_tbl,
                                     ref_series = ref_series,
                                     dtw_funs = dtw_funs_lg,
                                     dtw_grids = dtw_grids_lg,
                                     num_best = 2)

head(search_res_lg)


# Can still be ran with a minimal "grid"
params_ls_sm <- list(dtw2 = list(step.pattern = list(symmetric1)))

dtw_grids_sm <- create_dtw_grids(params_ls_sm)

dtw_funs_sm <- list(dtw2 = dtw2)

search_res_sm <- dtw_dist_gridsearch(query_tbl = query_tbl,
                                     ref_series = ref_series,
                                     dtw_funs = dtw_funs_sm,
                                     dtw_grids = dtw_grids_sm,
                                     num_best = "all")

head(search_res_sm)


ercbk/ebtools documentation built on Feb. 22, 2025, 1:51 p.m.