【问题标题】:hierarchical clustering with gower distance - hclust() and philentropy::distance()具有高尔距离的层次聚类 - hclust() 和 philentropy::distance()
【发布时间】:2018-12-03 08:11:25
【问题描述】:

我有一个混合数据集(分类变量和连续变量),我想使用 Gower 距离进行层次聚类

我的代码基于来自https://www.r-bloggers.com/hierarchical-clustering-in-r-2/ 的示例,该示例使用基数 R dist() 表示欧几里得距离。由于dist() 不计算高尔距离,我尝试使用philentropy::distance() 来计算它,但它不起作用。

感谢您的帮助!

# Data
data("mtcars")
mtcars$cyl <- as.factor(mtcars$cyl)

# Hierarchical clustering with Euclidean distance - works 
clusters <- hclust(dist(mtcars[, 1:2]))
plot(clusters)

# Hierarchical clustering with Gower distance - doesn't work
library(philentropy)
clusters <- hclust(distance(mtcars[, 1:2], method = "gower"))
plot(clusters)

【问题讨论】:

  • philentropy::distance函数返回一个矩阵,在聚类前尝试将其转换为dist对象和as.dist
  • 我尝试了集群

标签: r cluster-analysis


【解决方案1】:

错误在于distance 函数本身。

我不知道是不是故意的,但是当前使用“gower”方法实现philentropy::distance无法处理任何混合数据类型,因为第一个操作是转置data.frame,产生一个字符矩阵,然后在传递给DistMatrixWithoutUnit 函数时抛出输入错误。

您可以尝试改用cluster 中的daisy 函数。

library(cluster)

x <- mtcars[,1:2]

x$cyl <- as.factor(x$cyl)

dist <- daisy(x, metric = "gower")

cls <- hclust(dist)

plot(cls)

编辑: 为了将来参考,philentropy 似乎将被更新以在下一版本中包含更好的类型处理。来自vignette

在 philentropy 的未来版本中,我将优化距离() 功能,以便内部检查数据类型的正确性和正确性 输入数据将比基本 dist() 花费更少的终止时间 功能。

【讨论】:

    【解决方案2】:

    LLL; 抱歉,我不懂英语,无法解释。现在这是一个尝试。 但是代码很好;-)

    library(philentropy)
    clusters <- hclust(
                       as.dist(
                              distance(mtcars[, 1:2], method = "gower")))
    plot(clusters)
    

    好看

    【讨论】:

      【解决方案3】:

      您可以使用 gower 包非常高效地完成此操作

      library(gower)
      
      d <- sapply(1:nrow(mtcars), function(i) gower_dist(mtcars[i,],mtcars))
      d <- as.dist(d)
      h <- hclust(d)
      plot(h)
      

      【讨论】:

        【解决方案4】:

        非常感谢这个好问题,感谢所有提供出色答案的人。

        只是为了为未来的读者解决问题:

        # import example data
        data("mtcars")
        # store example subset with correct data type 
        mtcars_subset <- tibble::tibble(mpg = as.numeric(as.vector(mtcars$mpg)), 
                                        cyl = as.numeric(as.vector(mtcars$cyl)), 
                                        disp = as.numeric(as.vector(mtcars$disp)))
        
        # transpose data.frame to be conform with philentropy input format
        mtcars_subset <- t(mtcars_subset)
        
        # cluster
        clusters <- hclust(as.dist(philentropy::distance(mtcars_subset, method = "gower")))
        plot(clusters)
        
        # When using the developer version on GitHub you can also specify 'use.row.names = TRUE'
        clusters <- hclust(as.dist(philentropy::distance(mtcars_subset, method = "gower", 
        use.row.names = TRUE)))
        plot(clusters)
        

        如您所见,集群现在工作得非常好。

        问题在于,在示例数据集中,cyl 列存储了factor 值,而不是philentropy::distance() 函数所需的double 值。由于底层代码是用Rcpp写的,不符合的数据类型会导致问题。正如 Esther 正确指出的那样,我将在未来版本的包中实现一种更好的检查类型安全性的方法。

        head(tibble::as.tibble(mtcars))
        
        # A tibble: 6 x 11
        mpg cyl    disp    hp  drat    wt  qsec    vs    am  gear  carb
        <dbl> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
        1  21   6       160   110  3.9   2.62  16.5     0     1     4     4
        2  21   6       160   110  3.9   2.88  17.0     0     1     4     4
        3  22.8 4       108    93  3.85  2.32  18.6     1     1     4     1
        4  21.4 6       258   110  3.08  3.22  19.4     1     0     3     1
        5  18.7 8       360   175  3.15  3.44  17.0     0     0     3     2
        6  18.1 6       225   105  2.76  3.46  20.2     1     0     3     1
        

        为了克服这个限制,我将来自mtcars 数据集的感兴趣的列存储在单独的data.frame/tibble 中,并通过as.numeric(as.vector(mtcars$mpg)) 将所有列转换为双精度值。

        生成的子集 data.frame 现在仅根据需要存储 double 值。

        mtcars_subset
        
        # A tibble: 32 x 3
         mpg   cyl  disp
        <dbl> <dbl> <dbl>
        1  21       6  160 
        2  21       6  160 
        3  22.8     4  108 
        4  21.4     6  258 
        5  18.7     8  360 
        6  18.1     6  225 
        7  14.3     8  360 
        8  24.4     4  147.
        9  22.8     4  141.
        10  19.2     6  168.
        # … with 22 more rows
        

        还请注意,如果您只为 philentropy::distance() 函数提供 2 个输入向量,则只会返回一个距离值,而 hclust() 函数将无法计算具有一个值的任何聚类。因此,我添加了第三列 disp 以启用集群的可视化。

        我希望这会有所帮助。

        【讨论】:

          猜你喜欢
          • 2012-07-09
          • 2016-05-28
          • 2016-08-21
          • 2014-10-24
          • 2012-09-05
          • 2019-01-11
          • 2015-01-07
          • 2014-02-26
          • 2016-08-09
          相关资源
          最近更新 更多