我有一个时间序列列表:
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
距离作为proxy
距离pr_DB
数据库中的距离函数时,我可以使用proxy::dist
快速计算所有距离。问题是proxy::dist
似乎返回原始距离,而由于我的时间序列长度不同,我需要它返回归一化距离。 dtw::dtw
函数返回一个以normalizedDistance
作为一个元素的列表。那么如何更改proxy::dist
的返回值并使之返回normalizedDistance
?也许还有另一种方法来计算每对归一化距离? (当然,因为我的数据集很大,所以我需要避免遍历所有对。)
下面是一段简短的代码,可以帮助您查看问题:
> 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
答案 0 :(得分: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