R 用dtw计算距离矩阵
在时间序列第1天到第26天,我有两个标准化读取计数矩阵用于控制和治疗。我想通过动态时间包装计算距离矩阵,然后将其用于聚类,但看起来太复杂了。我这样做了;请谁来帮忙澄清?非常感谢R 用dtw计算距离矩阵,r,dtw,R,Dtw,在时间序列第1天到第26天,我有两个标准化读取计数矩阵用于控制和治疗。我想通过动态时间包装计算距离矩阵,然后将其用于聚类,但看起来太复杂了。我这样做了;请谁来帮忙澄清?非常感谢 > head(control[,1:4]) MAST2 WWC2 PHYHIPL R3HDM2 Control_D1 6.591024 5.695156 3.388652 5.756384 Control_D1 8.043454 5.365221 6.859768 6.
> head(control[,1:4])
MAST2 WWC2 PHYHIPL R3HDM2
Control_D1 6.591024 5.695156 3.388652 5.756384
Control_D1 8.043454 5.365221 6.859768 6.936970
Control_D3 7.731590 4.868267 6.919972 6.931073
Control_D4 8.129948 5.105528 6.627016 7.090268
Control_D5 7.690863 4.729501 6.824746 6.904610
Control_D6 8.101723 5.334501 6.868990 7.115883
>
> head(lead[,1:4])
MAST2 WWC2 PHYHIPL R3HDM2
Lead30_D1 6.418423 5.610699 3.734425 5.778046
Lead30_D2 7.918360 4.295191 6.559294 6.780952
Lead30_D3 7.807142 4.294722 6.599187 6.716040
Lead30_D4 7.856720 4.432136 6.572337 6.848483
Lead30_D5 7.827311 4.204738 6.607107 6.784094
Lead30_D6 7.848760 4.458451 6.581216 6.943003
>
> dim(control)
[1] 26 2603
> dim(lead)
[1] 26 2603
library(dtw)
for (i in control) {
for (j in lead) {
result[i,j] <- dtw( dist(control[,,i],lead[,,j]), distance.only=T )$normalizedDistance
}
}
如果你的问题是“为什么我会有这个错误?”答案是你试图根据第三维度来子集一个矩阵,这是一个二维数组
见:
希望您现在可以看到您有一些问题:
i
和j
值分别是control
和lead
中的值。您可以使用它们作为它们的值,或者您可以生成索引,例如,如果您计划将其用于除获取相同值以外的其他用途,则可以为(i in seq_-along(control))dist
函数的是什么。dist
获取单个矩阵并计算其行之间的距离。您似乎试图传递两个不同矩阵的值,或者两个不同矩阵的两个子集。看起来您可能需要返回并查看xtr
i
和j
值分别是control
和lead
中的值。您可以将它们用作它们的值,或者您可以生成索引,例如,如果您计划将其用于除获取相同值之外的其他用途,则可以使用for(i in seq__on(control)
dist
函数的是什么。dist
获取单个矩阵并计算其行之间的距离。您似乎试图传递两个不同矩阵的值,或者两个不同矩阵的两个子集。看起来您可能需要返回并查看xtr
已经有类似于你的问题了, 但答案并不太详细。 这是你需要知道的部分, 在R 计算交叉距离矩阵 该软件包专门用于计算交叉距离矩阵。 您应该检查它的小插曲,以了解它已经实施了哪些措施。 其使用示例如下:
set.seed(1L)
sample_data <- matrix(rnorm(50L), nrow = 5L, ncol = 10L)
suppressPackageStartupMessages(library(proxy))
distance_matrix <- proxy::dist(sample_data, method = "euclidean",
upper = TRUE, diag = TRUE)
print(distance_matrix)
#> 1 2 3 4 5
#> 1 0.000000 2.636027 3.834764 5.943374 3.704322
#> 2 2.636027 0.000000 2.587398 4.515470 2.310364
#> 3 3.834764 2.587398 0.000000 4.008678 3.899561
#> 4 5.943374 4.515470 4.008678 0.000000 5.059321
#> 5 3.704322 2.310364 3.899561 5.059321 0.000000
使用自定义距离
proxy
的一个优点是,它为您提供了注册自定义函数的选项。
您似乎对DTW的规范化版本感兴趣,
所以你可以这样做:
ndtw <- function(x, y = NULL, ...) {
dtw::dtw(x, y, ..., distance.only = TRUE)$normalizedDistance
}
pr_DB$set_entry(
FUN = ndtw,
names = "ndtw",
loop = TRUE,
distance = TRUE
)
ndtw_distmat <- proxy::dist(sample_data, method = "ndtw",
upper = TRUE, diag = TRUE)
print(ndtw_distmat)
#> 1 2 3 4 5
#> 1 0.0000000 0.4046622 0.5075772 0.6789465 0.5290478
#> 2 0.4046622 0.0000000 0.3630849 0.4866252 0.3612722
#> 3 0.5075772 0.3630849 0.0000000 0.5678698 0.3303344
#> 4 0.6789465 0.4866252 0.5678698 0.0000000 0.5078112
#> 5 0.5290478 0.3612722 0.3303344 0.5078112 0.0000000
dtw_basic
实现只支持两个步骤模式和一个窗口类型,
但速度要快得多:
suppressPackageStartupMessages(library(microbenchmark))
microbenchmark(
proxy::dist(sample_data, method = "dtw", window.type = "sakoechiba", window.size = 5L),
proxy::dist(sample_data, method = "dtw_basic", window.size = 5L)
)
Unit: microseconds
expr min lq mean
proxy::dist(sample_data, method = "dtw", window.type = "sakoechiba", window.size = 5L) 5279.124 5621.742 6070.069
proxy::dist(sample_data, method = "dtw_basic", window.size = 5L) 657.966 710.418 776.474
median uq max neval cld
5802.354 6348.199 10411.000 100 b
752.282 814.037 1161.626 100 a
包中还包括另一个多线程实现,
虽然我还没有亲自测试过
多元或多维时间序列
单个多变量序列通常是一个矩阵,其中时间跨越行,多个变量跨越列。
DTW也适用于他们:
mv_series1 <- matrix(rnorm(15L), nrow = 5L, ncol = 3L)
mv_series2 <- matrix(rnorm(15L), nrow = 5L, ncol = 3L)
print(dtw_distance <- dtw_basic(mv_series1, mv_series2))
#> [1] 22.80421
你的案子
不管你选择什么,
您可能可以使用代理
获得结果,
但既然你没有提供全部数据,
我不能给你一个更具体的例子。
我假设dtwclust::dtw_basic(control[,1:4],lead[,1:4],normalize=TRUE)
会给出一对序列之间的距离,
假设你把每一个都当作一个包含4个变量的多变量序列。已经有类似于你的问题了, 但答案并不太详细。 这是你需要知道的部分, 在R 计算交叉距离矩阵 该软件包专门用于计算交叉距离矩阵。 您应该检查它的小插曲,以了解它已经实施了哪些措施。 其使用示例如下:
set.seed(1L)
sample_data <- matrix(rnorm(50L), nrow = 5L, ncol = 10L)
suppressPackageStartupMessages(library(proxy))
distance_matrix <- proxy::dist(sample_data, method = "euclidean",
upper = TRUE, diag = TRUE)
print(distance_matrix)
#> 1 2 3 4 5
#> 1 0.000000 2.636027 3.834764 5.943374 3.704322
#> 2 2.636027 0.000000 2.587398 4.515470 2.310364
#> 3 3.834764 2.587398 0.000000 4.008678 3.899561
#> 4 5.943374 4.515470 4.008678 0.000000 5.059321
#> 5 3.704322 2.310364 3.899561 5.059321 0.000000
使用自定义距离
proxy
的一个优点是,它为您提供了注册自定义函数的选项。
您似乎对DTW的规范化版本感兴趣,
所以你可以这样做:
ndtw <- function(x, y = NULL, ...) {
dtw::dtw(x, y, ..., distance.only = TRUE)$normalizedDistance
}
pr_DB$set_entry(
FUN = ndtw,
names = "ndtw",
loop = TRUE,
distance = TRUE
)
ndtw_distmat <- proxy::dist(sample_data, method = "ndtw",
upper = TRUE, diag = TRUE)
print(ndtw_distmat)
#> 1 2 3 4 5
#> 1 0.0000000 0.4046622 0.5075772 0.6789465 0.5290478
#> 2 0.4046622 0.0000000 0.3630849 0.4866252 0.3612722
#> 3 0.5075772 0.3630849 0.0000000 0.5678698 0.3303344
#> 4 0.6789465 0.4866252 0.5678698 0.0000000 0.5078112
#> 5 0.5290478 0.3612722 0.3303344 0.5078112 0.0000000
dtw_basic
实现只支持两个步骤模式和一个窗口类型,
但速度要快得多:
suppressPackageStartupMessages(library(microbenchmark))
microbenchmark(
proxy::dist(sample_data, method = "dtw", window.type = "sakoechiba", window.size = 5L),
proxy::dist(sample_data, method = "dtw_basic", window.size = 5L)
)
Unit: microseconds
expr min lq mean
proxy::dist(sample_data, method = "dtw", window.type = "sakoechiba", window.size = 5L) 5279.124 5621.742 6070.069
proxy::dist(sample_data, method = "dtw_basic", window.size = 5L) 657.966 710.418 776.474
median uq max neval cld
5802.354 6348.199 10411.000 100 b
752.282 814.037 1161.626 100 a
包中还包括另一个多线程实现,
虽然我还没有亲自测试过
多元或多维时间序列
单个多变量序列通常是一个矩阵,其中时间跨越行,多个变量跨越列。
DTW也适用于他们:
mv_series1 <- matrix(rnorm(15L), nrow = 5L, ncol = 3L)
mv_series2 <- matrix(rnorm(15L), nrow = 5L, ncol = 3L)
print(dtw_distance <- dtw_basic(mv_series1, mv_series2))
#> [1] 22.80421
你的案子
不管你选择什么,
您可能可以使用代理
获得结果,
但既然你没有提供全部数据,
我不能给你一个更具体的例子。
我假设dtwclust::dtw_basic(control[,1:4],lead[,1:4],normalize=TRUE)
会给出一对序列之间的距离,
假设你把每一个都当作一个包含4个变量的多变量序列。谢谢,我想用dtw R软件包动态时间包装来计算距离矩阵。我有一个控制矩阵和一个铅处理过的样本。请帮助我please@Fereshteh你明白我列出的前两个问题吗?对不起,我是农业专业的毕业生,对农业来说太陌生了R.我不想听起来很苛刻,但是如果你不能理解3个问题列表中的前两个问题,那么你需要先学习R中的基本概念,然后堆栈溢出才能帮助你解决代码问题。你不能
mv_series <- lapply(1L:5L, function(dummy) {
matrix(rnorm(15L), nrow = 5L, ncol = 3L)
})
mv_distmat_dtwclust <- proxy::dist(mv_series, method = "dtw_basic")
print(mv_distmat_dtwclust)
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 0.00000 27.43599 32.14207 36.42211 31.19279
#> [2,] 27.43599 0.00000 20.88470 23.88436 29.73219
#> [3,] 32.14207 20.88470 0.00000 22.14376 29.99899
#> [4,] 36.42211 23.88436 22.14376 0.00000 28.81111
#> [5,] 31.19279 29.73219 29.99899 28.81111 0.00000