gpt4 book ai didi

r - 在 PCA 图上测试聚类的显着性

转载 作者:行者123 更新时间:2023-12-03 21:19:00 24 4
gpt4 key购买 nike

是否可以测试 PCA 图上 2 个已知组之间聚类的显着性?测试它们的接近程度或传播量(方差)和聚类之间的重叠量等。

最佳答案

这是一种使用 ggplot(...) 的定性方法。在簇周围绘制 95% 置信椭圆。请注意 stat_ellipse(...)使用二元 t 分布。

library(ggplot2)

df <- data.frame(iris) # iris dataset
pca <- prcomp(df[,1:4], retx=T, scale.=T) # scaled pca [exclude species col]
scores <- pca$x[,1:3] # scores for first three PC's

# k-means clustering [assume 3 clusters]
km <- kmeans(scores, centers=3, nstart=5)
ggdata <- data.frame(scores, Cluster=km$cluster, Species=df$Species)

# stat_ellipse is not part of the base ggplot package
source("https://raw.github.com/low-decarie/FAAV/master/r/stat-ellipse.R")

ggplot(ggdata) +
geom_point(aes(x=PC1, y=PC2, color=factor(Cluster)), size=5, shape=20) +
stat_ellipse(aes(x=PC1,y=PC2,fill=factor(Cluster)),
geom="polygon", level=0.95, alpha=0.2) +
guides(color=guide_legend("Cluster"),fill=guide_legend("Cluster"))

产生这个:



比较 ggdata$Clustersggdata$Species显示 setosa 完美映射到集群 1,而 versicolor 在集群 2 中占主导地位,而 virginica 在集群 3 中占主导地位。 然而,集群 2 和 3 之间存在显着重叠。

感谢 Etienne Low-Decarie将这个非常有用的补充发布到 ggplot在github上。

关于r - 在 PCA 图上测试聚类的显着性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20260434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com