1

我有一个时间序列列表:

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")))

我想计算列表中每对之间的 dtw 距离。由于读取dtw包注册dtw距离作为距离数据库中的距离函数proxypr_DB我可以使用它proxy::dist来快速计算所有距离。问题是它proxy::dist似乎返回了原始距离,而由于我的时间序列长度不同,我需要它来返回归一化距离。dtw::dtw函数返回一个包含normalizedDistance一个元素的列表。那么我怎样才能改变什么是 return byproxy::dist并让它 returnnormalizedDistance呢?或者也许有另一种方法来计算每对归一化距离?(当然,由于我的数据集很大,我需要避免遍历所有对)。

这是一个简短的代码,可帮助您查看问题:

> 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
4

1 回答 1

0

如果我们正在寻找成对的,然后尝试combn

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

dtw通过在对上单独应用函数进行检查

dtw::dtw(ex[[2]], ex[[3]])$normalizedDistance
#[1] 39.04219
dtw::dtw(ex[[1]], ex[[3]])$normalizedDistance
#[1] 129.9101
于 2019-01-05T08:41:40.550 回答