如何在R中生成层次聚类的Scree图?

时间:2017-02-19 23:57:44

标签: r

我使用plot()函数生成了树形图,并使用hclust()进行层次聚类。我希望为此生成一个碎石图。有什么建议吗?

3 个答案:

答案 0 :(得分:1)

有点晚了,但是我有一个答案。

# creating a dissimilarity matrix
res.dist <- dist(USArrests, method = "euclidean")

# creating an object of class "hclust"
res.hc <- hclust(d = res.dist, method = "ward.D2")

documentation to hclust中所示,它是值的列表。您可以使用

检查它们
View(res.hc)

现在,可变高度正好是碎石图所需的高度。以下代码生成了一个碎石图:

> ggplot(res.hc$height %>%
+            as.tibble() %>%
+            add_column(groups = length(res.hc$height):1) %>%
+            rename(height=value),
+        aes(x=groups, y=height)) +
+     geom_point() +
+     geom_line()

基本上,您要做的是绘制多个组的高度。 (这可能不是很优雅,我很高兴听到较短的版本产生相同的结果。)

我的结果是:

enter image description here

答案 1 :(得分:0)

library(nFactors)
ev <- eigen(cor(mydata)) # get eigenvalues
ap <- parallel(subject=nrow(mydata),var=ncol(mydata),
rep=100,cent=.05)
nS <- nScree(x=ev$values, aparallel=ap$eigen$qevpea)
plotnScree(nS)

答案 2 :(得分:0)

请在这里查看youtube链接,这将是有用的

https://www.youtube.com/watch?v=aMYCFtoBrdA

此致

链接到谷歌驱动器上的R代码以供下载 https://drive.google.com/file/d/0Byo-GmbU7XciVGRQcTk3QzdTMjA/view?usp=sharing

R代码

#-----------------------------------------------
# Hierarchical clustering with the sample data
#------------------------------------------------


# Reading data into R similar to CARDS

temp_str <- "Name physics math
P 15 20
Q 20 15
R 26 21
X 44 52
Y 50 45
Z 57 38
A 80 85
B 90 88
C 98 98"

base_data <- read.table(textConnection(
  temp_str), header = TRUE)
closeAllConnections()

# Check distinct categories of Variables useing STR function
str(base_data)

# Plot data 
plot(base_data$physics, base_data$math, 
     pch=21, bg=c("red","green3","blue","red","green3","blue",
                  "red","green3","blue")[unclass(base_data$Name)],
     main="Base Data")




# Step 01- obtain distance matrix (right way)
my_dist <- dist(base_data[c(2,3)], method = "euclidean")
print(my_dist)

# Step 02- Apply Hierarchical Clustering
fit <- hclust(my_dist, method="ward.D2")

# Step 03- Display dendogram
plot(fit, labels = base_data$Name)


Dendogram_Height=0
for (i in 2:9) Dendogram_Height[i] <- fit$height[i-1]
plot(1:9, Dendogram_Height, type="b", xlab="Sequence of merging",
     ylab="Dendogram Height")
plot(9:1, Dendogram_Height, type="b", xlab="# of clusters",
     ylab="Dendogram Height")




# Step 04- draw dendogram with color borders 
# One can use this step to take a look at execution
rect.hclust(fit, k=8, border="red")
plot(fit, labels = base_data$Name)
rect.hclust(fit, k=7, border="red")
plot(fit, labels = base_data$Name)
rect.hclust(fit, k=6, border="red")

# draw color borders around required clusterd
plot(fit, labels = base_data$Name)
rect.hclust(fit, k=3, border="blue")

# cut tree into 3 clusters
my_groups <- cutree(fit, k=3)