使用 emdist (R) 计算不同大小分布的 EMD

问题描述 投票:0回答:1

我正在尝试通过

emdist
包使用 R 中的 Earthmover 距离来查找具有不同观测数量的两个分布之间的距离。我的理解是,EMD 可以用于不同大小的分布,这确实是当分布具有不同参数时求距离的目的。

但是,我尝试运行多个代码,在所有情况下我都会收到错误:

Error in emd2d(sample, sample2, dist = "manhattan") : 
  A and B must be matrices of the same dimensions

我的代码如下:

library(emdist)

sample = structure(c(6, 4, 5, 6, 4, 0, 5, 6, 3, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1), dim = c(10L, 2L), dimnames = list(NULL, c("variable", 
"weight")))

sample2 = structure(c(0.25, 0.75, 0, 0.75, 0.75, 0.75, 0, 0, 0.75, 0.25, 
0, 0.25, 0.75, 0.75, 0.25, 0.25, 0, 0.75, 0, 0.75, 0.75, 0.75, 
0.25, 0.75, 1, 0.25, 0.75, 0.25, 0.25, 0.25, 0.25, 0.75, 0, 0.75, 
0.75, 1, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.25, 0.25, 0.25, 
0, 1, 0.25, 0.25, 0.75, 0.75, 0.25, 0.25, 1, 0, 0, 0.25, 0.25, 
0.25, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), dim = c(59L, 
2L), dimnames = list(NULL, c("variable", "weight")))

emd2d(sample,sample2)

产生错误:

Error in emd2d(sample, sample2, dist = "manhattan") : 
  A and B must be matrices of the same dimensions

我尝试转置矩阵,以便它们与包小插图中的示例相匹配:

emd2d(t(sample),t(sample2))

但是当然这不会使尺寸相同,因此不会改变任何东西。

我还尝试使用

emd
and emdr
函数,但这始终返回 0 作为距离,我认为这不正确,因为我多次更改了值,但没有得到不同的输出:

emd(sample)
emdr(sample2)

我对 EMD 的编程是否错误?从软件包手册中很难看出如何将其应用于具有不同观察数的分布。这可以用 emdist 包来完成吗?我是否误解了矩阵的结构所代表的含义?

如果有帮助,示例代码中包含的权重并不重要;我不需要它们,我的理解是这些必须在 EMD 语法中指定,所以我将它们全部包含在内。我只想计算样本的“变量”和样本2的“变量”之间的EMD。

r matrix earth-movers-distance
1个回答
0
投票

在阅读文档时,不可能根据以下内容比较两个具有不同维度的矩阵:

emd2d 将两个矩阵 A 和 B 解释为 a 上的分布 二维网格。每个网格点之间的距离 方向由 xdist 和 ydist 定义。两个矩阵都必须有 相同维度。

您的矩阵具有不同的维度,这就是它返回错误的原因:

library(emdist)

dim(sample)
#> [1] 10  2
dim(sample2)
#> [1] 59  2
emd2d(sample,sample2)
#> Error in emd2d(sample, sample2): A and B must be matrices of the same dimensions

创建于 2024-03-08,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.