【问题标题】:Mahalanobis distance with multiple observations per group每组有多个观测值的马氏距离
【发布时间】:2015-06-18 22:30:36
【问题描述】:

我想计算以下物种群中的Mahalanobis distance

  • i) 有两个以上的组(两个以上的物种)。
  • ii) 需要考虑多个变量(此类物种的特征)。
  • iii) 每组有多个观察值(在数据框中,这意味着每个物种有不止一行)。

我试图了解在这种情况下如何在 R 中运行 mahalanobis 函数。这个问题类似于:

Mahalanobis distance on R for more than 2 groups

但在那里,只使用了一个变量。怎么可能有多个变量呢?

下面有一个例子,我相信它重现了我的实际数据。

Sp. X1  X2  X3
A   0.7 11  215
B   0.8 7   214
B   0.8 6.5 187
C   0.3 4   456
D   0.4 3   111
A   0.1 7   205
A   0.2 7   196
C   0.1 9.3 77
D   0.6 8   135
D   0.8 4   167
B   0.4 6   228
C   0.1 5   214
A   0.4 7   156
C   0.5 2   344

Sp。 = 物种; X1、X2、X3为观测变量。

在真实的数据集中,有超过 50 个物种,并且观察的数量在它们之间有所不同(从 100 行/物种到 1000 行)。

【问题讨论】:

    标签: r statistics cluster-analysis similarity mahalanobis


    【解决方案1】:

    这就是我所拥有的,使用 HDMD 包中的 pairwise.mahalanobis 函数:

    #data
    a = structure(list(Sp = structure(c(1L, 2L, 2L, 3L, 4L, 1L, 1L, 3L,4L, 4L, 2L, 3L, 1L, 3L), .Label = c("A", "B", "C", "D"), class = "factor"), 
                       X1 = c(0.7, 0.8, 0.8, 0.3, 0.4, 0.1, 0.2, 0.1, 0.6, 0.8,0.4, 0.1, 0.4, 0.5), 
                       X2 = c(11, 7, 6.5, 4, 3, 7, 7, 9.3,8, 4, 6, 5, 7, 2), 
                       X3 = c(215L, 214L, 187L, 456L, 111L, 205L,196L, 77L, 135L, 167L, 228L, 214L, 156L, 344L)),
                  .Names = c("Sp","X1", "X2", "X3"), 
                  row.names = c(NA, -14L),
                  class = "data.frame")
    
    library(HDMD) #pairwise.mahalanobis function
    library(cluster) #agnes function
    
    group = matrix(a$Sp) #what is being compared
    group = t(group[,1]) #prepare for pairwise.mahalanobis function
    
    variables = c("X1","X2","X3") #variables (what is being used for comparison)
    variables = as.matrix(a[,variables]) #prepare for pairwise.mahalanobis function
    
    mahala_sq = pairwise.mahalanobis(x=variables, grouping=group) #get squared mahalanobis distances (see mahala_sq$distance).
    names = rownames(mahala_sq$means) #capture labels
    
    mahala = sqrt(mahala_sq$distance) #mahalanobis distance
    rownames(mahala) = names #set rownames in the dissimilarity matrix
    colnames(mahala) = names #set colnames in the dissimilarity matrix
    
    mahala #this is the mahalanobis dissimilarity matrix 
    
             A        B         C         D
    A  0.00000 17.78689  86.83294  62.65437
    B 17.78689  0.00000  69.07937  80.31577
    C 86.83294 69.07937   0.00000 149.36579
    D 62.65437 80.31577 149.36579   0.00000
    
    #This is how I used the dissimilarity matrix to find clusters.
    cluster = agnes(mahala,diss=TRUE,keep.diss=FALSE,method="complete") #hierarchical clustering
    plot(cluster,which.plots=2) #plot dendrogram
    

    【讨论】:

      猜你喜欢
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 2016-04-21
      • 2013-09-10
      • 1970-01-01
      • 2023-03-12
      • 2016-04-14
      相关资源
      最近更新 更多