K-均值聚类方法
K-Means Clustering Method
Description of the data
我正在尝试在 R
中生成一个合适的聚类均值图形显示。
如何将属性放在 x-axis
上并将每个集群的 means
视为项目的轨迹?
所有数据都是连续的。
以下方法如何:由于您的变量在相似的测量尺度(例如李克特尺度)上,您可以显示每个变量在每个聚类中的分布(例如使用箱线图)并通过使用相同的方法直观地比较它们的分布每个集群的轴限制。
这可以通过将数据置于合适的格式并使用 ggplot2
包生成绘图来实现。如下所示。
第 1 步:生成模拟数据以模仿您拥有的数值数据
生成的数据包含四个非负整数变量和一个具有 3 个簇的簇变量。
library(ggplot2)
set.seed(1717) # make the simulated data repeatable
N = 100
nclusters = 3
cluster = as.numeric( cut(rnorm(N), breaks=nclusters, label=seq_len(nclusters)) )
df = data.frame(cluster=cluster,
x1=floor(cluster + runif(N)*5),
x2=floor(runif(N)*5),
x3=floor((nclusters-cluster) + runif(N)*5),
x4=floor(cluster + runif(N)*5))
df$cluster = factor(df$cluster) # define cluster as factor to ease plotting code below
tail(df)
table(df$cluster)
其输出是:
cluster x1 x2 x3 x4
95 2 5 2 5 2
96 3 5 4 0 3
97 3 3 3 1 7
98 2 5 4 3 3
99 3 6 1 1 7
100 3 5 1 2 5
1 2 3
15 64 21
即,在 100 个模拟案例中,数据包含集群 1 中的 15 个案例、集群 2 中的 64 个案例和集群 3 中的 21 个案例。
第 2 步:准备绘图数据
这里我们使用 stats
包中的 reshape()
将数据集从宽转换为长,以便四个数字变量 (x1
、x2
、x3
, x4
) 被放入单列,适用于为四个变量中的每一个生成一个箱线图,然后按聚类变量分组。
vars2transpose = c("x1", "x2","x3", "x4")
df.long = reshape(df, direction="long", idvar="id",
varying=list(vars2transpose),
timevar="var", times=vars2transpose, v.names="value")
head(df.long)
table(df.long$cluster)
其输出是:
cluster var value id
1.x1 1 x1 5 1
2.x1 1 x1 3 2
3.x1 3 x1 5 3
4.x1 1 x1 1 4
5.x1 2 x1 3 5
6.x1 1 x1 2 6
1 2 3
60 256 84
请注意,由于数据现在采用转置长格式,因此每个集群中的案例数量增加了 4 倍(即数字变量的数量)。
第 3 步:使用线连接方式按聚类创建变量的箱线图
我们为每个变量 x1
、x2
、x3
、x4
绘制水平箱线图,显示它们在每个集群中的分布,并用连接标记平均值红色十字(您所追求的轨迹)。
gg <- ggplot(df.long, aes(x=var, y=value))
gg + facet_grid(cluster ~ ., labeller=label_both) +
geom_boxplot(aes(fill=cluster)) +
stat_summary(fun.y="mean", geom="point", color="red", pch="x", size=3) +
stat_summary(fun.y="mean", geom="line", color="red", aes(group=1)) +
coord_flip() # swap the x and y axis to make boxplots horizontal instead of vertical
生成下图。
该图表可能包含您拥有的许多变量,因此您可能需要:
- 要么通过删除最后
coord_flip()
行来显示垂直箱线图
- 或者完全删除箱线图,只显示通过删除
geom_boxplot()
线连接的红色十字。
如果你想在不同的集群中并排比较每个变量,你可以交换分组和 x 轴变量,如下所示:
gg <- ggplot(df.long, aes(x=cluster, y=value))
gg + facet_grid(var ~ ., labeller=label_both) +
geom_boxplot(aes(group=cluster, fill=cluster)) +
stat_summary(fun.y="mean", geom="point", color="red", pch="x", size=3) +
stat_summary(fun.y="mean", geom="line", color="red", aes(group=1)) +
coord_flip() # swap the x and y axis to make boxplots horizontal instead of vertical
Description of the data
我正在尝试在 R
中生成一个合适的聚类均值图形显示。
如何将属性放在 x-axis
上并将每个集群的 means
视为项目的轨迹?
所有数据都是连续的。
以下方法如何:由于您的变量在相似的测量尺度(例如李克特尺度)上,您可以显示每个变量在每个聚类中的分布(例如使用箱线图)并通过使用相同的方法直观地比较它们的分布每个集群的轴限制。
这可以通过将数据置于合适的格式并使用 ggplot2
包生成绘图来实现。如下所示。
第 1 步:生成模拟数据以模仿您拥有的数值数据
生成的数据包含四个非负整数变量和一个具有 3 个簇的簇变量。
library(ggplot2)
set.seed(1717) # make the simulated data repeatable
N = 100
nclusters = 3
cluster = as.numeric( cut(rnorm(N), breaks=nclusters, label=seq_len(nclusters)) )
df = data.frame(cluster=cluster,
x1=floor(cluster + runif(N)*5),
x2=floor(runif(N)*5),
x3=floor((nclusters-cluster) + runif(N)*5),
x4=floor(cluster + runif(N)*5))
df$cluster = factor(df$cluster) # define cluster as factor to ease plotting code below
tail(df)
table(df$cluster)
其输出是:
cluster x1 x2 x3 x4
95 2 5 2 5 2
96 3 5 4 0 3
97 3 3 3 1 7
98 2 5 4 3 3
99 3 6 1 1 7
100 3 5 1 2 5
1 2 3
15 64 21
即,在 100 个模拟案例中,数据包含集群 1 中的 15 个案例、集群 2 中的 64 个案例和集群 3 中的 21 个案例。
第 2 步:准备绘图数据
这里我们使用 stats
包中的 reshape()
将数据集从宽转换为长,以便四个数字变量 (x1
、x2
、x3
, x4
) 被放入单列,适用于为四个变量中的每一个生成一个箱线图,然后按聚类变量分组。
vars2transpose = c("x1", "x2","x3", "x4")
df.long = reshape(df, direction="long", idvar="id",
varying=list(vars2transpose),
timevar="var", times=vars2transpose, v.names="value")
head(df.long)
table(df.long$cluster)
其输出是:
cluster var value id
1.x1 1 x1 5 1
2.x1 1 x1 3 2
3.x1 3 x1 5 3
4.x1 1 x1 1 4
5.x1 2 x1 3 5
6.x1 1 x1 2 6
1 2 3
60 256 84
请注意,由于数据现在采用转置长格式,因此每个集群中的案例数量增加了 4 倍(即数字变量的数量)。
第 3 步:使用线连接方式按聚类创建变量的箱线图
我们为每个变量 x1
、x2
、x3
、x4
绘制水平箱线图,显示它们在每个集群中的分布,并用连接标记平均值红色十字(您所追求的轨迹)。
gg <- ggplot(df.long, aes(x=var, y=value))
gg + facet_grid(cluster ~ ., labeller=label_both) +
geom_boxplot(aes(fill=cluster)) +
stat_summary(fun.y="mean", geom="point", color="red", pch="x", size=3) +
stat_summary(fun.y="mean", geom="line", color="red", aes(group=1)) +
coord_flip() # swap the x and y axis to make boxplots horizontal instead of vertical
生成下图。
该图表可能包含您拥有的许多变量,因此您可能需要:
- 要么通过删除最后
coord_flip()
行来显示垂直箱线图 - 或者完全删除箱线图,只显示通过删除
geom_boxplot()
线连接的红色十字。
如果你想在不同的集群中并排比较每个变量,你可以交换分组和 x 轴变量,如下所示:
gg <- ggplot(df.long, aes(x=cluster, y=value))
gg + facet_grid(var ~ ., labeller=label_both) +
geom_boxplot(aes(group=cluster, fill=cluster)) +
stat_summary(fun.y="mean", geom="point", color="red", pch="x", size=3) +
stat_summary(fun.y="mean", geom="line", color="red", aes(group=1)) +
coord_flip() # swap the x and y axis to make boxplots horizontal instead of vertical