dtw_dismat: DTW Distance Matrix/ Distance Vector

View source: R/dtw.R

dtw_dismatR Documentation

DTW Distance Matrix/ Distance Vector

Description

Calculate a matrix of pairwise DTW distances for a set of univariate or multivariate time series. The output matrix (or dist object) of DTW distances can easily be applied for clustering the set of time series. Or calculate a vector of DTW distances of a set of time series all relative to one query time series. Parallel computations are possible.

Usage

dtw_dismat(lot, dist_method = c("norm1", "norm2", "norm2_square"),
         step_pattern = c("symmetric2", "symmetric1"), normalize = TRUE,
         ws = NULL, threshold = NULL,
         return_matrix = FALSE, ncores = NULL, useRcppParallel = TRUE)

dtw_disvec(Q, lot, dist_method = c("norm1", "norm2", "norm2_square"),
         step_pattern = c("symmetric2", "symmetric1"), normalize = TRUE,
         ws = NULL, threshold = NULL, ncores = NULL)

Arguments

Q

time series, vector (univariate) or matrix (multivariate)

lot

List of time series. Each entry of the list is a time series as described in dtw2vec.

dist_method

character, describes the method of distance measure. See also dtw.

step_pattern

character, describes the step pattern. See also dtw.

normalize

logical, whether to return normalized pairwise distances or not. If step_pattern == 'symmetric1' only non-normalized distances can be returned (default = TRUE)

ws

integer, describes the window size for the sakoe chiba window. If NULL, then no window is applied. (default = NULL)

threshold

numeric, the threshold for early abandoning. In the calculation of the global cost matrix a possible path stops as soon as the threshold is reached. Facilitates faster calculations in case of low threshold. (default = FALSE)

return_matrix

logical, If FALSE (default) the distance matrix is returned as dist object. If TRUE a symmetric matrix of differences is returned.

ncores

integer, number of cores to be used for parallel computation of the distance matrix. If ncores = NULL (default) then ncores is set to the number of available cores minus 1. If ncores = 0 then no parallel computation is performed and standard sapply instead of parallel::parSapply is applied.

useRcppParallel

logical, if the package RcppParallel (TRUE, default) or parallel (FALSE) is used for parallel computation

Details

By setting the parameter return_matrix = FALSE (default) the output value dismat of dtw_dismat is a dist object and can easily be passed to standard clustering functions (see examples).

No matrices are allocated for calculating the pairwise distances.

Value

input

the function input parameters

dismat

the matrix of pairwise DTW distances, either as matrix or dist object

disvec

the vector DTW distances

Examples

## Not run: 

#--- Example for clustering a set of time series by feeding well known 
# clustering methods with DTW-distance objects. First we simulate 
# two prototype random walks and some cluster members. The cluster
# members are simulated by adding noise and randomly stretching and 
# comressing the time series, to get time warped time series of 
# varying lengths. The built clusters are 1:6 and 7:12.
set.seed(123)
N <- 100
rw_a <- cumsum(rnorm(N))
rw_b <- cumsum(rnorm(N))
sth <- sample(seq(0, 0.2, 0.01), size = 10)
cmp <- sample(seq(0, 0.2, 0.01), size = 10)
lot <- c(list(rw_a), 
         lapply(1:5, function(i){ 
           simulate_timewarp(rw_a + rnorm(N), sth[i], cmp[i]) 
         }),
         list(rw_b),
         lapply(6:10, function(i){ 
           simulate_timewarp(rw_b + rnorm(N), sth[i], cmp[i]) 
         }))


# Next get the distance matrix, as dist object. Per default all 
# minus 1 available cores are used:
result <- dtw_dismat(lot = lot, dist_method = "norm2", ws = 50, 
                     return_matrix = FALSE)
class(result$dismat)


# Finally you can cluster the result with the following
#  well known methods:
require(cluster)
myclus <- hclust(result$dismat)
plot(myclus)
summary(myclus)

myclus <- agnes(result$dismat)
plot(myclus)
summary(myclus)

myclus <- pam(result$dismat, k=2)
plot(myclus)
summary(myclus)
myclus$medoids



## End(Not run)

IncDTW documentation built on March 18, 2022, 6:43 p.m.