具有 gower 距离的层次聚类 - hclust() 和 philentropy::distance()
hierarchical clustering with gower distance - hclust() and philentropy::distance()
我有一个混合数据集(分类变量和连续变量),我想使用 Gower 距离进行层次聚类。
我的代码基于 https://www.r-bloggers.com/hierarchical-clustering-in-r-2/ 中的示例,该示例使用基数 R dist()
作为欧氏距离。由于 dist()
不计算 Gower 距离,我尝试使用 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)
错误出在 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
In future versions of philentropy I will optimize the distance()
function so that internal checks for data type correctness and correct
input data will take less termination time than the base dist()
function.
小儿;
抱歉,我不懂英文,无法解释。现在这是一个尝试。
但是代码很好 ;-)
library(philentropy)
clusters <- hclust(
as.dist(
distance(mtcars[, 1:2], method = "gower")))
plot(clusters)
好看
您可以使用 gower
包
非常有效地完成它
library(gower)
d <- sapply(1:nrow(mtcars), function(i) gower_dist(mtcars[i,],mtcars))
d <- as.dist(d)
h <- hclust(d)
plot(h)
非常感谢这个很好的问题,也感谢所有提供出色答案的人。
只是为了解决未来读者的问题:
# 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
数据集中感兴趣的列存储在单独的数据中。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
以实现集群的可视化。
希望对您有所帮助。
我有一个混合数据集(分类变量和连续变量),我想使用 Gower 距离进行层次聚类。
我的代码基于 https://www.r-bloggers.com/hierarchical-clustering-in-r-2/ 中的示例,该示例使用基数 R dist()
作为欧氏距离。由于 dist()
不计算 Gower 距离,我尝试使用 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)
错误出在 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
In future versions of philentropy I will optimize the distance() function so that internal checks for data type correctness and correct input data will take less termination time than the base dist() function.
小儿; 抱歉,我不懂英文,无法解释。现在这是一个尝试。 但是代码很好 ;-)
library(philentropy)
clusters <- hclust(
as.dist(
distance(mtcars[, 1:2], method = "gower")))
plot(clusters)
好看
您可以使用 gower
包
library(gower)
d <- sapply(1:nrow(mtcars), function(i) gower_dist(mtcars[i,],mtcars))
d <- as.dist(d)
h <- hclust(d)
plot(h)
非常感谢这个很好的问题,也感谢所有提供出色答案的人。
只是为了解决未来读者的问题:
# 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
数据集中感兴趣的列存储在单独的数据中。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
以实现集群的可视化。
希望对您有所帮助。