1

I've got a list of time series:

ex <- list(ts1 = structure(c(15.33, 46.83, 69.93, 79.59, 85.785, 78.132, 
61.812, 189.108, 188.904, 159.936, 35.175, 62.37, 77.49, 85.785, 
87.36, 17.952, 198.696, 198.084, 159.936, 157.692), .Dim = 5:4, .Dimnames = list(
    NULL, c("var1", "var2", "var3", "var4")), .Tsp = c(1, 5, 
1), class = c("mts", "ts", "matrix")), ts2 = structure(c(34.65, 
43.47, 34.125, 62.424, 10.2, 45.084, 43.575, 34.125, 27.72, 10.812, 
48.756, 92.616), .Dim = 3:4, .Dimnames = list(NULL, c("var1", 
"var2", "var3", "var4")), .Tsp = c(1, 3, 1), class = c("mts", 
"ts", "matrix")), ts3 = structure(c(33.915, 59.325, 47.736, 8.772, 
54.18, 80.115, 4.08, 61.2), .Dim = c(2L, 4L), .Dimnames = list(
    NULL, c("var1", "var2", "var3", "var4")), .Tsp = c(1, 2, 
1), class = c("mts", "ts", "matrix")))

I would like to calculate the dtw distances between each pair in the list. As reading in dtw package register dtw distance as a distance function in the database of proxy distances pr_DB, I can just use proxy::dist to quickly calculate all the distances. The problem is that proxy::dist seems to return raw distances, while as my time series are of different length I need it to return normalised distance. dtw::dtw function returns a list with normalizedDistance as one element. So how can I change what is return by proxy::dist and make it return normalizedDistance? Or maybe is there another way to calculate each pair normalised distances? (Of course, as my dataset is large I need to avoid looping over all pairs).

Here's a short code to help to see an issue:

> proxy::dist(ex, method = 'dtw')
         ts1      ts2
ts2 822.2551         
ts3 909.3705 195.2110
> dtw::dtw(ex[[1]], ex[[2]])$distance
[1] 822.2551
> dtw::dtw(ex[[1]], ex[[2]])$normalizedDistance
[1] 102.7819
Kuba_
  • 886
  • 6
  • 22

1 Answers1

0

If we are looking for pairwise, then try with combn

library(dtw)
combn(ex, 2, FUN = function(x) dtw(x[[1]], x[[2]])$normalizedDistance)
#[1] 102.78188 129.91007  39.04219

Checking by individually applying the dtw function on the pairs

dtw::dtw(ex[[2]], ex[[3]])$normalizedDistance
#[1] 39.04219
dtw::dtw(ex[[1]], ex[[3]])$normalizedDistance
#[1] 129.9101
akrun
  • 874,273
  • 37
  • 540
  • 662